1. 创建应用
到阿里云移动推送控制台创建应用,应用创建完成后,进入移动推送相关模块进行设置,具体操作请参见
创建APP。
2. SDK下载和集成
2.1 手动集成
2.1.1 控制台下载
- [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]initCloudPush [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// SDK初始化
- [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]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]res[backcolor=transparent].[backcolor=transparent]success[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [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]]);
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [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]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}];
- [backcolor=transparent]}
- 向苹果APNs注册获取deviceToken并上报到阿里云推送服务器;
- [backcolor=transparent]/**
- [backcolor=transparent] * 注册苹果推送,获取deviceToken用于推送
- [backcolor=transparent] *
- [backcolor=transparent] * @param application
- [backcolor=transparent] */
- [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]{
- [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]{
- [backcolor=transparent] [backcolor=transparent]// iOS 8 Notifications
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]application registerUserNotificationSettings[backcolor=transparent]:
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UIUserNotificationSettings[backcolor=transparent] settingsForTypes[backcolor=transparent]:
- [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])
- [backcolor=transparent] categories[backcolor=transparent]:[backcolor=transparent]nil[backcolor=transparent]]];
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]application registerForRemoteNotifications[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// iOS < 8 Notifications
- [backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]UIApplication[backcolor=transparent] sharedApplication[backcolor=transparent]][backcolor=transparent] registerForRemoteNotificationTypes[backcolor=transparent]:
- [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])];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- [backcolor=transparent]/*
- [backcolor=transparent] * 苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器
- [backcolor=transparent] */
- [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]{
- [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]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]res[backcolor=transparent].[backcolor=transparent]success[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Register deviceToken success."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [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]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}];
- [backcolor=transparent]}
- [backcolor=transparent]/*
- [backcolor=transparent] * 苹果推送注册失败回调
- [backcolor=transparent] */
- [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]{
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"didFailToRegisterForRemoteNotificationsWithError %@"[backcolor=transparent],[backcolor=transparent] error[backcolor=transparent]);
- [backcolor=transparent]}
- [backcolor=transparent]/**
- [backcolor=transparent] * 注册推送消息到来监听
- [backcolor=transparent] */
- [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]registerMessageReceive [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]NSNotificationCenter[backcolor=transparent] defaultCenter[backcolor=transparent]][backcolor=transparent] addObserver[backcolor=transparent]:[backcolor=transparent]self
- [backcolor=transparent] selector[backcolor=transparent]:[backcolor=transparent]@selector[backcolor=transparent]([backcolor=transparent]onMessageReceived[backcolor=transparent]:)
- [backcolor=transparent] name[backcolor=transparent]:@[backcolor=transparent]"CCPDidReceiveMessageNotification"
- [backcolor=transparent] [backcolor=transparent]object[backcolor=transparent]:[backcolor=transparent]nil[backcolor=transparent]];
- [backcolor=transparent]}
- [backcolor=transparent]/**
- [backcolor=transparent] * 处理到来推送消息
- [backcolor=transparent] *
- [backcolor=transparent] * @param notification
- [backcolor=transparent] */
- [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]{
- [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]];
- [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]];
- [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]];
- [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]);
- [backcolor=transparent]}
- [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]{
- [backcolor=transparent] [backcolor=transparent]// 点击通知将App从关闭状态启动时,将通知打开回执上报
- [backcolor=transparent] [backcolor=transparent]// [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1)
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]CloudPushSDK[backcolor=transparent] sendNotificationAck[backcolor=transparent]:[backcolor=transparent]launchOptions[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] YES[backcolor=transparent];
- [backcolor=transparent]}
- [backcolor=transparent]/*
- [backcolor=transparent] * App处于启动状态时,通知打开回调
- [backcolor=transparent] */
- [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]{
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Receive one notification."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 取得APNS通知内容
- [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]];
- [backcolor=transparent] [backcolor=transparent]// 内容
- [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]];
- [backcolor=transparent] [backcolor=transparent]// badge数量
- [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]];
- [backcolor=transparent] [backcolor=transparent]// 播放声音
- [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]];
- [backcolor=transparent] [backcolor=transparent]// 取得Extras字段内容
- [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是自己定义的
- [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]);
- [backcolor=transparent] [backcolor=transparent]// iOS badge 清0
- [backcolor=transparent] application[backcolor=transparent].[backcolor=transparent]applicationIconBadgeNumber [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]// 通知打开回执上报
- [backcolor=transparent] [backcolor=transparent]// [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1)
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]CloudPushSDK[backcolor=transparent] sendNotificationAck[backcolor=transparent]:[backcolor=transparent]userInfo[backcolor=transparent]];
- [backcolor=transparent]}
如果使用推送模块,请参考
移动推送常见问题