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
随机推荐
- hdu 4762 公式 java
n/(n)^(m-1) import java.io.*; import java.math.*; import java.util.*; public class Main { static Big ...
- 为什么没有选择sipml5
转自:http://www.myvoipapp.com/blogs/yxh/2015/01/23/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%B2%A1%E6%9C%89%E9%80 ...
- PMP 第十二章 项目采购管理
1规划采购 2实施采购 3管理采购 4结束采购 1.合同的不同叫法?卖方的不同叫法? 2.规划采购管理的目的是什么?合同类型有哪些?不同的特点是什么?何种情况下应选择何种合同?自制或外购分析计算和合同 ...
- linux 用户之间的切换
从root用户切换到普通用户fxm, 使用如下命令:su - fxm 从普通用户切换到root用户,使用如下命令:su - 或者 su, root可以省略不写.
- tree view
<TreeView x:Name="treeParameter" Width=" Margin="11,6,11,6" ItemsSource= ...
- 程序员必懂:javaweb三大框架知识点总结
原文链接:http://www.cnblogs.com/SXTkaifa/p/5968631.html javaweb三大框架知识点总结 一.Struts2的总结 1.Struts 2的工作流程,从请 ...
- cocos2dx游戏开发——微信打飞机学习笔记(二)——游戏框架
一.游戏的基本框架: WelcomeScene ——> GameScene ——> GameOverScene || ...
- Java中引用类 strong reference .SoftReference 、 WeakReference 和 PhantomReference的区别
当在 Java 2 平台中首次引入 java.lang.ref 包,其中包含 SoftReference . WeakReference 和 PhantomReference 三个引用类,引用类的 ...
- Linux学习笔记(12)用户和用户组管理
越是对服务器安全性要求高的服务器,越需要建立合理的用户权限等级制度和服务器操作规范.在Linux中主要是通过用户配置文件来查看和修改用户信息. 1 用户信息文件 (1)用户信息文件/etc/passw ...
- 启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required
错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009) http://blog.csdn.net/xinxin19881112/article/det ...