iOS推送 (百度推送)

近期在使用推送,所以与大家分享一下我所遇到的问题,与解决这个问题的方法。!

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到⼯project中的过程例如以下:

   libBPush.a BPush.h 加入到⾃自⼰己的⼯project下,加入时须要注意勾选当前Target 


6.创建并配置BPushConfig.plist文件。在project中创建一个新的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 ,请在⼯project中加入 

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



posted @ 2017-06-28 16:07  brucemengbm  阅读(226)  评论(0)    收藏  举报