APNs-百度推送使用总结(二)

三.代码配置
   
  1.我们要把百度云推送的SDK添加到项目当中,并添加相应的架包。


     2.在应用程序的AppDelegate文件里,配置推送

//推送相关的设置

    [BPush setupChannel:launchOptions];

    // 必须。参数对象必须实现(void)onMethod:(NSString*)method response:(NSDictionary*)data 方法,本示例中为 self

    [BPush setDelegate:self];

    

    //推送的通知事件

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];

    

    if (launchOptions) {

        

        //截取apns推送的消息

        NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];

        //[self setRemoteInfo:pushInfo];

        //[[NSNotificationCenter defaultCenter] postNotificationName:kMessageDidChangeNofication object:nil];

        

        //获取推送详情

        NSString *pushInfoStr = [NSString stringWithFormat:@"%@",[pushInfo  objectForKey:@"aps"]];

        NSLog(@"aps : %@",pushInfoStr);

    }


     3.接收和解析推送信息。 

#pragma mark - PNS Push Service


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    

    [application setApplicationIconBadgeNumber:0];

    //[self setRemoteInfo:userInfo];

    

    [BPush handleNotification:userInfo];

    NSLog(@"RemoteInfo : %@",userInfo);

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

#warning BPush - 自定义一个方法提交设备信息

    //[self registClientWithDeviceToken:deviceToken];

    [BPush registerDeviceToken: deviceToken];

    [BPush bindChannel];

}


- (void) onMethod:(NSString*)method response:(NSDictionary*)data {

    

#warning BPush - 接受后台推送的信息

        NSDictionary* res = [[NSDictionary alloc] initWithDictionary:data];

        if ([BPushRequestMethod_Bind isEqualToString:method]) {

            // NSString *appid = [res valueForKey:BPushRequestAppIdKey];

            NSString *userid = [res valueForKey:BPushRequestUserIdKey];

            NSString *channelid = [res valueForKey:BPushRequestChannelIdKey];

            //NSString *requestid = [res valueForKey:BPushRequestRequestIdKey];

            int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];

            self.bduid = userid;

            if (returnCode == BPushErrorCode_Success) {

                [[XZDataStore sharedObject] saveBDUid:userid];

                [[XZDataStore sharedObject] saveBDChannelId:channelid];

            }

        } else if ([BPushRequestMethod_Unbind isEqualToString:method]) {

            int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];

            if (returnCode == BPushErrorCode_Success) {

                [[XZDataStore sharedObject] saveBDUid:@""];

                [[XZDataStore sharedObject] saveBDChannelId:@""];

            }

        }

    

}

写到这里我们就可以使用通知了。我们这里主要讲的是远程通知,一般情况下根据具体的业务逻辑回合本地通知结合起来使用。

四.使用心得

1.遇到的问题 
     当百度推送和支付宝的SDK同时加入到项目里面的时候会出现遍以上的错误

     
原因是他们都引用了相同的第三方开源的类库所造成的,如Base64,JsonKit等。删除多余的保留一个就可以了。 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值