移动客户端谈入手百度推送经验

百度推送:(先读百度提供的文档,按照要求一步一步做,基本上都能完成,per证书需要苹果开发者账号,百度一搜ios推送一堆介绍per证书生成流程的,就不多解释了)

1、按照百度文档的要求在百度开发者服务中创建应用(工程)


2、点击获取SDK(推送为必选)


3、首先在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中创建frontia对象,并加入push任务,如下所示


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    

    //初始化Frontia

    [Frontia initWithApiKey:APP_KEY];

    

    _push = [Frontia getPush];

    [FrontiaPush setupChannel:launchOptions];

    

    [application registerForRemoteNotificationTypes:

     UIRemoteNotificationTypeAlert

     | UIRemoteNotificationTypeBadge

     | UIRemoteNotificationTypeSound];

    // Override point for customization after application launch.

    return YES;

}





4、在用户接受推送通知后系统会调用

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 方法,在此方法内写入以下代码(设置tag可以不写)

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

    

//    NSLog(@"frontia application:%@", deviceToken);

    [FrontiaPush registerDeviceToken: deviceToken];


    //绑定

    [_push bindChannel:^(NSString *appId,NSString *userId,NSString *channelId){

        NSLog(@"appid = %@,userid = %@,channelId = %@,",appId,userId,channelId);

    } failureResult:^(NSString *action,int errorCode,NSString *errorMessage){

        NSLog(@"errorcode = %d,errorMessage = %@",errorCode,errorMessage);

    }];

    

    //为此app设置tag值,分组通知时使用,如不使用分组可写

    [_push setTag:@"1" tagOpResult:^(int count, NSArray *failureTag){

    

         NSString *message = [[NSString alloc] initWithFormat:@"set tag success result: %d with failure tags %@", count, failureTag];

        NSLog(@"%@",message);

    } failureResult:^(NSString *action,int errorCode, NSString *errorMessage){

        NSString *message = [[NSString alloc] initWithFormat:@"set tag failed with %@ error code : %d error message %@", action, errorCode, errorMessage];

        NSLog(@"%@",message);

    }];

}


5、- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 此方法为系统调用推送失败的方法,内容可写可不写

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"frontia application:%@", error);

}


6、当用户接到推送后点击时,会调用

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo方法,在此方法内写上百度push调用的方法和自己需要进行的操作


- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    //app需要做的操作

//    NSLog(@"frontia applciation receive Notify: %@", [userInfo description]);

//    NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];

//    if (application.applicationState == UIApplicationStateActive) {

//        // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.

//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification"

//                                                            message:[NSString stringWithFormat:@"The application received this remote notification while it was running:\n%@", alert]

//                                                           delegate:self

//                                                  cancelButtonTitle:@"OK"

//                                                  otherButtonTitles:nil];

//        [alertView show];

//    }

//    [application setApplicationIconBadgeNumber:0];

    

    //百度推送需要调用的

    [FrontiaPush handleNotification:userInfo];

    

}


7、到此就可以在百度后台推送中发送通知了



8、另附百度另一个单独的推送库(BPush)代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    

    [BPush setupChannel:launchOptions];

    [BPush setDelegate:self];


    [application setApplicationIconBadgeNumber:0];

    [application registerForRemoteNotificationTypes:

     UIRemoteNotificationTypeAlert

     | UIRemoteNotificationTypeBadge

     | UIRemoteNotificationTypeSound];

    

    self.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:[Controllers firstViewController]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    return YES;

    

    

}


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

    

    NSLog(@"frontia application:%@", deviceToken);

    [BPush registerDeviceToken:deviceToken];

    [BPush bindChannel];

    

}



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

    NSLog(@"On method:%@", method);

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

    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];

        NSLog(@"appid:%@/userid:%@/nchannelid:%@requestid:%@returncode:%d",appid,userid,channelid,requestid,returnCode);

    }

}


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


    [application setApplicationIconBadgeNumber:0];

    

    [BPush handleNotification:userInfo];

}



- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"frontia application:%@", error);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值