iOS開發--本地通知

這是我寫的第一篇簡書文章,寫的不好,希望大家多多指導,多多交流.

iOS的本地通知,多用於定時發送通知,比如遊戲中常見的中午十二點的體力領取的通知,吃藥APP的定時提醒等等,例子不多舉了,總之,就是根據大家的需求,根據具體的特定的時間段,APP自動以iOS系統的通知的形式發送通知.

下面就iOS本地通知做出詳細的說明:

注:本地通知作爲一個重要的模塊,這裏創建一個本地通知的管理類:LocalNotificationManager.因爲部分APP有寫成單例類的需求,在程序中,添加了單例類的方法(多線程創建方式),僅供參考,主要還是以類方法執行操作:

 static LocalNotificationManager * shareManager = nil;
 + (LocalNotificationManager *)shareMG {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[LocalNotificationManager alloc]init];
    });
    return shareManager;
}

1.註冊本地通知
在iOS8之後,以前的本地推送寫法可能會出錯,接收不到推送的信息,
如果出現以下信息:
1 Attempting to schedule a local notification
2 with an alert but haven’t received permission from the user to display alerts
3 with a sound but haven’t received permission from the user to play sounds

因此本片文章主要針對iOS8之後做出的說明.
首先判斷當前用戶對APP的通知權限,如果是首次運行軟件,則會出現如下圖的提示,這個是iOS系統自帶的提示選擇方式,相信每個iOS開發的程序員都知道的,具體操作,我就不多做解釋了.

在註冊通知權限的情況,主要是iOS8之後版本的設置:

+ (void)registLocalNotification {
//創建本地通知對象
UILocalNotification * localNotif = [[UILocalNotification alloc]init];
//判斷本地通知中是否已經註冊過通知了
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// 通知的類型,設置聲音,彈框等等......
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//執行通知註冊
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重複提示的單位,可以是天、周、月
localNotif.repeatInterval = NSCalendarUnitDay;
}else {
// 通知重複提示的單位,可以是天、周、月
localNotif.repeatInterval = NSDayCalendarUnit;
}
}

2.設置通知的重要參數
這裏主要是本地通知的參數設置,主要包括內容,觸發時間等等

+ (void)setLocalNotificationWithAlertBody:(NSString *)alertBody alertTime:(NSInteger)alertTime noticeStr:(NSString *)str {
UILocalNotification * localNotification = [[UILocalNotification alloc]init];
//設置出發通知的時間
NSDate * date = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"---%@", date);
localNotification.fireDate = date;
// 設置時區
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// 設置重複的間隔
localNotification.repeatInterval = kCFCalendarUnitSecond;
// 設置通知的內容
localNotification.alertBody = alertBody;
localNotification.applicationIconBadgeNumber = 1;
// 通知時播放聲音
localNotification.soundName = UILocalNotificationDefaultSoundName;
// 通知參數,將內容通過通知攜帶
NSDictionary * dic = [NSDictionary dictionaryWithObject:str forKey:@"localNotification"];
localNotification.userInfo = dic;
// 將通知添加到系統中
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

3.本地通知的實現
這裏測試系統的可行性,我們可以通過本地通知與NSTimer結合,從而更形象化的進行說明.
1).首先註冊通知:

[LocalNotificationManager registLocalNotification];

2).點擊事件觸發NSTimer的倒計時,倒計時結束後,執行本地通知

- (IBAction)sendLocalNotification:(UIButton *)sender {
//設置倒計時的總時間
timeNumber = 5;
//設置倒計時
NSTimer * timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reloadTimeBtn:) userInfo:nil repeats:YES];
//倒計時執行
[timer fire];
}

3).NSTimer執行的方法(這裏是以秒爲單位,每秒執行)

- (void)reloadTimeBtn:(NSTimer *)sender {
[self.button setTitle:[NSString stringWithFormat:@"%ld", timeNumber] forState:UIControlStateNormal];
if (timeNumber < 0) {
//time爲0的時候執行通知
[self.button setTitle:[NSString stringWithFormat:@"完成"] forState:UIControlStateNormal];
[sender invalidate];
//執行通知,設置參數
//body  彈框的標題
//noticeStr  彈框的主要內容
[LocalNotificationManager setLocalNotificationWithAlertBody:@"爆炸啦" alertTime:0 noticeStr:@"Boom!!沙卡拉卡"];
}
timeNumber--;
}

彈框的示例圖:

後臺運行,發送通知的樣式:

運行APP的樣式:

目前爲止,發送通知的基本內容就如上所講,至於豐富內容,大家自己擴展.
4.查看通知具體內容
這部分也是最重要的部分,也是比較容易忽略的部分,猶如這個涉及到APP的運行狀態,所以,這裏需要在APPDelegate中進行設置
1).首先在發送通知的同時,在APP上面會出現強迫症最討厭的小1的標誌,所以,我們首先應該先消除提醒個數,

- (void)applicationDidBecomeActive:(UIApplication *)application {
//清空提醒的個數
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

2).在App的代理中,在didReceiveLocalNotification中執行方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//獲取通知信息
NSString * messageNoti = [notification.userInfo objectForKey:@"localNotification"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"爆炸啦" message:messageNoti delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
// 更新顯示的徽章個數
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
// 在不需要再推送時,可以取消推送
[LocalNotificationManager cancelLocalNotificationWithKey:@"key"];
}

Demo演示地址:
https://github.com/zhangfurun/FRLocalNotificationDemo.git
如果寫的還行,記得點贊哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章