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
随机推荐
- [荐]jquery插件开发入门
http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html $.fn.myPlugin = function() { //在这里面,this指 ...
- C# 指针操作图像 二值化处理
/// <summary> /// 二值化图像 /// </summary> /// <param name="bmp"></param& ...
- commonlisp教程以及学习笔记-01
手上有两个教程,但是感觉这个教程可能更适合自己
- 关于移动端1px边框问题
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...
- c++11 正则表达式基本使用
c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...
- 非旋转Treap及可持久化[Merge,Split]
http://memphis.is-programmer.com/posts/46317.html http://fanhq666.blog.163.com/blog/static/819434262 ...
- Http协议提要
HTTP协议提要 简单来说,HTTP就是一个基于应用层的通信规范:双方要进行通信,大家就要遵守一个规范---HTTP协议.HTTP协议从WWW服务器超文本到本地浏览器 ,可以使浏览器更加高效.HTTP ...
- Coursera课程下载和存档计划[转载]
上周三收到Coursera平台的群发邮件,大意是Coursera将在6月30号彻底关闭旧的课程平台,全面升级到新的课程平台上,一些旧的课程资源(课程视频.课程资料)将不再保存,如果你之前学习过相关的课 ...
- redis 的使用 (sort set排序集合类型操作)
sort set排序集合类型 释义: sort set 是 string 类型的集合 sort set 的每个元素 都会关联一个 权 通过 权值 可以有序的获取集合中的元素 应用场合: 获取热门帖子( ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...