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

如果寫的還行,記得點贊哦

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 作者雷潮關注 2016.02.01 00:18*字數 1921閱讀 7038評論 1喜歡 35 這里是指推送通知跟...
    對面來個小胖子閱讀 328評論 0 1
  • 這是我寫的第一篇簡書文章,寫的不好,希望大家多多指導,多多交流. iOS的本地通知,多用于定時發送通知,比如游戲中...
    FR_Zhang閱讀 1,226評論 0 1
  • 許多集成的步驟個推官網都有了,這里只寫關于推送的遠程推送和本地通知的步驟和代碼。APP在后臺時:走蘋果的APNS通...
    AllureJM閱讀 2,771評論 1 9
  • 這里是指推送通知跟NSNotification有區別: 1、NSNotification是系統內部發出通知,一般用...
    元宇宙協會閱讀 17,315評論 2 55
  • 女人說,在我身邊的不是我最愛的人,內心總有那么一個人,僅此或取代自己的男人 前男友總會說,只要你過得好,過的開心,...
    墨二少閱讀 192評論 0 0