iOS 10 添加本地推送(Local Notification)
2017-03-28 12:24
375 查看
前言
iOS 10 中废弃了UILocalNotification(
UIKit Framework) 这个类,采用了全新的
UserNotifications Framework来推送通知,从此推送通知也有了自己的标签
UN(这待遇真是没别人了),以及对推送功能的一系列增强改进(两个 extension 和 界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点。
本文主要查看了 iOS 10 的相关文档,整理出了在 iOS 10 下的本地推送通知,由于都是代码,就不多做讲解,直接看代码及注释,有问题留言讨论哦。
新的推送注册机制
注册通知(Appdelegate.m):
#import <UserNotifications/UserNotifications.h> #import "AppDelegate.h" @interface AppDelegate ()<UNUserNotificationCenterDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 使用 UNUserNotificationCenter 来管理通知 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //监听回调事件 center.delegate = self; //iOS 10 使用以下方法注册,才能得到授权 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { // Enable or disable features based on authorization. }]; //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }]; return YES; } #pragma mark - UNUserNotificationCenterDelegate //在展示通知前进行处理,即有机会在展示通知前再修改通知内容。 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //1. 处理通知 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式 completionHandler(UNNotificationPresentationOptionAlert); } @end
推送本地通知
//使用 UNNotification 本地通知+(void)registerNotification:(NSInteger )alerTime{
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 在 alertTime 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:alerTime repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
//添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable 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];
}];
}
iOS 10 以前本地推送通知:请查看:点击打开链接
相关文章推荐
- iOS 10 添加本地推送(Local Notification)
- iOS 10本地推送
- 【iOS开发】iOS 10 添加推送功能注意点及问题汇总
- ios 本地推送的添加和取消
- iOS - iOS10 添加本地推送(Local Notification)
- iOS 10 本地推送UNUserNotificationCenter
- iOS-本地推送之UserNotifications(iOS 10)
- iOS 10本地推送
- IOS开发(10)UIBarButtonItem导航栏添加按钮
- iOS 推送,证书申请,本地推送
- ios推送:本地通知UILocalNotification
- iOS推送:实现本地推送
- ios推送:本地通知UILocalNotification
- IOS开发中本地消息推送方法
- iOS推送:实现本地推送
- ios UIApplication 在本地消息的推送上的应用
- [原]逆向iOS SDK -- “添加本地通知”的流程分析
- iOS中添加/删除本地通知(闹钟)的方法
- IOS iOS本地推送---常用开发中,通过消息传递信息
- ios本地推送方法