輕鬆搞定iOS本地訊息推送_IOS

來源:互聯網
上載者:User

首先,我們先要明白一個概念,這裡的本地通知是UILocalNotification類,和系統的NSNotificationCenter通知中樞是完全不同的概念。

一、我們可以通過本地通知做什麼

通知,實際上是由IOS系統管理的一個功能,比如某些後台應用做了某項活動需要我們處理、已經退出的應用在某個時間提醒我們喚起等等,如果註冊了通知,系統都會在通知觸發時給我們發送訊息。由此,我們可以通過系統給我們的APP添加通知使用者的功能,並且應用非常廣泛。例如,鬧種類應用,有按時簽到相似功能的應用。下面,我們就來介紹如何註冊並且設定一個本地通知。

二、瞭解UILocalNotification類

顧名思義,這個類就是我們需要使用的本地通知類,先來看它的幾個屬性:

設定系統發送通知的時間(如果是過去的時間或者0,則會立刻發起通知) 

@property(nonatomic,copy) NSDate *fireDate;

設定時間的時區 

@property(nonatomic,copy) NSTimeZone *timeZone;

設定周期性通知 

@property(nonatomic) NSCalendarUnit repeatInterval;

NSCalendarUnit對象是枚舉,設定通知的周期

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) { NSCalendarUnitEra  = kCFCalendarUnitEra, NSCalendarUnitYear  = kCFCalendarUnitYear, NSCalendarUnitMonth  = kCFCalendarUnitMonth, NSCalendarUnitDay  = kCFCalendarUnitDay, NSCalendarUnitHour  = kCFCalendarUnitHour, NSCalendarUnitMinute  = kCFCalendarUnitMinute, NSCalendarUnitSecond  = kCFCalendarUnitSecond, NSCalendarUnitWeekday  = kCFCalendarUnitWeekday, NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal, }

設定周期性通知參照的日曆表 

@property(nonatomic,copy) NSCalendar *repeatCalendar;

下面這兩個函數是IOS8的新功能,在使用者進去或者離開某一地區時發送通知 

@property(nonatomic,copy) CLRegion *region;

設定地區檢測通知是否重複(如果為YES,則沒次進去出來都會發送,否則只發送一次)

 @property(nonatomic,assign) BOOL regionTriggersOnce; 

設定通知的主體內容 

@property(nonatomic,copy) NSString *alertBody; 

是否隱藏滑動啟動按鈕 

@property(nonatomic) BOOL hasAction;

設定滑動開啟的提示文字 

@property(nonatomic,copy) NSString *alertAction;

設定點擊通知後啟動的啟動圖片 

@property(nonatomic,copy) NSString *alertLaunchImage;

下面這個方法是IOS8的新方法,是iwatch的介面,通知的短標題 

@property(nonatomic,copy) NSString *alertTitle;

收到通知時,播放的系統音 

@property(nonatomic,copy) NSString *soundName;

設定應用程式Icon頭標數字

@property(nonatomic) NSInteger applicationIconBadgeNumber;

使用者字典,可用於傳遞通知訊息參數 

@property(nonatomic,copy) NSDictionary *userInfo;

注意:這個字串是系統預設的提示音 

NSString *const UILocalNotificationDefaultSoundName; 

三、本地通知的設計流程

首先,想讓我們的APP實現本地通知功能,必須得到使用者的授權,在Appdelegate中實現如下代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //如果已經得到授權,就直接添加本地通知,否則申請詢問授權 if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) { [self addLocalNotification]; }else{ [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; } return YES;}

當使用者點擊允許或者不允許後,會執行如下代理方法,我們把處理邏輯在其中實現

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ if (notificationSettings.types!=UIUserNotificationTypeNone) { [self addLocalNotification]; }}
 

添加本地通知的方法:

-(void)addLocalNotification{ //定義本地通知對象 UILocalNotification *notification=[[UILocalNotification alloc]init]; //設定調用時間 notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:0];//立即觸發 //設定通知屬性 notification.alertBody=@"HELLO,我是本地通知哦!"; //通知主體 notification.applicationIconBadgeNumber=1;//應用程式圖示右上方顯示的訊息數 notification.alertAction=@"開啟應用"; //待機介面的滑動動作提示  notification.soundName=UILocalNotificationDefaultSoundName;//收到通知時播放的聲音,預設訊息聲音 //調用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification];}

實現了上面三個步驟,本地通知的發出和接受基本都已完成,還有一些細節我們需要考慮:

應用進入前台後,將Icon上的頭標清除:

-(void)applicationWillEnterForeground:(UIApplication *)application{ [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//進入前台取消應用訊息表徵圖}

 當不再需要這個通知時,清除它

 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

四、擷取通知中的使用者參數字典

在上面,我們提到了一個參數

@property(nonatomic,copy) NSDictionary *userInfo;

我們可以在註冊通知時將這個參數設定,然後在收到通知時使用get方法得到,但是這裡有兩種情況:

1、如果我們的APP在前台或者後台進入前台時 

複製代碼 代碼如下:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

這個方法是APP在前台或者後台收到通知進入前台時調用的方法

2、如果我們的APP在關閉狀態

如果是這種情況,我們只能從下面函數的launchOptions中取到我們想要的參數

複製代碼 代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

程式碼範例如下:

 //接收通知參數  UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSDictionary *userInfo= notification.userInfo;

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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.