iOS - 百度推送

来源:互联网 发布:网络推广员有用吗 编辑:程序博客网 时间:2024/06/05 10:21

在很多项目开发中,大家都做过推送!例如:极光推送、信鸽推送、个推等等一系列的推送。我们使用的都是集成过后的SDK,其原理都是相同的。
这里我们来做一下百度推送。(目前更新到iOS10,具体iOS10新的推送样式参考其他资料,这里不做具体说明了)

1.百度推送

1.1 百度账号的注册申请和审核

登录百度云推送:http://push.baidu.com/fc。填写相关资料、审核。
审核通过后下载SDK,创建应用(上传相应的推送证书格式看要求到出),默认创建的应用是,开发者测试的,上线后要改成生产的。

1.2 SDK的导入与配置

1)所使用的SDK很简单。(里面有个测试音频text.caf
这里写图片描述
2)相应的库是否导入
这里写图片描述
3)证书要带推送功能

1.3 SDK在项目中的使用

1)启动时写的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{//***********************关于如何设置badge角标加1的方法***********************/* 服务端推送的badge是几就会显示几,你只需要跟服务端同步消息数目,然后让服务端自己,该推送几,就推送几,比如你应用打开的时候,或者进入后台的时候跟服务端同步,这个点,需要你们自己去设计,应用没有消息的时候,服务端推送了1,当应用打开时候,告诉服务端,app没点击通知,那下次应用推送2,依次类推。 */
    //-----------------2016年11月17日更新------------------    // iOS10 下需要使用新的 API(注意:UNUserNotificationCenterDelegate 这个代理是一定要签的)#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import <UserNotifications/UserNotifications.h>#endif    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {#ifdef NSFoundationVersionNumber_iOS_9_x_Max        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];        [application registerUserNotificationSettings:settings];        UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];        //代理必须写        center.delegate = self;        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)                              completionHandler:^(BOOL granted, NSError * _Nullable error) {                                  // Enable or disable features based on authorization.                                  if (granted) {                                      [application registerForRemoteNotifications];                                      if (!error) {                                          NSLog(@"succeeded!");                                      }                                  }                              }];        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {            //打印推送设置            NSLog(@"========%@",settings);        }];#endif//-----------------2016年11月17日更新------------------    // 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];    }    #warning 测试 开发环境 时需要修改BPushMode为BPushModeDevelopment 需要修改Apikey为自己的Apikey    // 在 App 启动时注册百度云推送服务,需要提供 Apikey    [BPush registerChannel:launchOptions apiKey:@"Apikey改成自己的" pushMode:BPushModeDevelopment withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];    // App 是用户点击推送消息启动    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    if (userInfo) {        NSLog(@"从消息启动:%@",userInfo);        [BPush handleNotification:userInfo];    }     //角标清0    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];}

2)点击推送消息

// 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{     completionHandler(UIBackgroundFetchResultNewData);    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//杀死状态下,直接跳转到跳转页面。    {      //这里实现页面跳转    }}

3)注册推送服务

// 在 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) {            [BPush setTag:@"Mytag" withCompleteHandler:^(id result, NSError *error) {                if (result) {                    NSLog(@"设置tag成功");                }            }];        }    }];}// 当 DeviceToken 获取失败时,系统会回调此方法- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{    NSLog(@"DeviceToken 获取失败,原因:%@",error);}

4)收到推送消息时

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    // App 收到推送的通知    [BPush handleNotification:userInfo];    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//杀死状态下,直接跳转到跳转页面。    {    }}- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    NSLog(@"接收本地通知啦!!!");    [BPush showLocalNotificationAtFront:notification identifierKey:nil];}

iOS10新增回调

//iOS10 回调//-----------------20161117日更新------------------- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {    completionHandler(UNNotificationPresentationOptionBadge|                      UNNotificationPresentationOptionSound|                      UNNotificationPresentationOptionAlert);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{    completionHandler(UNNotificationPresentationOptionBadge|                      UNNotificationPresentationOptionSound|                      UNNotificationPresentationOptionAlert);}//-----------------2016年11月17日更新------------------

1.4 开启和关闭推送按钮

说说我的思路和做法:本地存一个参数1或者0 ,例如:status用于存放状态。加一个按钮,点击关闭状态status存放0,同时UIUserNotificationType设置为:UIUserNotificationTypeNone UIRemoteNotificationType设置为:UIRemoteNotificationTypeNone 反之设置回声音、徽章、弹窗提醒。
其他的跳转页面等操作可以预判所存放状态status.


其他的具体内容各个推送都有自己具体的详细文档,说明的要比我写的详尽的多,如有什么不对不懂得地方可以评论,我会及时解答和修改!

1 0