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

来源:互联网 发布:truncate删除部分数据 编辑:程序博客网 时间:2024/05/16 01:55
三.代码配置
   
  1.我们要把百度云推送的SDK添加到项目当中,并添加相应的架包。


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

//推送相关的设置

    [BPushsetupChannel:launchOptions];

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

    [BPushsetDelegate:self];

    

    //推送的通知事件

    [[UIApplicationsharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];

    

    if (launchOptions) {

        

        //截取apns推送的消息

        NSDictionary* pushInfo = [launchOptionsobjectForKey:@"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];

    

    [BPushhandleNotification:userInfo];

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

}

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

{

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

    //[self registClientWithDeviceToken:deviceToken];

    [BPushregisterDeviceToken: deviceToken];

    [BPushbindChannel];

}


- (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) {

                [[XZDataStoresharedObject] saveBDUid:userid];

                [[XZDataStoresharedObject] saveBDChannelId:channelid];

            }

        } else if ([BPushRequestMethod_UnbindisEqualToString:method]) {

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

            if (returnCode == BPushErrorCode_Success) {

                [[XZDataStoresharedObject] saveBDUid:@""];

                [[XZDataStoresharedObject] saveBDChannelId:@""];

            }

        }

    

}

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

四.使用心得

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

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



0 0
原创粉丝点击