百度推送--IOS(一)
来源:互联网 发布:阿桑奇曝光中国知乎 编辑:程序博客网 时间:2024/05/16 08:22
1、 百度推送封装了苹果的APNS和google的SMS推送,作为开发者可以通过百度来完成这两个平台的推送。
百度推送开发指南:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guideios
苹果APNS开发相关指南请参考苹果开发者网站:https://developer.apple.com/
本文是完成简单的功能,可以发送通知到客户端,之后我会将这部分内容写成一个phonegap插件,以后在做推送时直接调用就可以了。
2、相关准备工作
在此之前,需要进入百度云推送管理控制台→开发者服务→云推送→推送设置, 新建一个工程,百度回返回给所建工程的的一些信息:
3、新建一个工程,导入第三方的包和头文件;
4、新建一个Property List 文件将百度返回给我们的工程信息添进去;
5、将导入的文件修改为非ARC模式:
6、打开AppDelegate.m引用头文件同时申请要用到的变量:
- #import "MainViewController.h"
- #import "BPush.h"
- #import "JSONKit.h"
- #import <Cordova/CDVPlugin.h>
- @interface AppDelegate ()
- @property (nonatomic, retain) NSString *appId;
- @property (nonatomic, retain) NSString *channelId;
- @property (nonatomic, retain) NSString *userId;
- @end
- @implementation AppDelegate
- @synthesize window, viewController;
- @synthesize appId, channelId, userId;
7、注册push
- - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
- {
- self.window.rootViewController = self.viewController;
- [self.window makeKeyAndVisible];
- //初始化push
- [BPush setupChannel:launchOptions];
- [BPush setDelegate:self];
- //在程序图标的右上角显示数字
- [application setApplicationIconBadgeNumber:0];
- //管理远程通知 方法来与苹果推送通知服务注册
- [application registerForRemoteNotificationTypes:
- /*推送的类型*/
- UIRemoteNotificationTypeAlert
- | UIRemoteNotificationTypeBadge
- | UIRemoteNotificationTypeSound];
- NSLog(@"++++++++++++++++++++++");
- return YES;
- }
8、因为在推送时开发者需要知道客户端手机的userid 和 channelid,所以我们需要在程序中获取到这些信息:
- - (void) onMethod:(NSString*)method response:(NSDictionary*)data {
- NSLog(@"On method:%@", method);
- NSLog(@"data:%@", [data description]);
- NSDictionary* res = [[NSDictionary alloc] initWithDictionary:data];
- if ([BPushRequestMethod_Bind isEqualToString:method]) {
- //从字典里取值
- NSString *appid = [res valueForKey:BPushRequestAppIdKey];
- NSString *userid = [res valueForKey:BPushRequestUserIdKey];
- NSString *channelid = [res valueForKey:BPushRequestChannelIdKey];
- NSString *requestid = [res valueForKey:BPushRequestRequestIdKey];
- int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];
- if (returnCode == BPushErrorCode_Success) {
- // 在内存中备份,以便短时间内进入可以看到这些值,而不需要重新bind
- self.appId = appid;
- self.channelId = channelid;
- self.userId = userid;
- [[NSUserDefaults standardUserDefaults] setObject:userId
- forKey:@"BAIDUNOTIFICATION_USERID"];
- [[NSUserDefaults standardUserDefaults] setObject:channelId
- forKey:@"BAIDUNOTIFICATION_CHANNELID"];
- }
- } else if ([BPushRequestMethod_Unbind isEqualToString:method]) {
- int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];
- if (returnCode == BPushErrorCode_Success) {
- }
- }
- NSLog(@"%@===========%@==============",method,[data description]);
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
- NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
- //程序运行的状态判断
- if (application.applicationState == UIApplicationStateActive) {
- // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification"
- message:[NSString stringWithFormat:@"The application received this remote notification while it was running:\n%@", alert]
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- }
- // 写程序图标左上角显示数字
- [application setApplicationIconBadgeNumber:0];
- //推送反馈
- [BPush handleNotification:userInfo];
- NSLog(@"%@",[userInfo JSONString]);
- NSLog(@"didReceiveRemoteNotification=======%@\n=========%@\n========%@\n",self.appId,self.userId,self.channelId);
- }
- - (void)application:(UIApplication *)application
- didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:token];
- [BPush registerDeviceToken:deviceToken];
- [BPush bindChannel];
- NSLog(@"%@", deviceToken);
- }
- //失败
- - (void) application:(UIApplication *)application
- didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
- // re-post ( broadcast )
- [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotificationError object:error];
- NSLog(@"Error in registration. Error: %@", error);
- }
13、Ok,在真机上运行我们的工程,这是获取到得手机的数据:
14、打开百度开发者服务→云推送→通知 ,选择添加通知推送,这样在客户端有网的情况下就可以将通知发送到客户端上。
15、demo下载请戳:https://github.com/dengfeng520/GSbaidupush
0 0
- 百度推送--IOS(一)
- 百度推送--IOS(一)
- 百度推送(iOS)
- iOS推送 (百度推送)
- iOS推送 (百度推送)
- ios推送(一)
- iOS推送(一):本地推送
- ios 推送消息 极光推送/百度推送
- 百度 iOS推送
- iOS百度推送
- <iOS>百度推送
- 百度推送iOS
- iOS 百度推送
- iOS - 百度推送
- 百度云推送----iOS
- ios 百度推送
- APNs-百度推送使用总结(一)
- ios 消息推送(一)
- 周报—FPGA(浩子)
- gcc -wall -pedantic -ansi
- LeetCode OJ 之 Implement strStr() (在字符串中查找字符串)
- 八皇后问题
- Android EventBus详解
- 百度推送--IOS(一)
- 在Java中使用weka:在数据集中增加属性
- 数字电路设计之数字电路工程师面试集锦
- HDU 1056 Hang over 主要是注意精度
- 最近整理IOS开发关于scrollView的一些细节和技巧
- 适配iOS 8备忘录 开始启动(持续更新。。。1030)
- 成绩排序
- 如何在window上把项目提交到github上
- 希尔排序