iOS10 推送通知 (UserNotifications.framework 的使用)

UserNotificationsDemo

iOS10 推送通知 (UserNotifications.framework 的使用)

本地

iOS10 新加入了UserNotifications.framework 框架,用于本地和远程推送的专属框架,可以说是在iOS历史上首次完全重构推送通知的框架结构。来自这里

在 iOS10 之前,在处理推送时各种代理回调,而且本地通知和远程通知不一样

各种代理如下:

/*本地*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
}
/*本地*/

/*远程*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))
completionHandler {
}
/*远程*/

在 iOS10 时系统把本地和远程统一起来了

注册
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (!error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"注册成功" message:nil delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alert show];
    }
}];
center.delegate = self;
发出本地通知
    // 使用 UNUserNotificationCenter 来管理通知
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

    //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"hi!" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
                                                         arguments:nil];
    content.sound = [UNNotificationSound defaultSound];
    
    // 在 alertTime 后推送本地推送
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:10.0 repeats:NO];
    
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content trigger:trigger];

    //添加推送成功后的处理!
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:cancelAction];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        }
    }];
回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) {

    UIAlertView *ALERT = [[UIAlertView alloc] initWithTitle:@"willPresentNotification----- 1" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [ALERT show];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED {
    dispatch_after(2, dispatch_get_main_queue(), ^{
        UIAlertView *ALERT = [[UIAlertView alloc] initWithTitle:@"didReceiveNotificationResponse----- 2" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [ALERT show];
    });
}

  • 完整demo地址

远程


欢迎转载,转载请注明出处!

  • 原博客
  • 我的blog
  • 我的github

你可能感兴趣的:(iOS10 推送通知 (UserNotifications.framework 的使用))