开发者社区> 问答> 正文

iOS SDK配置的使用方法有哪些?



1. 创建应用


到阿里云移动推送控制台创建应用,应用创建完成后,进入移动推送相关模块进行设置,具体操作请参见 创建APP



2. SDK下载和集成



2.1 手动集成



2.1.1 控制台下载

  • 在移动推送控制台进行SDK下载;

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

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

展开
收起
猫饭先生 2017-10-23 11:05:44 1420 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
聚划算iOS平台动态化方案LuaView 立即下载
滴滴出行iOS端瘦身实践 立即下载
一个跨平台的云服务SDK需要什么 立即下载