制作推送证书
iOS工程开发指引中对推送流程的概括如下
服务端的Provider通过APNs将信息推送给Client App经过两步:
1 Provider -> APNs//需要苹果机构颁发的证书
2 APNs -> Client//需要DeviceToken标志App
制作证书之前,介绍一下iOS的设计理念: 基于闭环和安全的思考,苹果公司要求使用APNs服务的开发者,提供开发时的Mac设备、App的ID和运行App的手机,通过对三者的联合检查,基本上能保证确认App的唯一性,保证对AppStore的管理的安全性和可靠性。
首先,我们在苹果开发者中心,注册自己的App的唯一ID:
继续直至Done.
然后制作和AppID相绑定的CER证书
点击continue:
点击continue,能够看到需要创建CSR证书,下面有详细创建步骤,这一步可以绑定开发设备Mac。英文很简单,和创建发布证书时在「钥匙串访问」中的操作一样。
在「钥匙串访问」中能得到CSR文件
![](http://img.e-com-net.com/image/info10/7046565f3c5a4d79b892a35280c0e813.jpg)
上传CSR文件
直至Done,下载CER文件:
![](http://img.e-com-net.com/image/info10/751c6bc3729945a6822b67f67784a81a.jpg)
在「钥匙串访问」我的证书中,能看到安装后的结果:
可以将证书导出,单独存放。以后别人需要,方便直接发送。
打开AppID的PushNotification功能
现在,证书已经制作好了
在工程中使用证书
确认Target的Identify和Signing:
iOS10中,改进了推送的代理方法,增加了3DTouch效果。下面以iOS10的新方法在AppDelegate添加接受通知的代码:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
if(@available(iOS10.0, *)) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOLgranted,NSError* _Nullable error) {NSLog(@"%@", error); }]; UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:@"GENERAL"actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];// Create the custom actions for expired timer notifications.UNNotificationAction* snoozeAction = [UNNotificationAction actionWithIdentifier:@"SNOOZE_ACTION"title:@"Snooze"options:UNNotificationActionOptionAuthenticationRequired]; UNNotificationAction* stopAction = [UNNotificationAction actionWithIdentifier:@"STOP_ACTION"title:@"Stop"options:UNNotificationActionOptionDestructive]; UNNotificationAction* forAction = [UNNotificationAction actionWithIdentifier:@"FOR_ACTION"title:@"forAction"options:UNNotificationActionOptionForeground];// Create the category with the custom actions.UNNotificationCategory* expiredCategory = [UNNotificationCategory categoryWithIdentifier:@"TIMER_EXPIRED"actions:@[snoozeAction, stopAction,forAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];// Register the notification categories.UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center setDelegate:self]; [center setNotificationCategories:[NSSetsetWithObjects:generalCategory, expiredCategory,nil]]; [[UIApplicationsharedApplication] registerForRemoteNotifications]; }else{ }returnYES;}
#pragma mark - UNUserNotificationCenterDelegate- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions options))completionHandler{NSLog(@"%s", __func__);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{NSLog(@"%s", __func__);}- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)pToken {//保存deviceTokenNSLog(@"regisger success:%@",pToken);}iOS的远程推送需要在真机上调试,如果注册成功,就能在didRegisterForRemoteNotificationsWithDeviceToken方法中获取APNs返回的DeviceToken,在打印栏可以看到。
使用SmartPush调试
使用SmartPush可以在电脑上方便的模拟APNs推送。运行程序,选择我们生成的证书和填上打印栏获得的DeviceToken,就能在我们的App中看到APNs推送来的带有3DTouch功能的通知。
{
"aps" : { // 必须有
"alert" : {"body":"主体内容","title":"标题","subtitle":"子标题"},
"body" : "string",//消息体
"badge" : 1,//app的icon右上角的推送数字 在这里设置
"sound" : "default" 可以为空,为空就是默认的声音
},
"NotiId" : 20150821, // 自定义key值
}
-----------------------------------------------------------------------------------------------------------------------
支持后台推送语音播报
推送消息
{"aps":{"alert":"This is some fancy message.","content-available":1}}
//后台语音播报
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
-(void)applicationWillResignActive:(UIApplication*)application
{
//结束接受远程控制
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//后台语音播报
- (void)applicationDidEnterBackground:(UIApplication*)application {
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [appbeginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if(bgTask !=UIBackgroundTaskInvalid)
{
bgTask =UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if(bgTask !=UIBackgroundTaskInvalid)
{
bgTask =UIBackgroundTaskInvalid;
}
});
});
}
1.推送的大小限制
远程通知负载的大小根据服务器使用的API不同而不同。当使用HTTP/2 provider API时,负载最大为4kB;当使用legacy binary interface时,负载最大为2kB。当负载大小超过规定的负载大小时,APNs会拒绝发送此通知。
2.推送设计原理图