iOS开发三步搞定百度推送
iOS开发三步搞定百度推送
百度推送很简单,准备工作:在百度云推送平台注册应用,上传证书。
步骤一:
百度云推送平台 http://push.baidu.com/sdk/push_client_sdk_for_ios
在这里下载iOS端SDK包,如下图;

把SDK包里面的下图文件夹拖到你的工程中,如下图,第一步就这么简单。

步骤二:
在工程中AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}方法中里初始化百度推送,代码如下加粗字体;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//百度推送
[BPush registerChannel:launchOptions apiKey:@"这里是百度推送平台给的你的应用的apikey" pushMode:BPushModeProduction withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];
//初始化百度推送
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[BPush handleNotification:userInfo];
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];
self.mainController = [[MainTabBarViewController alloc] init];
self.window.rootViewController = self.mainController;
[self.window makeKeyAndVisible];
return YES;
}
注意:1、代码中的apikey需要改成自己应用在百度推送注册后平台给的apikey;(这个代表你的项目在百度推送平台上的唯一标识)

2、代码中pushMode:的两种状态分别是开发状态和生产状态,开发者可以根据自己目前状态进行更改。

步骤三:还是在AppDelegate.m文件的下述方法中将得到的deviceToken传给SDK。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[BPush registerDeviceToken:deviceToken];
[BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *channle_id = result[@"channel_id"];
[user setObject:channle_id forKey:@"channel_id"];
[user synchronize];
}];
}
然后在下述两个方法(方法名很相似,上边的带有block,这里两个方法里写的东西是相同的)中实现相关跳转就可以啦(给出的例子仅供参考,这里跳转用的是用本地通知实现的相应跳转)注:具体跳转需要跟后台开发人员制定规则,一起调试。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
type = userInfo[@"push_type"];
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
else
//杀死状态下,直接跳转到跳转页面。
{
if ([type isEqualToString:@"comment"]) {
NSNotification *noti =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"reward"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"follow"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"system"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}
}
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[BPush handleNotification:userInfo];
[application setApplicationIconBadgeNumber:5];
type = userInfo[@"push_type"];
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
// NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
else//杀死状态下,直接跳转到跳转页面。
{
if ([type isEqualToString:@"comment"]) {
NSNotification *noti =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"reward"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"follow"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"system"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}
}
}
iOS开发三步搞定百度推送的更多相关文章
- Spring Boot 集成 Ehcache 缓存,三步搞定!
作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...
- Excel大数据排查重复行内容方法,三步搞定!
首先第一步,我们找到一个空白列D输入公式“=A1&B1&C1”: 然后第二步,再选择下一空白列输入公式“=IF(COUNTIF(D:D,D1)>1,"重复", ...
- 三步搞定IDEA集成热部署
第一步.在你的SpringBoot项目中添加DevTools依赖 <!-- 热部署DevTools --> <dependency> <groupId>org.sp ...
- [原创][开源]C# Winform DPI自适应方案,SunnyUI三步搞定
SunnyUI.Net, 基于 C# .Net WinForm 开源控件库.工具类库.扩展类库.多页面开发框架 Blog: https://www.cnblogs.com/yhuse Gitee: h ...
- iOS开发- 三步快速集成社交化分享工具ShareSDK
1.前言 作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里较为常用的. 最近因为公司App有社交化分享的需要,就特此研究了会,拿出来与大家分享. 想要集成社交会分享,我们可以使 ...
- 从 0 开始手写一个 Mybatis 框架,三步搞定!
阅读本文大概需要 3 分钟. MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码. 本文完成的Mybatis功能比较简单,代码还有许 ...
- 三步搞定Centos 7 上特定版本的 docker 安装
由于国内网络原因,使用centos的用户yum源常用国内的阿里云.现在把centos7上安装docker的详细过程记录如下: 一.配置centos7的yum源(阿里云) 1.cd /etc/yum. ...
- 三步搞定 opencv 初始环境设定
一.设定bin的初始位置:比如我的电脑 D:\安装程序\opencv\build\x86\vc10\bin H:\生产力工具\opencv\build\x86\vc10\bin D:\安装程 ...
- 三步搞定ISO/GHO安装系统 - imsoft.cnblogs
高清互动安装系统附件:重装系统视频教程.7z
随机推荐
- Ubuntu下配置samba实现文件夹共享
转自:http://www.cnblogs.com/phinecos/archive/2009/06/06/1497717.html 一. samba的安装: sudo apt-get insall ...
- 在Virtulbox上装Ubuntu
做个程序员,会用Linux,这应该是最基本的要求吧.可惜本人经常用Windows,只是偶尔去服务器上做些操作的时候才接触到linux.so,我要学Linux.刚学所以还是先装个虚拟机吧,等在虚拟机上用 ...
- 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数
题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...
- Arduino101学习笔记(十二)—— 101定时器中断
一.API 1.开定时器中断 //*********************************************************************************** ...
- Android 弹出对话框Dialog充满屏幕宽度
final View view = LayoutInflater.from(context).inflate(layoutId, null); final Dialog dialog = new Di ...
- Almost Sorted Array
http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006 #include<iostream> ...
- hdu1754 线段树
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...
- hdu2546 01背包
http://acm.split.hdu.edu.cn/showproblem.php?pid=2546 01背包问题,首先拿出5元买最贵的东西,那接下来就是背包容量m-5,物品数量n-1 的01背包 ...
- Zepto tap 穿透bug
当两个层重叠在一起时,使用Zepto的tap事件时,点击上面的一层时会触发下面一层的事件,特别是底层如果是input框时,必“穿透”,“google”说原因是“tap事件实际上是在冒泡到body上时才 ...
- time元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...