从iOS8.0开始推送功能的实现在不断改变,功能也在不断增加,iOS10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码:
在开始之前需要打开一个推送开关,不然无法获取deviceToken,老项目或者出现deviceToken无效的情况:如图:
打开后会生成entitlements文件,需要有APS Environment
或许还应该打开这个
#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/* APP未启动,点击推送消息的情况下 iOS10遗弃UIApplicationLaunchOptionsLocalNotificationKey,使用代理UNUserNotificationCenterDelegate方法didReceiveNotificationResponse:withCompletionHandler:获取本地推送
*/
// NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// if (localUserInfo) {
// NSLog(@"localUserInfo:%@",localUserInfo);
// //APP未启动,点击推送消息
// }
NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteUserInfo) {
NSLog(@"remoteUserInfo:%@",remoteUserInfo);
//APP未启动,点击推送消息,iOS10下还是跟以前一样在此获取
}
[self registerNotification];
return YES;
}
注册推送方法的改变:
新增库 #import <UserNotifications/UserNotifications.h> 推送单列UNUserNotificationCenter 等API
- (void)registerNotification{
/*
identifier:行为标识符,用于调用代理方法时识别是哪种行为。
title:行为名称。
UIUserNotificationActivationMode:即行为是否打开APP。
authenticationRequired:是否需要解锁。
destructive:这个决定按钮显示颜色,YES的话按钮会是红色。
behavior:点击按钮文字输入,是否弹出键盘
*/
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行为1" options:UNNotificationActionOptionForeground];
/*iOS9实现方法
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotification