iOS10 推送最新特性研究

2020-01-17 21:54:51于丽

最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:

 1.推送内容更加丰富,由之前的alert 到现在的title, subtitle, body
 2.推送统一由trigger触发
 3.可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
 4.可以方便的更新推送内容 

import 新框架

添加新的框架 UserNotifications.framework

 iOS10,推送

#import <UserNotifications/UserNotifications.h> 

注册推送

在设置通知的时候,需要先进行注册,获取授权
iOS10 所有通知都是通过UNUserNotificationCenter来管理,包括远程通知和本地通知


  //iOS8以下
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

  //iOS8 - iOS10
  [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];

  //iOS10
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {

  }

获取用户设置

iOS10 提供了获取用户授权相关设置信息的接口getNotificationSettingsWithCompletionHandler: , 回调带有一个UNNotificationSettings对象,它具有以下属性,可以准确获取各种授权信息

authorizationStatus
soundSetting
badgeSetting
alertSetting
notificationCenterSetting
lockScreenSetting
carPlaySetting
alertStyle 

像下面的方法,点击allow

iOS10,推送


   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
     if (granted) {
        //点击允许
        NSLog(@"注册通知成功");
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@", settings);
        }];
      } else {
        //点击不允许
        NSLog(@"注册通知失败");
      }
    }];

打印信息:   *<UNNotificationSettings: 0x174090a90; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle: Banner>*