2017-01-25 62 views
1

使用TwilioChatClient窗格,我已成功地將我的應用程序註冊到Twilio Programmable Chat以接收APN通知。iOS Swift Twilio可編程聊天推送通知

但是,據我所知,這些通知是在實例化TwilioChatClient客戶端上調用client.register(withToken: deviceToken)後創建的,而不是通過應用程序的AppDelegate didReceiveRemoteNotification方法創建的。更奇怪的是,didReceiveRemoteNotification被調用,但只有當應用程序處於活動狀態時,而不是後臺狀態時,我希望執行某些操作。

有誰知道在哪裏以及如何創建這些通知,或者爲什麼didReceiveRemoteNotification僅在活動狀態期間被調用?除其他外,我想通過客戶端發出的每個通知來增加應用程序圖標徽章號。

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    print("Registered for notifications"); 
    if UserUtils.client?.userInfo != nil { 
     print("Has info"); 
     UserUtils.deviceToken = deviceToken; 
     UserUtils.client?.register(withToken: deviceToken) 
    } else { 
     print("No info"); 
     updatedPushToken = deviceToken as NSData? 
    } 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    print("Received a notification"); 
    if UIApplication.shared.applicationState != .active { 
     print(userInfo); 
     UserUtils.client?.handleNotification(userInfo); 
     UIApplication.shared.applicationIconBadgeNumber += 1; 
     if UserUtils.client?.userInfo != nil { 
      print(userInfo); 
      let jsonNotification = JSON(userInfo["aps"]) 
      let alert = jsonNotification["alert"].stringValue + "\"}"; 
      print(JSON.init(parseJSON: alert)["body"]); 
     } else { 
      print(userInfo); 
      let jsonNotification = JSON(userInfo["aps"]) 
      let alert = jsonNotification["alert"].stringValue + "\"}"; 
      print(JSON.init(parseJSON: alert)["body"]); 
     } 
    } else { 
    } 
} 

其中client.register(withToken: deviceToken)按預期工作。

+0

請添加您的代碼片段 – muescha

+0

您使用了哪個'didReceiveRemoteNotification'方法?你正在使用這個[應用程序(_:didReceiveRemoteNotification:fetchCompletionHandler:)](https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623013-application)? – muescha

+0

@muescha,我正在使用包含fetchCompletionHandler的方法 - 但我已經嘗試了這兩種方法,並且它們都不適用於我。我現在將發佈我的代碼片段! –

回答

0

Twilio開發人員在這裏傳播。

我已經說出了與可編程聊天球隊,這就是我發現:

  • application(_:didReceiveRemoteNotification:fetchCompletionHandler:)是在後臺靜默通知執行後臺處理只(即與"content-available": 1集APNS通知)。可編程聊天會向用戶發送顯示信息的通知,所以它不會在後臺模式下被觸發
  • 通知可以爲您更新徽章計數,所以這是您不必處理的處理,這需要一個不同的在我們目前不支持的通知中設置,但是現在正在完成添加該支持的工作
  • 如果要同時顯示通知和進一步後臺處理,則在常規通知中不支持此操作,但支持此操作與iOS 10的service extensions。可編程聊天不支持那些要麼,但同樣,它正在開發中,所以你可以看到,不久

留意的Programmable Chat Changelog這些補充。

讓我知道這是否有幫助。