前言
使用推送消息,可提醒用户,召回玩家。延长游戏的寿命和收益!下面我们就一起来学习,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 引擎可以跳过下载。解压文件进入该目录 创建工程
- $cdcocos2d-x
- $./setup.py
- $sourceFILE_TO_SAVE_SYSTEM_VARIABLE
- $cocosnewpushDemo-pcom.your_company.pushDemo-lcpp-d/home
- $cd/home/pushDemo
push相关配置
登陆iOS Dev Center选择进入iOS Provisioning Portal。创建应用程序ID
创建 App ID,如果 ID 已经存在可以直接跳过此步骤
根据实际情况完善 App ID 信息并提交,注意此处需要指定具体的 Bundle ID 不要使用通配符。
新建证书需要注意选择证书种类(开发证书用于开发和调试使用,生产证书用于 App Store 发布)
app Provisioning Profile 创建,选取app Id,Certificates,Devices. 分develop 和 production
新建pushHelper C++类,定义监听push回调相关的接口。
- classpushHelper
- {
- public:
- /**returnsasharedinstanceofthepushHelper
- *@jsgetInstance
- */
- staticpushHelper*sharedPushHelper(void);
- /**
- @briefThefunctionbecalledwhentheapplicationlaunchingreceiveremotenotification
- @paramnotificationJsonthepointerofthenotificationjsonstring
- */
- boolapplicationDidFinishLaunchingWithNotification(constchar*notificationJson);
- /**
- @briefThefunctionbecalledwhentheapplicationregisterremotenotificationsuccess
- @paramdeviceTokenthepointerofthenotificationtokenstring
- voidapplicationDidRegisterForRemoteNotificationsWithDeviceToken(char*deviceToken);
- @briefThefunctionbecalledwhentheapplicationregisterremotenotificationFailed
- @paramerrorthepointeroftheregisterremotenotificationFailedstring
- */
- voidapplicationdidFailToRegisterForRemoteNotificationsWithError(char*error);
- @briefThefunctionbecalledwhentheapplicationrunningreceiveremotenotification
- @paramnotificationJsonthepointerofthenotificationjsonstring
- voidapplicationDidReceiveRemoteNotification(char*notificationJson);
- };
在AppController.mm加入 ios remoteNotification 相关实现
1.在application: didFinishLaunchingWithOptions:注册push的种类,并处理启动时收到push
- //======================push========================
- [applicationregisterForRemoteNotificationTypes:
- UIRemoteNotificationTypeAlert
- |UIRemoteNotificationTypeBadge
- |UIRemoteNotificationTypeSound];
- [applicationsetApplicationIconBadgeNumber:0];
- //启动时收到pushdelay5s派发
- NSDictionary*userInfo=[launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- if(userInfo){
- [applicationsetApplicationIconBadgeNumber:0];
- NSLog(@"LaunchOptionsRemoteNotification:%@",[userInfodescription]);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(5*NSEC_PER_SEC)),dispatch_get_main_queue(),^{
- pushHelper::sharedPushHelper()->applicationDidFinishLaunchingWithNotification([[userInfodescription]cStringUsingEncoding:NSUTF8StringEncoding]);
- });
- }
- //======================push========================
2.在application: didRegisterForRemoteNotificationsWithDeviceToken:处理收到注册push成功返回的device token:
- NSLog(@"pushdeviceToken:%@",deviceToken);
- pushHelper::sharedPushHelper()->applicationDidRegisterForRemoteNotificationsWithDeviceToken([[deviceTokendescription]cStringUsingEncoding:NSUTF8StringEncoding]);
3.在application: didReceiveRemoteNotification: 处理app运行状态下接收到的Push消息:
- NSLog(@"ReceiveNotify:%@",[userInfodescription]);
- [applicationsetApplicationIconBadgeNumber:0];
- pushHelper::sharedPushHelper()->applicationDidReceiveRemoteNotification([[userInfodescription]cStringUsingEncoding:NSUTF8StringEncoding]);
4.在application: didFailToRegisterForRemoteNotificationsWithError:处理注册push失败
- pushHelper::sharedPushHelper()->applicationdidFailToRegisterForRemoteNotificationsWithError([[errordescription]cStringUsingEncoding:NSUTF8StringEncoding]);
实现pushHelper.cpp
在cocos2dx 3.0的版本中 CCNotificationCenter 被废弃。我们 使用自定义事件进行推送通知派发。
- boolpushHelper::applicationDidFinishLaunchingWithNotification(char*notificationJson)
- {CCLOG("applicationDidFinishLaunchingWithNotification=%s",notificationJson);
- dispatcherNotificationEvent(notificationJson,NOTIFICATION_EVENT);
- returntrue;
- }
- voidpushHelper::applicationDidRegisterForRemoteNotificationsWithDeviceToken(char*deviceToken)
- {CCLOG("applicationDidRegisterForRemoteNotificationsWithDeviceToken=%s",deviceToken);
- dispatcherNotificationEvent(deviceToken,REGISTER_NOTIFICATION_DEVICETOKEN_EVENT);
- }
- voidpushHelper::applicationdidFailToRegisterForRemoteNotificationsWithError(char*error)
- {CCLOG("FailToRegisterForRemoteNotificationsWithError=%s",error);
- dispatcherNotificationEvent(error,REGISTER_NOTIFICATION_ERROR_EVENT);
- }
- voidpushHelper::applicationDidReceiveRemoteNotification(char*notificationJson)
- {CCLOG("applicationDidReceiveRemoteNotification=%s",notificationJson);
- dispatcherNotificationEvent(notificationJson,NOTIFICATION_EVENT);
- }
- voidpushHelper::dispatcherNotificationEvent(char*data,char*notificationEventType)
- {
- autodirector=Director::getInstance();
- char*buf=newchar[256];
- sprintf(buf,"%s",data);
- EventCustomevent(notificationEventType);
- event.setUserData(buf);
- director->getEventDispatcher()->dispatchEvent(&event);
- CC_SAFE_DELETE_ARRAY(buf);
- }
push测试
push 消息的推送 需要客户端和服务器的支持。自己搭建推送服务器(对于没有服务器编程经验的人)比较麻烦。现在国内主流的第三方push方案(百度云推送、极光推送、个推),都提供push集成客户端SDK 和 push消息推送控制台 或 消息推送服务SDK,推送结果统计。根据应用ID注册,消息推送条数 决定收费。开发者可以灵活选择使用。在此,本demo使用免费百度云推送方案,使用百度云推送的推送控制台测试。
集成百度云推送
3.完善工程配置,develop / production选择,上传APNS证书
在HelloWorldScene.cpp中监听remoteNotification
- //listen&handlepushmessage
- voidHelloWorld::onEnter()
- {
- addNotificationListener();
- }
- voidHelloWorld::onExit()
- {
- removeNotificationListener();
- }
- voidHelloWorld::addNotificationListener()
- {
- notification_listener=EventListenerCustom::create(NOTIFICATION_EVENT,[=](EventCustom*event){
- char*buf=static_cast<char*>(event->getUserData());
- CCLOG("Notification=%s",buf);
- });
- _eventDispatcher->addEventListenerWithFixedPriority(notification_listener,1);
- register_notification_deviceToken_listener=EventListenerCustom::create(REGISTER_NOTIFICATION_DEVICETOKEN_EVENT,153); background-color:inherit; font-weight:bold">char*>(event->getUserData());
- CCLOG("registernotificationdeviceToken=%s",buf);
- });
- _eventDispatcher->addEventListenerWithFixedPriority(register_notification_deviceToken_listener,1);
- register_notification_error_listener=EventListenerCustom::create(REGISTER_NOTIFICATION_ERROR_EVENT,153); background-color:inherit; font-weight:bold">char*>(event->getUserData());
- CCLOG("registernotificationerror=%s",buf);
- });
- _eventDispatcher->addEventListenerWithFixedPriority(register_notification_error_listener,1);
- }
- voidHelloWorld::removeNotificationListener()
- {
- _eventDispatcher->removeEventListener(notification_listener);
- _eventDispatcher->removeEventListener(register_notification_deviceToken_listener);
- _eventDispatcher->removeEventListener(register_notification_error_listener);
- }
push运行结果