开发者社区> 问答> 正文

移动推送3. Push SDK使用

已解决

展开
收起
2018-04-03 00:05:08 1383 0
1 条回答
写回答
取消 提交回答
  • 采纳回答

    详细解答可以参考官方帮助文档

    • 请参照以下代码完成SDK的初始化,appKey/appSecret的获取参考创建App
    • 注意:百川云推送迁移来的用户,不要使用百川平台获取到的appKey/appSecret,参考上述链接从阿里云平台获取,否则会发生鉴权错误。
    • > iOS 10系统的设备需注意,源于工信部的要求,国行手机首次安装App时,会弹出一个询问用户“是否允许应用访问数据”的弹框,如下图所示。在用户点击允许前,或点击不允许,App的网络环境是不通的,会导致推送SDK的初始化失败,推送服务不能正常使用。推送各接口内部是有重试机制的,但是,建议用户在业务层,也要捕获处理SDK接口错误回调,确保正确获知SDK接口调用状态。

    ios-network-alert

    1. - (void)initCloudPush {
    2. // SDK初始化
    3. [CloudPushSDK asyncInit:@"*****" appSecret:@"*****" callback:^(CloudPushCallbackResult *res) {
    4. if (res.success) {
    5. NSLog(@"Push SDK init success, deviceId: %@.", [CloudPushSDK getDeviceId]);
    6. } else {
    7. NSLog(@"Push SDK init failed, error: %@", res.error);
    8. }
    9. }];
    10. }
    • 向苹果APNs注册获取deviceToken并上报到阿里云推送服务器;
    1. /**
    2. * 注册苹果推送,获取deviceToken用于推送
    3. *
    4. * @param application
    5. */
    6. - (void)registerAPNS:(UIApplication *)application {
    7. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    8. // iOS 8 Notifications
    9. [application registerUserNotificationSettings:
    10. [UIUserNotificationSettings settingsForTypes:
    11. (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
    12. categories:nil]];
    13. [application registerForRemoteNotifications];
    14. }
    15. else {
    16. // iOS < 8 Notifications
    17. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
    18. (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    19. }
    20. }
    21. /*
    22. * 苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器
    23. */
    24. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    25. [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
    26. if (res.success) {
    27. NSLog(@"Register deviceToken success.");
    28. } else {
    29. NSLog(@"Register deviceToken failed, error: %@", res.error);
    30. }
    31. }];
    32. }
    33. /*
    34. * 苹果推送注册失败回调
    35. */
    36. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    37. NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
    38. }
    • 推送消息到来监听;
    1. /**
    2. * 注册推送消息到来监听
    3. */
    4. - (void)registerMessageReceive {
    5. [[NSNotificationCenter defaultCenter] addObserver:self
    6. selector:@selector(onMessageReceived:)
    7. name:@"CCPDidReceiveMessageNotification"
    8. object:nil];
    9. }
    10. /**
    11. * 处理到来推送消息
    12. *
    13. * @param notification
    14. */
    15. - (void)onMessageReceived:(NSNotification *)notification {
    16. CCPSysMessage *message = [notification object];
    17. NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
    18. NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
    19. NSLog(@"Receive message title: %@, content: %@.", title, body);
    20. }
    • 通知打开监听:
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    2. // 点击通知将App从关闭状态启动时,将通知打开回执上报
    3. // [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1)
    4. [CloudPushSDK sendNotificationAck:launchOptions];
    5. return YES;
    6. }
    7. /*
    8. * App处于启动状态时,通知打开回调
    9. */
    10. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
    11. NSLog(@"Receive one notification.");
    12. // 取得APNS通知内容
    13. NSDictionary *aps = [userInfo valueForKey:@"aps"];
    14. // 内容
    15. NSString *content = [aps valueForKey:@"alert"];
    16. // badge数量
    17. NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
    18. // 播放声音
    19. NSString *sound = [aps valueForKey:@"sound"];
    20. // 取得Extras字段内容
    21. NSString *Extras = [userInfo valueForKey:@"Extras"]; //服务端中Extras字段,key是自己定义的
    22. NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras);
    23. // iOS badge 清0
    24. application.applicationIconBadgeNumber = 0;
    25. // 通知打开回执上报
    26. // [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1)
    27. [CloudPushSDK sendNotificationAck:userInfo];
    28. }

    如果使用推送模块,请参考移动推送常见问题

    2018-04-08 22:55:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载