标签:
远程推送(糯米优惠推送)
前提准备
1.远程推送必需要真机
2.实现推送必需是开发者帐号(给钱)
远程推送示意图:
实现步骤
1.生成真机调试证书+描述文件
"生成真机调试证书(ios_development)" ——》目的是让你的电脑有真机调式的功能
"描述文件(Provisioning Profiles)" --》让你手机可以真机调试
》在后台添加设备ID
》添加测试app的bundle ID
"生成推送app的bundle ID 不能使用通配(cn.itcast.*) cn.itcast.nuomi"
2.生成推送证书(aps_development)
"此推送证书是绑定了你的电脑" -》 CSR
"工作过程中,要给后台开发人员一个推送的证书,但是这个证书不是aps_development.cer 文件,我们要给的是一个 "交换推送证书文件(p12)"给后台开发人员"
"现在演示是开发调试阶段,如果要上线了,生成一个 上线的推送证书(aps_Production.cer)"
3.获取deviceToken
每一台手机设备要实现远程推送,都会有一个标识,
*这个标识不是设备ID,
*而是设备会根据证书生成一个deviceToken
"怎么获取?"
•在didFinishLuaching方法里,添加远程通知的一个注册
[application registerForRemoteNotifications];
4.模拟服务器 发送 优惠的信息给 APNS
•打开PushMeBaby软件
•推送数据是有固定格式(JSON)
{"aps":{"alert":"This is some fancy message.","badge":1,"detaiURL":"xxxx.html"}}
•可以固定json推送数据添加一些额外key,如detaiURL
5.应用程序怎么接收到远程通知
// 程序运行在后台, 没有被杀死时
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
// 程序被杀死了 点击推送 通知消息在launchOptions里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
6. 代码示例
1 #import "AppDelegate.h"
2
3 @interface AppDelegate ()
5 @end
6
7 @implementation AppDelegate
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11 // Override poi nt for customization after application launch.
12
13 // 添加通知权,只有在ios8才需要
14 if ([[UIDevice currentDevice].systemVersion doubleValue] >=8.0) {
15 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
16 [application registerUserNotificationSettings:settings];
17
18 // 远程通知的一个开关
19 [application registerForRemoteNotifications];
20 }else{//ios8以前远程通知的一个开关
21 // UIRemoteNotificationTypeSound = 1 << 1,
22 // UIRemoteNotificationTypeAlert
23 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge];
24 }
25
26 // 接收远程通知数据
27 if (launchOptions) {
28
29 // 获取userInfo
30 NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
31 // 进入详情页面(网页)
32 [self enterWeb:userInfo];
33 // NSString *msg = [NSString stringWithFormat:@"%@",launchOptions];
34 // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xxx" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
35 // [alert show];
36 }
37
38 return YES;
39 }
40
41 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
42
43 //获取deviceToken
44 NSString *deviceTokenStr = [deviceToken description];
45 NSLog(@"%@",deviceTokenStr);
46
47 #warning 把deviceToken 传给 服务器
48 }
49
50 #pragma mark 接收远程通知
51 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
52 //{"aps":{"alert":"This is some fancy message.","badge":1}}
53 //{"aps":{"alert":"珠江新城某某海鲜自助餐18块.","badge":3,"detailURL":"http://gz.nuomi.com/deal/7dbq9rbz.html"}}
54 NSLog(@"%@",userInfo);
55
56 [self enterWeb:userInfo];
57 }
58
59
60 -(void)enterWeb:(NSDictionary *)userInfo{
61
62 // 延时
63 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
64 NSString *alert = userInfo[@"aps"][@"alert"];
65 NSLog(@"%@",alert);
66 // 接收到远程推送,进入一详细界面 http://gz.nuomi.com/deal/7dbq9rbz.html
67 // 获取URL
68 NSString *detailURL = userInfo[@"aps"][@"detailURL"];
69
70 // 一般会搞个网页控制器来显示URL
71 // 这边打开系统的safi
72 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:detailURL]];
73 });
74
75
76 }
标签:
原文地址:http://www.cnblogs.com/Life-GJB/p/5371866.html