远程通知的原理
-
获取device Token的过程
<div align = center>
</div>
-
从获得device Token到推送消息给设备的过程
<div align = center>
</div>
- Provider 为消息发送的服务器端
- APNs 远程推送服务
配置证书
-
调试远程推送需要的证书文本
- ios_development.cer 让电脑具备真机调试的能力
- aps_development.cer 让电脑有调试APP推送服务的能力
- iOS_Development.mobileprovision 某台电脑就能利用某台真机设备调试某个程序
ios_development.cer配置略 CSR创建略
-
必需一个配置了精确的Bundld ID调试证书
<div align = center>
</div>
-
创建完成之后,我们点击查看详情
Push Notifications
是Enabled
状态才可以
<div align = center>
</div> -
为App ID创建APNs SSL证书(真机调试用的APNs SSL证书:要在哪台电脑上调试具有推送服务的App
)
<div align = center>
</div>
-
生成描述文件 (描述文件的作用是用来描述:哪台设备要在哪台电脑上调试哪个程序)
<div align = center>
</div>
-
最终会得到3个文件
先安装cer文件,再安装mobileprovision文件
远程通知实现流程
- 获取device Token
要获取到正确的device Token
,要确保下面选项是打开的(Xcode7之后)
<div align = center>
</div>
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/// 注册通知
/// 判断当前设备
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// iOS8 之后的版本
//1. 配置通知的类型
/*
UIUserNotificationTypeBadge
UIUserNotificationTypeSound
UIUserNotificationTypeAlert
*/
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
//2. 配置settings
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//3. 注册settings
[application registerUserNotificationSettings:settings];
//4. 注册远程通知
[application registerForRemoteNotifications];
} else {
//1. 配置通知的类型
/**
UIRemoteNotificationTypeBadge
UIRemoteNotificationTypeSound
UIRemoteNotificationTypeAlert
*/
UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
//2. 注册远程通知
[application registerForRemoteNotificationTypes:type];
}
return YES;
}
#pragma mark - 当完成注册远程通知时,会调用的方法,在这里返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//3c48ae8d f6e2c1ff 473c84ea edcfff3d 9dd3beee 30eefbcb e5158680 80da7238
NSLog(@"%@",deviceToken);
//deviceToken发送到自己的服务器做存储
}
- 消息处理
1.PushMeBaby
我们要借助PushMeBaby模拟服务器,来给我们的APP推送消息, PushMeBaby
配置要做如下配置:
<div align = center>
</div>
2.代码实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册通知
/// 判断当前设备
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// iOS8 之后的版本
//1. 配置通知的类型
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
//2. 配置settings
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//3. 注册settings
[application registerUserNotificationSettings:settings];
//4. 注册远程通知
[application registerForRemoteNotifications];
} else {
//1. 配置通知的类型
UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
//2. 注册远程通知
[application registerForRemoteNotificationTypes:type];
}
//程序退出的时候 我们可以在这里拿到远程通知的内容
//launchOptions是一个字典
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSDictionary *remoteKey = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(@"%@",remoteKey);
}
return YES;
}
#pragma mark - 当完成注册远程通知时,会调用的方法,在这里返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//3c48ae8d f6e2c1ff 473c84ea edcfff3d 9dd3beee 30eefbcb e5158680 80da7238
NSLog(@"%@",deviceToken);
//deviceToken发送到自己的服务器做存储
}
#pragma mark - 当接受到远程推送的值得时候回调用
// 此方法无论前台/后台/退出的情况下都会调用 iOS7 新增的
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// 逻辑处理
NSLog(@"userInfo: %@",userInfo);
// 必须调用的
completionHandler(UIBackgroundFetchResultNewData);
}
3.控制台打印
2016-07-16 15:39:47.611 mall[8118:1494303] userInfo: {
aps = {
alert = "\U4eca\U5929\U505a\U4e2a\U56db\U6709\U9752\U5e74.";
badge = 1;
};
}
4.真机效果
<div align = center>
</div>
JPush远程推送设置
请看下一篇博客介绍 iOS开发-极光远程推送