TalkPlus
Search
K

Push Notification

Enable / Disable Push Notification

사용자가 Push Notification을 받을 지 여부를 설정할 수 있습니다.
// Enable push notification
[[TalkPlus sharedInstance] enablePushNotification:^{
NSLog(@"enablePushNotification success");
} failure:^(int errorCode, NSError *error) {
NSLog(@"enablePushNotification failure");
}];
// Disable push notification
[[TalkPlus sharedInstance] disablePushNotification:^{
NSLog(@"enablePushNotification success");
} failure:^(int errorCode, NSError *error) {
NSLog(@"enablePushNotification failure");
}];

Push Notification

TalkPlus의 Push Notification은 FCM을 통하여 이루어지게 됩니다. 기본적인 FCM을 연동하신 후, User를 로그인한 후 registerFCMToken 함수를 호출하여 FCM 토큰을 User 세션에 연동하여야 합니다.
로그인이 완료된 후 아래와 같이 FIRMessaging을 통하여 token을 얻어 register 하시는 것을 권장 드립니다.
// Firebase Messaging을 통하여 현재 token을 획득
[[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");
}];
}
그 후 Push Notification이 도착할 경우 아래와 같이 TalkPlus에서 handleFCMMessage 함수를 통하여 payload를 처리하여 기존 등록된 delegate로 이벤트를 전달 드립니다.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if([userInfo objectForKey:@"talkplus"] != nil) {
NSString payload = [userInfo objectForKey:@"talkplus"];
// channelId, title, body 정보가 필요한 경우 아래와 같이 사용합니다.
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:[payload dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSString * channelId = [dict objectForKey:@"channelId"];
NSString * messageId = [dict objectForKey:@"messageId"]; // available only for message event
NSString * title = [dict objectForKey:@"title"];
NSString * body = [dict objectForKey:@"body"];
[[TalkPlus sharedInstance] handleFCMMessage:payload];
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([userInfo objectForKey:@"talkplus"] != nil) {
NSString payload = [userInfo objectForKey:@"talkplus"];
// channelId 정보가 필요한 경우 아래와 같이 사용합니다.
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:[payload dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSString * channelId = [dict objectForKey:@"channelId"];
[[TalkPlus sharedInstance] handleFCMMessage:payload];
}
completionHandler(UIBackgroundFetchResultNewData);
}