【教程分享】Cocos2d-x iOS集成push

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

收到push消息:

d475dbaddda01e31077e0d8a6ff75e04.png

1e6031d495bcfee2a23c504c24d2c4e0.png

本文主要内容:

·iOS Push基本理念

·创建工程

·ios push相关配置

·push集成

·push 测试和使用

·总结

·相关资料

iOS Push基本理念

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

89169b192fff9b85f3010681051057ed.jpeg

从上图我们可以看到:

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

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

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

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

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

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

创建工程

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

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

97479f2490ff2d8901380e01761c2997.png

push相关配置

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

6dbfc079ccf67eeb8f77ff808511d402.jpeg

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

0eccd344c58fbebf88029a531580a3b5.png

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

0cb5e1cfcbbd21a1d39b2e1519af5ad9.jpeg

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

208477f0766a180032628e666c4787f5.png

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

161e49c664814086ce4a7bd126e841dc.png

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

e256ecb18d95c72af72e599c6b4e10f8.png

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

c5a3764bbc36fdfaca4ce550bfee98ce.jpeg

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

3194b6f71a20a1314abed1ee552eb412.png

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

dd7bf4d50dba078c5de96e95118e139c.png

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

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

a17fdebc341a0e5bf4188afc8b5a2da0.jpeg

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

6ac4aaa0bab921a0bcf3f2740387d9bd.png

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

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

13144d68017dc3ce16164755c2b03d5a.jpeg

push集成

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

d7a131acaff6bbf3d6f333f1cc96476e.jpeg

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

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

//======================push========================

[application registerForRemoteNotificationTypes:

UIRemoteNotificationTypeAlert

| UIRemoteNotificationTypeBadge

| UIRemoteNotificationTypeSound];

[application setApplicationIconBadgeNumber:0];

//启动时收到push delay 5s派发

NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(userInfo) {

[application setApplicationIconBadgeNumber:0];

NSLog(@"LaunchOptionsRemoteNotification:%@",[userInfo description]);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

pushHelper::sharedPushHelper()->applicationDidFinishLaunchingWithNotification([[userInfo description] cStringUsingEncoding:NSUTF8StringEncoding]);

});

}

//======================push========================

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

NSLog(@"push deviceToken:%@",deviceToken);

pushHelper::sharedPushHelper()->applicationDidRegisterForRemoteNotificationsWithDeviceToken([[deviceToken description] cStringUsingEncoding:NSUTF8StringEncoding]);

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

NSLog(@"Receive Notify: %@", [userInfo description]);

[application setApplicationIconBadgeNumber:0];

pushHelper::sharedPushHelper()->applicationDidReceiveRemoteNotification([[userInfo description] cStringUsingEncoding:NSUTF8StringEncoding]);

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

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

实现pushHelper.cpp

cb090c2164cde5d8a14914d2a947409a.png

05b7a7295f96774d26f7033fc5f4c02d.png

push测试

push 消息的推送 需要客户端和服务器的支持。

自己搭建推送服务器(对于没有服务器编程经验的人)比较麻烦。

现在国内主流的第三方push方案(百度云推送、极光推送、个推),都提供push集成客户端SDK 和 push消息推送控制台 或 消息推送服务SDK,推送结果统计。根据应用ID注册,消息推送条数 决定收费。开发者可以灵活选择使用。

在此,本demo使用免费百度云推送方案,使用百度云推送的推送控制台测试。

·集成百度云推送

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

e8721f5b0d2ceda791338c251c9fefe9.jpeg

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

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

c4bba9bff2d73ae688e10c47e3464023.jpeg

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

1598b8edd978b077112862e65eaaf0a0.jpeg

push使用

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

·在HelloWorldScene.cpp中监听remoteNotification

//listen & handle push message

void HelloWorld::onEnter()

{

addNotificationListener();

}

void HelloWorld::onExit()

{

removeNotificationListener();

}

void HelloWorld::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, [=](EventCustom* event){

char* buf = static_cast<char*>(event->getUserData());

CCLOG("register notification deviceToken=%s",buf);

});

_eventDispatcher->addEventListenerWithFixedPriority(register_notification_deviceToken_listener, 1);

register_notification_error_listener = EventListenerCustom::create(REGISTER_NOTIFICATION_ERROR_EVENT, [=](EventCustom* event){

char* buf = static_cast<char*>(event->getUserData());

CCLOG("register notification error=%s",buf);

});

_eventDispatcher->addEventListenerWithFixedPriority(register_notification_error_listener, 1);

}

void HelloWorld::removeNotificationListener()

{

_eventDispatcher->removeEventListener(notification_listener);

_eventDispatcher->removeEventListener(register_notification_deviceToken_listener);

_eventDispatcher->removeEventListener(register_notification_error_listener);

}

push运行结果

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

3f259826ab9fd81f4353a30a12448c7b.png

ii. 收到push消息

555aa1835cf54c331cad189c53222cc2.png

iii. 系统通知栏显示

ceec8c895635f9b890e689e6fdc69d01.png

8890d94ad0eff36a9768062a6a0fb216.png

后记

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

FAQ

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

可能原因:

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

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

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

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

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

push存在一定的延时

原文与相关资料请点击最下方“阅读原文”

04e6848aee6ef4a8aa239d26a4fe2518.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值