Push Notification

🚧

p12 files will be deprecated on October 1st, 2022.

Please replace your current APNs certifiacte with a p8 file. You can check p8 registration format when all p12 are removed.

This section explains how to enable push notification in Channel Plugin for iOS.

In order to enable push notification, you first have to generate p8 file from your Apple Push Certificate and then upload it to Channel through Channel Desk. (we support both development and production)

Dealing with Certificate(P8)

App-ID and turn on Push Notification

📘

Skip this step if you have already configured your app(by p8) to support Apple Push Notification Service.

  1. First, login to Apple Developer Center. Click Certificates, IDs & Profiles -> keys, register team key.
  1. Enter a Key Name and enable Apple Push notifications service (APNs).
  1. Click Register and an authentication key will be issued. The issued authentication key can be downloaded only once, so store it in a secure location. Check the Key ID.
  1. Go to Account > Membership tab, and check your Team ID.

Upload your p8 file to Channel Desk

App-ID and turn on Push Notification

On your desktop, run Channel Desk and click Settings, and then go to Follow-up Messages -> Mobile SDK Push section. Clicking "Upload file button" and enter your informations.

Register device token

To use push notification, you need to register device token to Channel server by calling initPushToken: method in application:didRegisterForRemoteNotificationWithDeviceToken delegate method.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  ChannelIO.initPushToken(deviceToken)
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [ChannelIO initPushTokenWithDeviceToken:deviceToken];
}

Store push notification

To handle incoming push notification for Channel, call [receivePushNotification:](doc: mobile-channel-io#receivePushNotification) and [storePushNotification:](doc: mobile-channel-io#storePushNotification) method in the delegate method you have used to handle your application's push notification

//iOS 10 and above
func userNotificationCenter(_ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if ChannelIO.isChannelPushNotification(userInfo) {
        ChannelIO.receivePushNotification(userInfo)
        ChannelIO.storePushNotification(userInfo)
    }
    completionHandler()
}
//iOS 10 and above
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
  didReceiveNotificationResponse:(UNNotificationResponse *)response  
  withCompletionHandler:(void (^)())completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;
    if ([ChannelIO isChannelNotification:userInfo]) {
        [ChannelIO receivePushNotification:userInfo completion: nil];
        [ChannelIO storePushNotification: userInfo];
    }
    completionHandler();
}

Open ChannelIO chat

If you want to move to the corresponding chat when when a user tap ChannelIO notification, handle push message by adding the following code in the last line of your UIViewController's viewDidLoad method.

class ViewController : UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    
    if ChannelIO.hasStoredPushNotification() {
      ChannelIO.openStoredPushNotification()
    }
  }
}
@implementation ViewController

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if ([ChannelIO hasStoredPushNotification]) {
        [ChannelIO openStoredPushNotification]
    }
}

@end

Combine with SMS feature

We also send SMS in case a user has a mobile number. If you do not want users to receive SMS notification but only push notification, turn SMS feature off in Channel Desk. On the other hand, you can take advantage on SMS feature and use it as a fallback method (when push notification did not reach).

Go to Capabilities tab and turn on Background Mode. Check on both Background fetch and Remote notification.

Next you implement application:didReceiveRemoteNotification:fetchCompletionHandler delegate method and call handlePushNotification: within that method.

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if ChannelIO.isChannelPushNotification(userInfo) {
        // This line
        ChannelIO.receivePushNotification(userInfo)
        ChannelIO.storePushNotification(userInfo)
    }
    completionHandler()
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if ([ChannelIO isChannelNotification:userInfo]) {
        [ChannelIO receivePushNotification:userInfo completion: ^{
            completionHandler(UIBackgroundFetchResultNoData);
        }];
        [ChannelIO storePushNotification: userInfo];
    } else {
        completionHandler(UIBackgroundFetchResultNoData);
    }
}

❗️

Maybe you need to add Notification Extension

When user or system terminate app, IOS block background task even if push received. So you maybe need to add additional code in Notification Extension.
If do not this, you can received push and sms together.

func application(_ application: didReceiveRemoteNotification userInfo: fetchCompletionHandler completionHandler:)
Above function sometimes do not called on app terminated. So we nee to use Notification Exention.

Extension Install Info:Notification Extension