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")
}
}
Last updated