国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS10添加本地推送(Local Notification)實例

2019-10-21 18:52:19
字體:
來源:轉載
供稿:網友

前言

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 以前本地推送通知:

+ (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {  // ios8后,需要添加這個注冊,才能得到授權  // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  // categories:nil];  // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  // // 通知重復提示的單位,可以是天、周、月  // }    UILocalNotification *notification = [[UILocalNotification alloc] init];  // 設置觸發通知的時間  NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];  NSLog(@"fireDate=%@",fireDate);    notification.fireDate = fireDate;  // 時區  notification.timeZone = [NSTimeZone defaultTimeZone];  // 設置重復的間隔  notification.repeatInterval = kCFCalendarUnitSecond;    // 通知內容  notification.alertBody = @"該起床了...";  notification.applicationIconBadgeNumber = 1;  // 通知被觸發時播放的聲音  notification.soundName = UILocalNotificationDefaultSoundName;  // 通知參數  NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學習iOS開發了" forKey:@"key"];  notification.userInfo = userDict;    // ios8后,需要添加這個注冊,才能得到授權  if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {    UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type                                         categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];    // 通知重復提示的單位,可以是天、周、月    notification.repeatInterval = NSCalendarUnitDay;  } else {    // 通知重復提示的單位,可以是天、周、月    notification.repeatInterval = NSDayCalendarUnit;  }    // 執行通知注冊  [[UIApplication sharedApplication] scheduleLocalNotification:notification];}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 分宜县| 定安县| 安庆市| 马边| 鄯善县| 上虞市| 桃源县| 察哈| 凤山县| 淄博市| 桂平市| 松阳县| 汉川市| 贵南县| 鸡西市| 定结县| 霸州市| 锡林郭勒盟| 凤山市| 西平县| 博爱县| 连江县| 耿马| 西青区| 秦安县| 成都市| 双鸭山市| 阿拉善盟| 团风县| 苗栗市| 北流市| 德令哈市| 宽城| 天等县| 浦城县| 永平县| 承德市| 涟源市| 潜山县| 金川县| 托克托县|