欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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

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

#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前進行處理,即有機會在展示通知前再修改通知內(nèi)容。
-(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];
 
 //需創(chuàng)建一個包含待通知內(nèi)容的 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后,需要添加這個注冊,才能得到授權(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ā)通知的時間
 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
 NSLog(@"fireDate=%@",fireDate);
 
 notification.fireDate = fireDate;
 // 時區(qū)
 notification.timeZone = [NSTimeZone defaultTimeZone];
 // 設(shè)置重復(fù)的間隔
 notification.repeatInterval = kCFCalendarUnitSecond;
 
 // 通知內(nèi)容
 notification.alertBody = @"該起床了...";
 notification.applicationIconBadgeNumber = 1;
 // 通知被觸發(fā)時播放的聲音
 notification.soundName = UILocalNotificationDefaultSoundName;
 // 通知參數(shù)
 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學(xué)習(xí)iOS開發(fā)了" forKey:@"key"];
 notification.userInfo = userDict;
 
 // ios8后,需要添加這個注冊,才能得到授權(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í)行通知注冊
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

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

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

相關(guān)文章

  • iOS基于UIScrollView實現(xiàn)滑動引導(dǎo)頁

    iOS基于UIScrollView實現(xiàn)滑動引導(dǎo)頁

    這篇文章主要為大家詳細介紹了iOS基于UIScrollView實現(xiàn)滑動引導(dǎo)頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS開發(fā)中仿Tumblr點贊心破碎動畫效果

    iOS開發(fā)中仿Tumblr點贊心破碎動畫效果

    這篇文章主要介紹了iOS開發(fā)中仿Tumblr點贊心破碎動畫效果,本文圖文并茂給大家介紹的非常詳細,需要的朋友可以參考下
    2017-04-04
  • iOS10適配之權(quán)限Crash問題的完美解決方案

    iOS10適配之權(quán)限Crash問題的完美解決方案

    這篇文章主要為大家詳細介紹了iOS10適配之權(quán)限Crash問題的完美解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • IOS獲取指定年月的當(dāng)月天數(shù)

    IOS獲取指定年月的當(dāng)月天數(shù)

    本文通過實例演示在IOS開發(fā)中如何獲取指定年月的當(dāng)月天數(shù),有需要的小伙伴們可以參考借鑒。
    2016-08-08
  • ios開發(fā):一個音樂播放器的設(shè)計與實現(xiàn)案例

    ios開發(fā):一個音樂播放器的設(shè)計與實現(xiàn)案例

    本篇文章主要介紹了ios開發(fā):一個音樂播放器的設(shè)計與實現(xiàn)案例,具有一定的參考價值,有需要的小伙伴可以參考下。
    2016-11-11
  • IOS 中NSTimer定時器的使用

    IOS 中NSTimer定時器的使用

    這篇文章主要介紹了IOS 中NSTimer定時器的使用的相關(guān)資料,希望通過本文能幫助到大家,能讓大家徹底理解使用該方法,需要的朋友可以參考下
    2017-10-10
  • iOS實現(xiàn)底部彈出PopupWindow效果 iOS改變背景透明效果

    iOS實現(xiàn)底部彈出PopupWindow效果 iOS改變背景透明效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)底部彈出PopupWindow效果,iOS改變背景透明效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 實例分析IOS實現(xiàn)自動打包

    實例分析IOS實現(xiàn)自動打包

    本篇文章給大家分享了IOS實現(xiàn)自動打包的相關(guān)知識點,以及需要的操作內(nèi)容做了分享,有需要的朋友可以學(xué)習(xí)下。
    2018-05-05
  • Objective-C?入門篇(推薦)

    Objective-C?入門篇(推薦)

    由C語言和Smalltalk擴展出來的,是C語言的超集,最大的區(qū)別是OC是面向?qū)ο蟮?,其火星文寫法對于之前從事Java開發(fā)的同學(xué)頗感蛋疼,OC最大特點之一是使用消息結(jié)構(gòu)而不是函數(shù)調(diào)用
    2021-11-11
  • iOS按比例實現(xiàn)方塊圖

    iOS按比例實現(xiàn)方塊圖

    這篇文章主要為大家詳細介紹了iOS按比例實現(xiàn)方塊圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評論