最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年09月21日 14:44:22   作者:Daizi  
這篇文章主要為大家詳細(xì)介紹了iOS10添加本地推送(Local Notification)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

iOS 10 中廢棄了 UILocalNotification ( UIKit Framework ) 這個(gè)類,采用了全新的 UserNotifications Framework 來推送通知,從此推送通知也有了自己的標(biāo)簽 UN (這待遇真是沒別人了),以及對(duì)推送功能的一系列增強(qiáng)改進(jìn)(兩個(gè) extension 和 界面的體驗(yàn)優(yōu)化),簡(jiǎn)直是蘋果的親兒子,因此推送這部分功能也成為開發(fā)中的重點(diǎn)。

本文主要查看了 iOS 10 的相關(guān)文檔,整理出了在 iOS 10 下的本地推送通知,由于都是代碼,就不多做講解,直接看代碼及注釋,有問題留言討論哦。

新的推送注冊(cè)機(jī)制

注冊(cè)通知( 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];
 //監(jiān)聽回調(diào)事件
 center.delegate = self;
 
 //iOS 10 使用以下方法注冊(cè),才能得到授權(quán)
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
       completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
       }];
 
 //獲取當(dāng)前的通知設(shè)置,UNNotificationSettings 是只讀對(duì)象,不能直接修改,只能通過以下方法獲取
 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  
 }];
 return YES;
}

#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前進(jìn)行處理,即有機(jī)會(huì)在展示通知前再修改通知內(nèi)容。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 //1. 處理通知
 
 //2. 處理完成后條用 completionHandler ,用于指示在前臺(tái)顯示通知的形式
 completionHandler(UNNotificationPresentationOptionAlert);
}
@end

推送本地通知

//使用 UNNotification 本地通知
+(void)registerNotification:(NSInteger )alerTime{
 
 // 使用 UNUserNotificationCenter 來管理通知
 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
 
 //需創(chuàng)建一個(gè)包含待通知內(nèi)容的 UNMutableNotificationContent 對(duì)象,注意不是 UNNotificationContent ,此對(duì)象為不可變對(duì)象。
 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后,需要添加這個(gè)注冊(cè),才能得到授權(quán)
 // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
 // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
 // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
 // categories:nil];
 // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 // // 通知重復(fù)提示的單位,可以是天、周、月
 // }
 
 UILocalNotification *notification = [[UILocalNotification alloc] init];
 // 設(shè)置觸發(fā)通知的時(shí)間
 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
 NSLog(@"fireDate=%@",fireDate);
 
 notification.fireDate = fireDate;
 // 時(shí)區(qū)
 notification.timeZone = [NSTimeZone defaultTimeZone];
 // 設(shè)置重復(fù)的間隔
 notification.repeatInterval = kCFCalendarUnitSecond;
 
 // 通知內(nèi)容
 notification.alertBody = @"該起床了...";
 notification.applicationIconBadgeNumber = 1;
 // 通知被觸發(fā)時(shí)播放的聲音
 notification.soundName = UILocalNotificationDefaultSoundName;
 // 通知參數(shù)
 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學(xué)習(xí)iOS開發(fā)了" forKey:@"key"];
 notification.userInfo = userDict;
 
 // ios8后,需要添加這個(gè)注冊(cè),才能得到授權(quán)
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                     categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  // 通知重復(fù)提示的單位,可以是天、周、月
  notification.repeatInterval = NSCalendarUnitDay;
 } else {
  // 通知重復(fù)提示的單位,可以是天、周、月
  notification.repeatInterval = NSDayCalendarUnit;
 }
 
 // 執(zhí)行通知注冊(cè)
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

本文已被整理到了《iOS推送教程》,歡迎大家學(xué)習(xí)閱讀。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    在IOS開法中經(jīng)常會(huì)遇到鍵盤遮擋屏幕的事情,經(jīng)常檔住下面的按鈕,下面小編給大家分享IOS開發(fā)中鍵盤輸入屏幕上移的解決方法,感興趣的朋友一起看看吧
    2016-10-10
  • iOS中金額字符串格式化顯示的方法示例

    iOS中金額字符串格式化顯示的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS中金額字符串格式化顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • iOS自定義控件開發(fā)梳理總結(jié)

    iOS自定義控件開發(fā)梳理總結(jié)

    這篇文章主要介紹了iOS自定義控件開發(fā)梳理總結(jié),自定義控件能讓我們完全控制視圖的展示內(nèi)容以及交互操作。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • ios 服務(wù)器端推送證書生成的方法

    ios 服務(wù)器端推送證書生成的方法

    這篇文章主要介紹了ios 服務(wù)器端推送證書生成的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼

    IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼

    這篇文章主要介紹了IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • 詳解iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的抽象工廠模式

    詳解iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的抽象工廠模式

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的抽象工廠模式,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03
  • iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究

    iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究

    這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究,這里用到了一個(gè)Method Swizzling原理,需要的朋友可以參考下
    2015-10-10
  • 判斷iOS應(yīng)用是否開放HTTP權(quán)限的方法

    判斷iOS應(yīng)用是否開放HTTP權(quán)限的方法

    這篇文章主要為大家詳細(xì)介紹了判斷iOS應(yīng)用是否開放HTTP權(quán)限的方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS中CPU線程調(diào)試的高級(jí)技巧分享

    iOS中CPU線程調(diào)試的高級(jí)技巧分享

    這篇文章主要給大家介紹了關(guān)于iOS中CPU線程調(diào)試的高級(jí)技巧,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • iOS開發(fā)教程之XLForm的基本使用方法

    iOS開發(fā)教程之XLForm的基本使用方法

    XLForm 是最靈活且最強(qiáng)大的創(chuàng)建動(dòng)態(tài)表單的iOS庫,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之XLForm的基本使用方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04

最新評(píng)論

恩平市| 定西市| 扶沟县| 华宁县| 资溪县| 泊头市| 濉溪县| 大埔区| 辽源市| 亳州市| 沙洋县| 禹州市| 菏泽市| 昆山市| 丰镇市| 阜康市| 福贡县| 什邡市| 武穴市| 赤壁市| 怀宁县| 墨玉县| 伊吾县| 惠水县| 浦县| 闽清县| 常山县| 灯塔市| 班戈县| 上高县| 怀宁县| 保靖县| 海安县| 海口市| 景东| 神农架林区| 土默特左旗| 忻州市| 中牟县| 镇远县| 榆社县|