您的位置:首页 > 移动开发 > IOS开发

iOS推送 (百度推送)

2015-06-05 17:59 316 查看
最近在使用推送,所以与大家分享一下我所遇到的问题,与解决问题的方法!!

1.首先生成CertificateSigningRequest文件。

点击钥匙串访问-->从证书颁发机构请求证书-->填写用户邮件地址-->常用名-->点击储存-->继续-->最后点击保存。

在桌面上就可以看见CertificateSigningRequest.certSigningRequest文件就是CSR文件,在我们生成CSR文件的同时,会在钥匙串访问中生成一对秘钥,名称为刚才我们填写的常用名。

2.打开开发者中心 首先创建Identifiers -->在创建Certificates -->在创建Provisoning Profiles

注意:1.创建Identifiers时,一定要勾选Push Notifications,

2.保证Identifiers中的ID,Certificates中的Name,Provisoning Profiles中的APP ID,应用程序中的Bundle identifier,保持一致。

3.点击钥匙串访问-->我的证书-->找到刚刚生成的.p12文件-->点击导出到桌面。

4.打开中端 -->openssl pkcs12 -in push.p12 -out push.pem -nodes,将.p12文件变成.pem文件。

5.添加到SDK到⼯工程中的步骤如下:


libBPush.a 和
BPush.h 添加到⾃自⼰己的⼯工程下,添加时需要注意勾选当前Target

6.创建并配置BPushConfig.plist文件,在工程中创建一个新的Property
List文件,并命名为BPushConfig.plist,添加以下键值:

{
“PRODUCTION_MODE” = NO
“API_KEY” = “uZbmgZKhfumvGYGowcjSPFc1”
“DEBUG” = NO
}


PRODUCTION_MODE:

必选。应用发布模式。开发证书签名时,值设为”NO”;发布证书签名时,值设为”YES”。请在调试和发布应用时,修改正确设置这个值,以免出现推送通知无法到达。

API_KEY:

必选。百度开发者中心为每个app自动分配的api key,在开发者中心app基本信息中可以查看。

6.SDK需要以下

库: Foundation.framework
、 CoreTelephony.framework
、 libz.dylib
、 SystemConfiguration.framework
,请在⼯工程中添加

7.在 AppDelegate
中的 application: didFinishLaunchingWithOptions:
中调⽤用 API,初始化Push:

因为iOS8中对于推送有更改,所以要判断设备的版本

[BPush setupChannel:launchOptions];
[BPush setDelegate:self]; //参数对象必须实现onMethod: response:方法,
#if SUPPORT_IOS8
//    8.0以后使用这种方法来注册推送通知
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}else
#endif
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}


8. 在application: didRegisterForRemoteNotificationsWithDeviceToken:中调用API,注册device token:

[BPush registerDeviceToken:deviceToken]; // 必须

[BPush bindChannel]; // 必须。可以在其它时机调用,只有在该方法返回(通过onMethod:response:回调)绑定成功时,app才能接收到Push消息。一个app绑定成功至少一次即可(如果access token变更请重新绑定)。


9. 实现BPushDelegate协议,必须实现方法onMethod:response::

if ([BPushRequestMethod_Bind isEqualToString:method])
{
NSDictionary* res = [[NSDictionary alloc] initWithDictionary:data];

NSString *appid = [res valueForKey:BPushRequestAppIdKey];
NSString *userid = [res valueForKey:BPushRequestUserIdKey];
NSString *channelid = [res valueForKey:BPushRequestChannelIdKey];
int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];
NSString *requestid = [res valueForKey:BPushRequestRequestIdKey];
}


10.在application: didReceiveRemoteNotification:中调用API,处理接收到的Push消息:

获取推送后返回的数据

[BPush handleNotification:userInfo]; // 可选


参考:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guideios
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: