监听推送消息通道建立
通知中心注册事件名为CCPDidChannelConnectedSuccess
的广播监听;
推送通道成功建立后,发出事件名为CCPDidChannelConnectedSuccess
的广播通知。
代码示例
- (void)listenerOnChannelOpened {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onChannelOpened:)
name:@"CCPDidChannelConnectedSuccess"
object:nil];
}
// 通道打开通知
- (void)onChannelOpened:(NSNotification *)notification {
}
消息接收监听
通知中心注册事件名为CCPDidReceiveMessageNotification
的广播监听;
成功接收消息后,发出事件名为CCPDidReceiveMessageNotification
的广播通知。
代码示例
- (void) registerMessageReceive {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onMessageReceived:)
name:@"CCPDidReceiveMessageNotification"
object:nil];
}
- (void)onMessageReceived:(NSNotification *)notification {
CCPSysMessage *message = [notification object];
NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
NSLog(@"Receive message title: %@, content: %@.", title, body);
}
通知打开监听
iOS 10+
App处于前台收到通知
/** * App处于前台时收到通知(iOS 10+) */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSLog(@"Receive a notification in foregound."); // 处理iOS 10通知相关字段信息 [self handleiOS10Notification:notification]; // 通知不弹出 //completionHandler(UNNotificationPresentationOptionNone); // 通知弹出,且带有声音、内容和角标(App处于前台时不建议弹出通知) completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge); }
触发通知动作时回调,比如点击、删除通知和点击自定义action(iOS 10+)。
/** * 触发通知动作时回调,比如点击、删除通知和点击自定义action(iOS 10+) */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSString *userAction = response.actionIdentifier; // 点击通知打开 if ([userAction isEqualToString:UNNotificationDefaultActionIdentifier]) { NSLog(@"User opened the notification."); // 处理iOS 10通知,并上报通知打开回执 [self handleiOS10Notification:response.notification]; } // 通知dismiss,category创建时传入UNNotificationCategoryOptionCustomDismissAction才可以触发 if ([userAction isEqualToString:UNNotificationDismissActionIdentifier]) { NSLog(@"User dismissed the notification."); } NSString *customAction1 = @"action1"; NSString *customAction2 = @"action2"; // 点击用户自定义Action1 if ([userAction isEqualToString:customAction1]) { NSLog(@"User custom action1."); } // 点击用户自定义Action2 if ([userAction isEqualToString:customAction2]) { NSLog(@"User custom action2."); } completionHandler(); } /** * 处理iOS 10通知(iOS 10+) */ - (void)handleiOS10Notification:(UNNotification *)notification { UNNotificationRequest *request = notification.request; UNNotificationContent *content = request.content; NSDictionary *userInfo = content.userInfo; // 通知时间 NSDate *noticeDate = notification.date; // 标题 NSString *title = content.title; // 副标题 NSString *subtitle = content.subtitle; // 内容 NSString *body = content.body; // 角标 int badge = [content.badge intValue]; // 取得通知自定义字段内容,例:获取key为"Extras"的内容 NSString *extras = [userInfo valueForKey:@"Extras"]; // 通知打开回执上报 [CloudPushSDK sendNotificationAck:userInfo]; NSLog(@"Notification, date: %@, title: %@, subtitle: %@, body: %@, badge: %d, extras: %@.", noticeDate, title, subtitle, body, badge, extras); }
iOS 10以下版本
App处于关闭状态,点击打开通知;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 点击通知将App从关闭状态启动时,将通知打开回执上报 // [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1) [CloudPushSDK sendNotificationAck:launchOptions]; return YES; }
App处于打开状态时,点击打开通知;
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { NSLog(@"Receive one notification."); // 取得APNS通知内容 NSDictionary *aps = [userInfo valueForKey:@"aps"]; // 内容 NSString *content = [aps valueForKey:@"alert"]; // badge数量 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; // 播放声音 NSString *sound = [aps valueForKey:@"sound"]; // 取得Extras字段内容 NSString *Extras = [userInfo valueForKey:@"Extras"]; //服务端中Extras字段,key是自己定义的 NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras); // iOS badge 清0 application.applicationIconBadgeNumber = 0; // 通知打开回执上报 // [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1) [CloudPushSDK sendNotificationAck:userInfo]; }
文档内容是否对您有帮助?