iOS之百度云推送二

在iOS开发中我们开发中往往离不开远程推送,因为他能够有提醒客户自己的存在,且能够通过消息提醒来获取用户的回头率。

百度推送:

1.框架设计:

    Apple 的推送服务器 APNS (Apple Push Notification Service) 发送需要推送的消息,APNS 在收到消息后,会将消息发到设备上。以上整个过程较为复杂,而且功能比较单一,在集成百度的Push SDK 后,可以越过这些复杂的操作,使用百度 Push SDK 提供的 API接口,可以更加简 便、简捷的在 App 中使用 Push 功能。由于苹果 iOS 系统限制,推送到搭载 iOS 系统设备上的消息都需要经过 APNS 再下发到设备,百度 Push 服务相当于开发者与 APNS 之间的桥梁,帮助 开发者完成 Push 服务。具体如下图所示: 

iOS之百度云推送二_第1张图片

操作步骤:

1.下载百度云推送SDK,并将libBPush导入工程文件夹。

配置:

iOS之百度云推送二_第2张图片

iOS之百度云推送二_第3张图片

2.配置证书文件:

创建为开发环境的的证书和为推送环境生产的证书。下载后导出生成p12证书。

使用终端命令

      key.p12文件转key.pem文件

            openssl pkcs12 -nocerts -out key.pem -inkey.p12

3.将pem文件导入百度云推送后台得到百度云后台生成的信息

iOS之百度云推送二_第4张图片

3.在AppDelegate导入工程文件:

#import "BPush.h"

 代码:

复制代码
//BPush
-(void)addBDPushWithBaby:(NSDictionary *)launchOptions application:(UIApplication *)application
{
    // iOS8 下需要使用新的 API
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    else
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }
    
# pragma warning 测试 开发环境 时需要修改BPushMode为BPushModeDevelopment 需要修改Apikey为自己的Apikey
    // 在 App 启动时注册百度云推送服务,需要提供 Apikey

 // 在 App 启动时注册百度云推送服务,需要提供 Apikey

    [BPush registerChannel:launchOptions apiKey:@"在百度云推送官网上注册后得到的apikey" pushMode:BPushModeProduction withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];

// App 是用户点击推送消息启动F
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo)
    {
        NSLog(@"从消息启动:%@",userInfo);
        [BPush handleNotification:userInfo];
    }
#if TARGET_IPHONE_SIMULATOR
    Byte dt[32] = {0xc6, 0x1e, 0x5a, 0x13, 0x2d, 0x04, 0x83, 0x82, 0x12, 0x4c, 0x26, 0xcd, 0x0c, 0x16, 0xf6, 0x7c, 0x74, 0x78, 0xb3, 0x5f, 0x6b, 0x37, 0x0a, 0x42, 0x4f, 0xe7, 0x97, 0xdc, 0x9f, 0x3a, 0x54, 0x10};
    [self application:application didRegisterForRemoteNotificationsWithDeviceToken:[NSData dataWithBytes:dt length:32]];
#endif
    //角标清0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
}

// 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    completionHandler(UIBackgroundFetchResultNewData);
    //存入消息
    [CommonUDManager setMessageData:@"1"];
    
    //通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BDPush" object:self];
    
    // 打印到日志 textView 中
    NSLog(@"********** iOS7.0之后 background **********");
    // 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
    if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
        NSLog(@"acitve or background");
        UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    else//杀死状态下,直接跳转到跳转页面。
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *mainNav = (UINavigationController *)[sb instantiateViewControllerWithIdentifier:SBID_MyCenterControllerNavID];
        self.window.rootViewController = mainNav;
        //消息详情
        id controller = [[NSClassFromString(@"MessageDetialViewController") alloc] init];
        [mainNav pushViewController:controller animated:YES];
    }
}
// 在 iOS8 系统中,还需要添加这个方法。通过新的 API 注册推送服务
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}
//注意:很重要的一步,我们在
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"test:%@",deviceToken);
    [BPush registerDeviceToken:deviceToken];
    [BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
        // 需要在绑定成功后进行 settag listtag deletetag unbind 操作否则会失败
        if (result) {
            NSString* identifierNumber = [[UIDevice currentDevice].identifierForVendor UUIDString] ;
            [BPush setTag:identifierNumber withCompleteHandler:^(id result, NSError *error) {
                if (result) {
                    NSLog(@"getChannelId = %@",[BPush getChannelId]);
                    NSLog(@"getUserId = %@",[BPush getUserId]);
                    NSLog(@"getUserId = %@",[BPush getUserId]);
                    NSLog(@"设置tag成功");
                }
            }];
        }
    }];
}

// 当 DeviceToken 获取失败时,系统会回调此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"DeviceToken 获取失败,原因:%@",error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //存入消息
    [CommonUDManager setMessageData:@"1"];
    //通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BDPush" object:self];
    // App 通知
    NSLog(@"********** ios7.0之前 **********");
    // 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
    if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
        NSLog(@"acitve or background");
        
        UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    else//杀死状态下,直接跳转到跳转页面。
    {
       //跳转页面
    }
    NSLog(@"%@",userInfo);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //跳转页面
    if (buttonIndex == 1) {
        //跳转页面
    }
}
复制代码

 

你可能感兴趣的:(iOS之百度云推送二)