iOS10之UserNotifications初见

UILocalNotification在iOS10上已经过期失效,熟悉下最新的framework成为必要。如下将一一道来:

使用通知需要在didFinishLaunching中注册,所以需要引用头,并添加代理,后期会用到。

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    center.delegate = self;

    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {

        /// 没有注册会弹出警告框

    }];

似乎我在使用delegate并未调用到willPresentNotification,后期再研究下。只使用了

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{}

testVC Event调用发消息如下——————————       跟LocalNotification近似

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];

/////   这里需要注意不能用UNNotificationContent,其为只读属性.

    content.title = @"FirstTest";

    content.subtitle = @"本地通知:";

    content.body = @"这是一长串的文字,用于来显示测试通知中的内容。";

    content.badge = @2;

    content.launchImageName =  @"launchScreen";

    content.sound = [UNNotificationSound defaultSound];

    content.userInfo = @{@"1":@"UNNotificationAction",

                         @"2":@"UNNotificationCategory",

                         @"3":@"UNNotificationContent"

                         };

    //UNNotificationTrigger有三种样式,看官方文档即懂

    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

    

    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"test" content:content trigger:trigger];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

//////   添加通知,只要知道这一句前面都可以反推了

    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

        NSLog(@"get notification");

    }];

 /****  取消通知

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    [center removeAllPendingNotificationRequests]; ////  removePendingNotificationRequestsWithIdentifiers:(NSArray<NSString *> *)identifiers; 这个是移除指定的通知

***********/

前面提到的代理中userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler,调用completionHandler()的block可以实现通知跳转界面。且response.notification.request.content得到UNMutableNotificationContent的属性,如usreInfo

打印response得到

response : <UNNotificationResponse: 0x60000023ae40; actionIdentifier: com.apple.UNNotificationDefaultActionIdentifier, notification: <UNNotification: 0x60000023b3a0; date: 2017-06-28 16:00:25 +0000, request: <UNNotificationRequest: 0x6080002276e0; identifier: test, content: <UNNotificationContent: 0x60800011a4c0; title: FirstTest, subtitle: 本地通知:, body: 这是一长串的文字,用于来显示测试通知中的内容。, categoryIdentifier: , launchImageName: LaunchScreen, peopleIdentifiers: (

), threadIdentifier: , attachments: (

), badge: 2, sound: <UNNotificationSound: 0x6080000a9a80>, hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x60000023af00; repeats: NO, timeInterval: 5.000000>>>>

提高技能如同提升自信心。
原文地址:https://www.cnblogs.com/chims-liu-touch/p/7092354.html