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
随机推荐
- 怎样打开64位 Ubuntu 的32位支持功能?
转自:http://jingyan.baidu.com/article/7082dc1c539c15e40a89bd3e.html 大多数使用基于 Ubuntu/Debian 的发行版的人都更倾向于选 ...
- 第七篇:创建一个SOUI的Hello World
从0开始一个SOUI项目 1.环境配置 SOUI项目本质是一个基于Win32窗口的应用程序.因此首先我们可以从Win32窗口应用程序向导创建一个简单的Win32项目. 并在第3页选择“Window应用 ...
- HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP
考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...
- Ubuntu14 搭载vim环境查看源码
首先是下载完整的vim74,然后编译安装.遗憾的是当编译时,没有开启图形界面. 在安装新版本的Vim之前,你需要卸载原来安装的老版本Vim,依次在终端下执行下列命令: sudo apt-get rem ...
- 导出word使用模版
在我们做我们的小组项目的时候,刚开始的时候我们用到的是Mvc+EF,用上了我们的ITOO框架.在最开始的计划,我们要用到瑞郎报表.可是呢,由于工期原因以及技术暂时没有实现,我们不得不想一个比较折中的方 ...
- How to choose the number of topics/partitions in a Kafka cluster?
This is a common question asked by many Kafka users. The goal of this post is to explain a few impor ...
- hdu 1069 Monkey and Banana
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Codeforces Round #355 (Div. 2)-A
A. Vanya and Fence 题目连接:http://codeforces.com/contest/677/problem/A Vanya and his friends are walkin ...
- The name does not exist in the namespace error in XAML
方法1:In the solution property page, check the platform of the assembly that contains "UpdatingMe ...
- HDU-2159FATE(二维完全背包)
FATE Problem Description 最 近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...