2
votes

Using the TwilioChatClient pod, I have successfully registered my app to Twilio Programmable Chat to receive APN notifications.

However, from what I can tell, these notifications are being created after calling client.register(withToken: deviceToken) on an instantiated TwilioChatClient client, and NOT through the application's AppDelegate didReceiveRemoteNotification method. Stranger yet, didReceiveRemoteNotification is called, but only when the application is in the active state, and not the background state, where I would like to perform some operations.

Does anyone know where and how these notifications are being created, or why didReceiveRemoteNotification is only called during the active state? Amongst other things, I would like to increment the application icon badge number with each notification sent out by the client.

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 {
    }
}

where the client.register(withToken: deviceToken) works as intended.

1
please add your code snippets - muescha
which of the didReceiveRemoteNotification methods you use? you are using this application(_:didReceiveRemoteNotification:fetchCompletionHandler:) ? - muescha
@muescha, I'm using the method that includes the fetchCompletionHandler - but I have tried both methods, and neither of them work for me. I will post my code snippets now! - Anbang Zhang
you also see to last paragraph in documentation? maybe thats why? - muescha
@muescha, which documentation are you referring to? Could you please link me? - Anbang Zhang

1 Answers

0
votes

Twilio developer evangelist here.

I've spoken with the Programmable Chat team and this is what I've found out:

  • application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is for silent notifications in the background that perform background processing only (that is, with "content-available": 1 set in the APNS notification). Programmable Chat sends notifications that show information to the user, so it won't be fired in background mode
  • Notifications can update the badge count for you though, so this is processing you don't have to do, this requires a different setting in the notification that we currently do not support, however work is being done to add that support now
  • If you want to both show a notification and do further background processing, this is not supported in regular notifications, however this is supported with iOS 10's service extensions. Programmable Chat doesn't support those either, but again, it is being worked on, so you may see that soon

Keep an eye on the Programmable Chat Changelog for these additions.

Let me know if that helps at all.