iOS远程推送通知
远程推送服务,APNs(apple push notification servers)
所有的苹果设备,在联网状态下,都会与苹果的服务器建立
长连接
-
长连接
- 只要联网了,就一直建立连接
- 长连接的作用:1.时间校准;2.系统升级;3.查找我的iPhone等
-
长连接的好处
- 数据传输速度快
- 数据保持最新状态
- 远程推送的基本过程
- 1.客户端的app需要将用户的UDID和app的bundleID发送给apns服务器,进行注册,apns将加密后的device Token返回给app
- 2.app获得device Token后,上传到公司服务器
- 3.当需要推送通知时,公司服务器会将推送内容和device Token一起发给apns服务器
- 4.apns再将推送内容送到客户端上
-
客户端要做的事情
- 1.注册苹果获得deviceToken
- 2.得到苹果返回的deviceToken
- 3.发送deviceToken给公司的服务器
- 4.监听用户对通知的点击
- 调试远程推送功能时,是需要用到真机测试
- 真机测试相关内容
- 创建appID ,(必须创建完整的appID,不能带有*)
- 创建apns ssl证书(获得一个调试证书,一个发布证书)
- 生成相应的描述文件
- 最后安装相关证书
-
注册远程通知(和本地通知注册基本相同)
// AppDelegate.m
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
// 1.注册UserNotification,以获取推送通知的权限
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[application registerUserNotificationSettings:settings];
// 2.注册远程推送
[application registerForRemoteNotifications];
} else {
//iOS8之前,注册远程推送的方法
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
//调用该方法,获得deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken.description);
}
// 当接受到远程退职时会执行该方法(当进入前台或者应用程序在前台)
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
//{
// NSLog(@"%@", userInfo);
//
// UIView *redView = [[UIView alloc] init];
// redView.backgroundColor = [UIColor redColor];
// redView.frame = CGRectMake(100, 100, 100, 100);
// [self.window.rootViewController.view addSubview:redView];
//}
/*
1.开启后台模式(图在下面)
2.调用completionHandler,告诉系统你现在是否有新的数据更新
3.userInfo添加一个字段:"content-available" : "1" : 只要添加了该字段,接受到通知都会在后台运行
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"%@", userInfo);
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.window.rootViewController.view addSubview:redView];
completionHandler(UIBackgroundFetchResultNewData);
}
-
配置后台模式
-
第三方推送 JPush的简单使用
JPush官方网站:https://www.jpush.cn
集成iOS SDK的步骤可以参考
http://docs.jpush.cn/pages/viewpage.action?pageId=2621727简单的后台配置流程
-
1.注册账号,创建应用
-
2.填写相关信息(上传相关p12文件)
3.发送通知
-
app中代码集成
- 导入 #import "APService.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)categories:nil];
} else {
//categories 必须为nil
[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil];
}
// Required
[APService setupWithOption:launchOptions];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required
[APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[APService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}