Push Notification

Enable / Disable Push Notification

You can set whether users receive push notifications.

// Enable Push Notification
[[TalkPlus sharedInstance] enablePushNotification:^(TPUser *tpUser) {
    // SUCCESS
} failure:^(int errorCode, NSError *error) {
    // FAILURE    
}];

// Disable Push Notification
[[TalkPlus sharedInstance] disablePushNotification:^(TPUser *tpUser) {
    // SUCCESS
} failure:^(int errorCode, NSError *error) {
    // FAILURE    
}];

You can set whether you want to receive push notifications from specific channels.

// Enable Push Notification from the channel
[[TalkPlus sharedInstance] enableChannelPushNotification:channel 
    success:^(TPChannel *tpChannel) {
    // SUCCESS
} failure:^(int errorCode, NSError *error) {
    // FAILURE    
}];

// Disable Push Notification from the channel
[[TalkPlus sharedInstance] disableChannelPushNotification:channel 
    success:^(TPChannel *tpChannel) {
    // SUCCESS    
} failure:^(int errorCode, NSError *error) {
    // FAILURE
}];

Push Notification

Push Notification is handled by FCM.

First, integrate FCM on TalkPlus dashboard. Then, when a user logs in, call registerFCMToken function to register FCM token from that device.

The following code demonstrates how you can obtain and register an FCM token.

#import <UserNotifications/UserNotifications.h>
@import Firebase;

// initialize Firebase
[FIRApp configure];
    
// request push notification authorization
UNUserNotificationCenter *center = 
    [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge)
    completionHandler:^(BOOL granted, NSError *error) {
    NSLog(@"granted: %@", granted ? @"YES":@"NO");
}];

// obtain token through Firebase Messaging
[[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error getting FCM registration token: %@", error);
    } else {
        NSLog(@"FCM registration token: %@", token);
        [self registerFCMToken:token];
    }
}];

-(void)registerFCMToken:(NSString *)fcmToken {
    [[TalkPlus sharedInstance] registerFCMToken:fcmToken success:^{
        NSLog(@"fcmToken register success");
    } failure:^(int errorCode, NSError *error) {
        NSLog(@"fcmToken register failure");
    }];
}

When a push notification arrives, TalkPlus SDK first processes the payload. Then it passes the event to an already registered delegate via handleFCMMessage function.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
    willPresentNotification:(UNNotification *)notification
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    // called when push notification arrives while in foreground
    NSDictionary *userInfo = notification.request.content.userInfo;
    if ([userInfo objectForKey:@"talkplus"] != nil) {
        NSLog(@"talkplus payload");
        [[TalkPlus sharedInstance] handleFCMMessage:[userInfo objectForKey:@"talkplus"]];
    }
    if (@available(iOS 14, *)) {
        completionHandler(UNNotificationPresentationOptionBadge|
                          UNNotificationPresentationOptionSound|
                          UNNotificationPresentationOptionBanner);
    } else {
        completionHandler(UNNotificationPresentationOptionBadge|
                          UNNotificationPresentationOptionSound);
    }
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
    didReceiveNotificationResponse:(UNNotificationResponse *)response
    withCompletionHandler:(void (^)(void))completionHandler
{
    // called when push notification is clicked while in background
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    if ([userInfo objectForKey:@"talkplus"] != nil) {
        NSLog(@"talkplus payload found, but do nothing");
    }
}

Last updated