Klat
  • TalkPlus SDK
  • Android
    • Getting Started
    • Callback
    • User
      • Create User / Login
      • Update User Information
      • Block / Unblock
      • Logout
      • Delete User
    • Channel
      • Create / Delete Channel
      • View Channel
      • Updating Channel
      • Viewing Channel LIst
      • Manage Channel Members
      • Join / Leave Channel
      • Messaging
      • Hide / Show Channel
      • Freeze / Unfreeze Channel
      • Transfer Channel Ownership
      • Channel Push Notification Settings
      • Channel Member Data
    • Push Notification
    • Sample Application
    • What's New
  • iOS
    • Getting Started
    • Callback
    • User
      • Create / Login
      • Update User Information
      • Block / Unblock
      • Logout
      • Delete User
    • Channel
      • Create / Delete Channel
      • View Channel
      • Updating Channel
      • View Channel List
      • Manage Channel Members
      • Join / Leave Channel
      • Messaging
      • Hide / Show Channel
      • Freeze / Unfreeze Channel
      • Transfer Channel Ownership
      • Channel Push Notification Settings
      • Channel Member Data
    • Push Notification
    • Sample Application
    • What's New
  • Unity
    • Getting Started
    • Callback
    • User
      • Create User / Login
      • Update User Information
      • Block / Unblock
      • Logout
      • Delete User
    • Channel
      • Create / Delete Channel
      • View Channel
      • Updating Channel
      • View Channel List
      • Manage Channel Members
      • Join / Leave Channel
      • Messaging
      • Hide / Show Channel
      • Freeze / Unfreeze Channel
      • Transfer Channel Ownership
      • Channel Push Notification Settings
      • Channel Member Data
    • Push Notification
    • Sample Application
  • JavaScript
    • Getting Started
    • Realtime Event
    • Pagination
    • User
      • Create User / Login
      • Update User Information
      • Block / Unblock
      • Logout
      • Delete
    • Channel
      • Create / Delete Channel
      • View Channel
      • Updating Channel
      • View Channel List
      • Manage Channel Members
      • Join / Leave Channel
      • Messaging
      • Hide / Show Channel
      • Freeze / Unfreeze Channel
      • Transfer Channel Ownership
      • Channel Push Notification Settings
      • Channel Member Data
    • Push Notification (FCM)
    • Sample Application
    • What's New
  • Flutter
    • Getting Started
    • Callback
    • User
      • Create User / Login
      • Update User Information
      • Block / Unblock
      • Logout
      • Delete User
    • Channel
      • Create Channel
      • View Channel
      • Updating Channel
      • View Channel List
      • Manage Channel Members
      • Join / Leave Channel
      • Messaging
      • Hide / Show Channel
      • Freeze / Unfreeze Channel
      • Transfer Channel Ownership
      • Channel Push Notification Settings
      • Channel Member Data
    • Push Notification (FCM)
  • REST API
    • Getting Started
    • API
      • Users
        • Create User
        • Login (using login token)
        • View User
        • Update User
        • Activate / Deactivate
        • Enable / Disable Push Notification
        • Delete User
        • View Users
        • View Channels
        • Block / Unblock
      • Channel
        • Create Channel
        • View Channel
        • Update Channel
        • Delete Channel
        • View Channel List
        • Manage Channel Members
        • Messaging
        • Hide / Show Channel
        • Channel Freeze / Unfreeze
        • Transfer Channel Ownership
        • Channel Push Notification Settings
      • Bot
        • Create Bot
        • View Bot List
        • View Bot
        • Update Bot
        • Delete Bot
        • View Joined Channel List
        • Messaging
        • Join / Leave Channel
    • Push Notification
    • Rate Limit
  • MISC
    • Webhooks
    • SDK Rate Limit
    • Error Code
    • FAQ
      • Function
      • Spec
Powered by GitBook
On this page
  • Enable / Disable Push Notification
  • Push Notification
  1. iOS

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    
}];
// Enable Push Notification
TalkPlus.sharedInstance().enablePushNotification { tpUser in
    // SUCCESS
} failure: { (errorCode, error) in }
    // FAILURE    
}

// Disable Push Notification
TalkPlus.sharedInstance().disablePushNotification { tpUser in
    // SUCCESS
} failure: { (errorCode, error) in }
    // 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
}];
// Enable Push Notification from the channel
TalkPlus.sharedInstance().enableChannelPushNotification(channel) { channel in
    // SUCCESS
} failure: { (errorCode, error) in 
    // FAILURE  
}

// Disable Push Notification from the channel
TalkPlus.sharedInstance().disableChannelPushNotification(channel) { channel in
    // SUCCESS
} failure: { (errorCode, error) in
    // 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");
    }];
}
import Firebase
import FirebaseMessaging
import TalkPlus

// initialize Firebase
FirebaseApp.configure()

// request push notification authorization
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
    print(granted)
}
application.registerForRemoteNotifications()

Messaging.messaging().token { [weak self] token, error in
    guard let token = token else { return }
    if(error != nil) {
        print("Error getting FCM registration token: \(String(describing: error))")
        return
    }
    print("FCM registration token: \(token)");
    self?.registerFCMToken(fcmToken: token)
}

func registerFCMToken(fcmToken:String){
    TalkPlus.sharedInstance().registerFCMToken(fcmToken) {
        print("fcmToken register success")
    } failure: { errorCode, error in
        print("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");
    }
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    // called when push notification arrives while in foreground
    let userInfo = notification.request.content.userInfo
    if let payload = userInfo["talkplus"] as? String {
        print("talkplus payload")
        TalkPlus.sharedInstance().handleFCMMessage(payload)
    }
    if #available(iOS 14.0, *) {
        completionHandler([.sound, .badge, .banner])
    } else {
        // Fallback on earlier versions
        completionHandler([.sound, .badge])
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void) 
{
    // called when push notification is clicked while in background
    let userInfo = response.notification.request.content.userInfo
    if let _ = userInfo["talkplus"] as? String {
        print("talkplus payload found, but do nothing")
    }
}
PreviousChannel Member DataNextSample Application

Last updated 11 months ago