【Cocos2d-x教程】iOS集成push


前言

使用推送消息,可提醒用户,召回玩家。延长游戏的寿命和收益!下面我们就一起来学习,cocos2d-x iOS集成push。

 

收到push消息:  

 

 

本文主要内容:

1. iOS Push基本理念

2. 创建工程

3. ios push相关配置

4. push集成

5. push 测试和使用

6. 总结

7. 相关资料

 

iOS Push基本理念

我们先来了解,iOS push实现的大概原理

从上图我们可以看到。

1. 首先是应用程序注册消息推送。

2. IOS跟APNS Server要deviceToken。应用程序接受deviceToken。

3. 应用程序将deviceToken发送给PUSH服务端程序。

4. 服务端程序向APNS服务发送消息。

5. APNS服务将消息发送给iPhone应用程序。

 

无论是iPhone客户端跟APNS,还是Provider和APNS都需要通过证书进行连接的

 

创建工程

工程基于 cocos2d-x-3.0rc2 版本, 针对ios平台.

 

cocos2d-x-3.0rc2 下载地址,如果已经有cocos2d-x 引擎可以跳过下载。解压文件进入该目录 创建工程

     
     
  1. $ cd cocos2d-x 
  2. $ ./setup.py 
  3. $ source FILE_TO_SAVE_SYSTEM_VARIABLE 
  4. $ cocos new pushDemo -p com.your_company.pushDemo -l cpp -d /home 
  5. $ cd /home/pushDemo 

 

push相关配置

登陆 iOS Dev Center 选择进入iOS Provisioning Portal。创建应用程序ID  

 

在 iOS Provisioning Portal中,点击App IDs进入App ID列表。 

 

创建 App ID,如果 ID 已经存在可以直接跳过此步骤 

 

为 App 开启 Push Notification 功能。如果是已经创建的 App ID 也可以通过设置开启 Push Notification 功能。

 

根据实际情况完善 App ID 信息并提交,注意此处需要指定具体的 Bundle ID 不要使用通配符。

 

push cer创建 develop 和 production 如果你之前没有创建过 Push 证书或者是要重新创建一个新的,请在证书列表下面新建。 

 

新建证书需要注意选择证书种类(开发证书用于开发和调试使用,生产证书用于 App Store 发布) 

 

点击 Continue 后选择证书对应的应用ID,然后继续会出现“About Creating a Certificate Signing Request (CSR)”。  

 

根据它的说明创建打开KeychainAccess 创建 Certificate Signing Request。  

 

填写“User Email Address”和“Common Name” 后选择 Saved to disk 进行保存 。

继续返回Apple developer 网站点击 Continue ,上传刚刚生成的 .certSigningRequest 文件生成 APNs Push Certificate。 下载并双击打开证书,证书打开时会启动“钥匙串访问”工具。 在“钥匙串访问”中你的证书会显示在“我的证书”中,注意选择“My Certificates” 和"login" 。 

 

导出 .p12 证书文件 在“钥匙串访问”中,选择刚刚加进来的证书,选择右键菜单中的“导出“...””。 注意要选“login”和“My Certificates” 导出证书时要选中证书文件,不要展开private key。  

 

将文件保存为Personal Information Exchange (.p12)格式。 保存p12文件时,可以为其设置密码,也可以让密码为空。

app Provisioning Profile 创建,选取app Id, Certificates,Devices. 分develop 和 production

 

push集成

新建pushHelper C++类,定义监听push回调相关的接口。

     
     
  1. class  pushHelper 
  2. {     
  3. public
  4.     /** returns a shared instance of the pushHelper 
  5.      *  @js getInstance 
  6.      */ 
  7.     static pushHelper* sharedPushHelper(void); 
  8.     /** 
  9.      @brief  The function be called when the application launching receive remote notification 
  10.      @param  notificationJson the pointer of the notification json string 
  11.      */ 
  12.     bool applicationDidFinishLaunchingWithNotification(const char* notificationJson); 
  13.     /** 
  14.      @brief  The function be called when the application register remote notification success 
  15.      @param  deviceToken the pointer of the notification token string 
  16.      */ 
  17.     void applicationDidRegisterForRemoteNotificationsWithDeviceToken(const char *deviceToken); 
  18.     /** 
  19.      @brief  The function be called when the application register remote notification failed 
  20.      @param  error the pointer of the register remote notification failed string 
  21.      */ 
  22.     void applicationdidFailToRegisterForRemoteNotificationsWithError(const char *error); 
  23.    /** 
  24.      @brief  The function be called when the application running receive remote notification 
  25.      @param  notificationJson the pointer of the notification json string 
  26.      */ 
  27.     void applicationDidReceiveRemoteNotification(const char* notificationJson); 
  28. }; 

 

在AppController.mm加入 ios remoteNotification 相关实现

1.在application: didFinishLaunchingWithOptions:注册push的种类,并处理启动时收到push

     
     
  1. //======================push======================== 
  2.  
  3.     [application registerForRemoteNotificationTypes: 
  4.      UIRemoteNotificationTypeAlert 
  5.      | UIRemoteNotificationTypeBadge 
  6.      | UIRemoteNotificationTypeSound]; 
  7.  
  8.     [application setApplicationIconBadgeNumber:0]; 
  9.  
  10.     //启动时收到push delay 5s派发 
  11.     NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
  12.     if(userInfo) { 
  13.         [application setApplicationIconBadgeNumber:0]; 
  14.         NSLog(@"LaunchOptionsRemoteNotification:%@",[userInfo description]); 
  15.  
  16.         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
  17.             pushHelper::sharedPushHelper()->applicationDidFinishLaunchingWithNotification([[userInfo description] cStringUsingEncoding:NSUTF8StringEncoding]); 
  18.         }); 
  19.     } 
  20.  
  21.     //======================push======================== 

 

2.在application: didRegisterForRemoteNotificationsWithDeviceToken:处理收到注册push成功返回的device token:

     
     
  1. NSLog(@"push deviceToken:%@",deviceToken); 
  2. pushHelper::sharedPushHelper()->applicationDidRegisterForRemoteNotificationsWithDeviceToken([[deviceToken description] cStringUsingEncoding:NSUTF8StringEncoding]); 

 

3.在application: didReceiveRemoteNotification: 处理app运行状态下接收到的Push消息: 

     
     
  1. NSLog(@"Receive Notify: %@", [userInfo description]); 
  2.  
  3.     [application setApplicationIconBadgeNumber:0]; 
  4.  
  5.     pushHelper::sharedPushHelper()->applicationDidReceiveRemoteNotification([[userInfo description] cStringUsingEncoding:NSUTF8StringEncoding]); 

 

4.在application: didFailToRegisterForRemoteNotificationsWithError:处理注册push失败

     
     
  1. pushHelper::sharedPushHelper()->applicationdidFailToRegisterForRemoteNotificationsWithError([[error description] cStringUsingEncoding:NSUTF8StringEncoding]); 

 

实现pushHelper.cpp

在cocos2dx 3.0的版本中 CCNotificationCenter 被废弃。我们 使用自定义事件进行推送通知派发。

     
     
  1. bool pushHelper::applicationDidFinishLaunchingWithNotification(const char* notificationJson) 
  2. { CCLOG("applicationDidFinishLaunchingWithNotification=%s",notificationJson); 
  3. dispatcherNotificationEvent(notificationJson, NOTIFICATION_EVENT); 
  4. return true
  5. void pushHelper::applicationDidRegisterForRemoteNotificationsWithDeviceToken(const char *deviceToken) 
  6. {   CCLOG("applicationDidRegisterForRemoteNotificationsWithDeviceToken=%s",deviceToken); 
  7. dispatcherNotificationEvent(deviceToken, REGISTER_NOTIFICATION_DEVICETOKEN_EVENT); 
  8. void pushHelper::applicationdidFailToRegisterForRemoteNotificationsWithError(const char *error) 
  9. {    CCLOG("FailToRegisterForRemoteNotificationsWithError=%s",error); 
  10. dispatcherNotificationEvent(error, REGISTER_NOTIFICATION_ERROR_EVENT); 
  11. void pushHelper::applicationDidReceiveRemoteNotification(const char* notificationJson) 
  12. {   CCLOG("applicationDidReceiveRemoteNotification=%s",notificationJson); 
  13. dispatcherNotificationEvent(notificationJson, NOTIFICATION_EVENT); 
  14. void pushHelper::dispatcherNotificationEvent(const char* data, const char* notificationEventType) 
  15.     auto director = Director::getInstance(); 
  16.     char* buf = new char[256]; 
  17.     sprintf(buf, "%s", data); 
  18.     EventCustom event(notificationEventType); 
  19.     event.setUserData(buf); 
  20.     director->getEventDispatcher()->dispatchEvent(&event); 
  21.     CC_SAFE_DELETE_ARRAY(buf); 

 

push测试

push 消息的推送 需要客户端和服务器的支持。自己搭建推送服务器(对于没有服务器编程经验的人)比较麻烦。现在国内主流的第三方push方案(百度云推送、极光推送、个推),都提供push集成客户端SDK 和 push消息推送控制台 或 消息推送服务SDK,推送结果统计。根据应用ID注册,消息推送条数 决定收费。开发者可以灵活选择使用。在此,本demo使用免费百度云推送方案,使用百度云推送的推送控制台测试。

 

集成百度云推送

1.上百度开发者注册工程 

 

2.参照文档完成客户端SDK的集成,加入baidu 云推送 依赖的framework

3.完善工程配置,develop / production选择,上传APNS证书 

 

4.使用百度云推送控制台推送push

 

push使用

在想使用推送通知消息的地方加入监听推送通知事件的代码:

在HelloWorldScene.cpp中监听remoteNotification

     
     
  1. //listen & handle push message 
  2.  
  3. void HelloWorld::onEnter() 
  4.  
  5.  
  6.     addNotificationListener(); 
  7.  
  8.  
  9. void HelloWorld::onExit() 
  10.  
  11.  
  12.     removeNotificationListener(); 
  13.  
  14.  
  15. void HelloWorld::addNotificationListener() 
  16.  
  17.  
  18.     notification_listener =  EventListenerCustom::create(NOTIFICATION_EVENT, [=](EventCustom* event){ 
  19.  
  20.         char* buf = static_cast<char*>(event->getUserData()); 
  21.  
  22.         CCLOG("Notification=%s",buf); 
  23.  
  24.     }); 
  25.  
  26.     _eventDispatcher->addEventListenerWithFixedPriority(notification_listener, 1); 
  27.  
  28.     register_notification_deviceToken_listener =  EventListenerCustom::create(REGISTER_NOTIFICATION_DEVICETOKEN_EVENT, [=](EventCustom* event){ 
  29.  
  30.         char* buf = static_cast<char*>(event->getUserData()); 
  31.  
  32.         CCLOG("register notification deviceToken=%s",buf); 
  33.  
  34.     }); 
  35.  
  36.     _eventDispatcher->addEventListenerWithFixedPriority(register_notification_deviceToken_listener, 1); 
  37.  
  38.     register_notification_error_listener =  EventListenerCustom::create(REGISTER_NOTIFICATION_ERROR_EVENT, [=](EventCustom* event){ 
  39.  
  40.         char* buf = static_cast<char*>(event->getUserData()); 
  41.  
  42.         CCLOG("register notification error=%s",buf); 
  43.  
  44.     }); 
  45.  
  46.     _eventDispatcher->addEventListenerWithFixedPriority(register_notification_error_listener, 1); 
  47.  
  48.  
  49. void HelloWorld::removeNotificationListener() 
  50.  
  51.  
  52.     _eventDispatcher->removeEventListener(notification_listener); 
  53.  
  54.     _eventDispatcher->removeEventListener(register_notification_deviceToken_listener); 
  55.  
  56.     _eventDispatcher->removeEventListener(register_notification_error_listener); 
  57.  

 

push运行结果

1.获取device Token成功 绑定百度云推送成功 

 

2.收到push消息 

 

3.系统通知栏显示 

 

 

后记

本demo 基于3.0 rc2版本实现,但是使用其他版本集成push功能与之类似,没有太大变化。 开发者可以灵活选择。 push 消息的处理 和 使用第三方推送SDK集成也可以灵活选择。

完整资源下载地址 

 

FAQ

1.push 不能获取device Token "未找到aps environment 的权利字串"。

可能原因:

没有配置push 证书,App id 没有配置push。

2.push 不能再 模拟器 和 已经越狱的机子上工作

3.收不到push 的可能原因:

1)根据devlop or production 模式选取 对应的APNS push证书配置和profile.

2)push 受网络因素影响,请检查网络连接是否畅通。

3)push存在一定的延时。

 

相关资料

1.iOS RemoteNotifications Programming Guide

2.百度云推送

3.极光push

4.个推

5.ios push证书设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值