美文网首页iOS知识集iOS开发iOS开发
iOS10 推送通知 (UserNotifications.fr

iOS10 推送通知 (UserNotifications.fr

作者: liangdahong | 来源:发表于2016-11-14 13:01 被阅读230次

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];
    });
}


远程


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


相关文章

  • iOS10 推送通知 (UserNotifications.fr

    UserNotificationsDemo iOS10 推送通知 (UserNotifications.frame...

  • iOS10推送通知整理总结

    这篇文章整理iOS10之后的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以后的推送通知) iOS10...

  • iOS推送通知学习与总结

    这篇文章整理iOS10之前的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以前的推送通知) iOS10...

  • IOS的通知

    通知详解 简书-iOS10 推送通知 UserNotifications iOS10本地通知UserNotifi...

  • 关于iOS通知那些事

    一、概述 通知分为本地通知和远程推送通知,iOS10中对于通知这一块改变较大,本文主要针对iOS10的通知,iOS...

  • iOS10里的通知与推送

    转载自:iOS10里的通知与推送 原文 通知和推送是一种东西么? iOS 10通知 推送 图1为通知,图2为推送也...

  • iOS10 - 推送通知

    iOS10里的通知与推送 通知文章2 远程推送工具 一、各版本通知比较 iOS 8以后,APNs推送的字节是2k,...

  • iOS10 推送通知详解(UserNotifications)

    本文发布在:iOS10 推送通知详解(UserNotifications) iOS10新增加了一个UserNoti...

  • iOS 10 推送你玩过了吗?

    相比之前的通知功能来说,iOS10的通知爽爆了。看完iOS10的推送总结以下几点相比之前的推送不一样的地方,可能有...

  • iOS10 推送通知

    iOS10新增加了一个UserNotificationKit(用户通知框架)来整合通知相关的API,UserNot...

网友评论

    本文标题:iOS10 推送通知 (UserNotifications.fr

    本文链接:https://www.haomeiwen.com/subject/vciiuttx.html