0

enter image description here

I have used Twilio Chat SDK for my iOS app. The notifications when new message is added to the channel is coming up fine. But it doesn't update the app's notification badge count. I have attached the screenshot for push notification configuration in Twilio Portal.

Also when I ask for user authorization for notification,

the options set are:

  let options: UNAuthorizationOptions = [.badge, .sound, .alert]
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63

1 Answers1

1

Your notification payload should look like the following to update app badge count:

{
    "aps" : {
        "alert" : "You got notificaiton.",
        "badge" : 5
    }
}

When this payload is received the badge count updates to 5.

Update: As mentioned in the Twilio documentation:

To update badge count on an application icon, you should pass badge count from the Chat Client delegate to the application:

func chatClient(_ client: TwilioChatClient, notificationUpdatedBadgeCount badgeCount: UInt) {
    UIApplication.shared.applicationIconBadgeNumber = Int(badgeCount)
}

Note: But, this only works when the App is active. To update the badge count when the App is inactive you need to configure notification payload to the above-mentioned format from via a custom Twilio server, default configuration doesn't allow this.

When the app is inactive you can use this method to append the badge count:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if UIApplication.shared.applicationState != .active {
        UIApplication.shared.applicationIconBadgeNumber += 1
    }
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • The message payload is created by Twilio itself. Which is not configurable, except for the notification message. – Prajeet Shrestha Jun 10 '20 at 07:22
  • The method you mentioned in your updated answer, will only get called when app is in foreground. Need to update badge count when app is in background or closed. – Prajeet Shrestha Jun 10 '20 at 07:35
  • For badge count to be updated payload needs to be in the above format, there's no other way. – Frankenstein Jun 10 '20 at 07:42
  • I've added a method to append badge count when app is inactive. – Frankenstein Jun 10 '20 at 07:51
  • So I have found this link https://www.twilio.com/docs/notify/send-notifications?code-sample=code-receiving-a-notification-in-ios-1&code-language=Objective-C&code-sdk-version=default. I think we need a server side implementation for sending a badge count. Default Twilio config won't allow this. – Prajeet Shrestha Jun 10 '20 at 08:01
  • Yes, configuring the Twilio server-side would fix the issue. – Frankenstein Jun 10 '20 at 08:02