iOS開(kāi)發(fā)-本地通知

這是我寫(xiě)的第一篇簡(jiǎn)書(shū)文章,寫(xiě)的不好,希望大家多多指導(dǎo),多多交流.

iOS的本地通知,多用于定時(shí)發(fā)送通知,比如游戲中常見(jiàn)的中午十二點(diǎn)的體力領(lǐng)取的通知,吃藥APP的定時(shí)提醒等等,例子不多舉了,總之,就是根據(jù)大家的需求,根據(jù)具體的特定的時(shí)間段,APP自動(dòng)以iOS系統(tǒng)的通知的形式發(fā)送通知.

下面就iOS本地通知做出詳細(xì)的說(shuō)明:

注:本地通知作為一個(gè)重要的模塊,這里創(chuàng)建一個(gè)本地通知的管理類(lèi):LocalNotificationManager.因?yàn)椴糠諥PP有寫(xiě)成單例類(lèi)的需求,在程序中,添加了單例類(lèi)的方法(多線(xiàn)程創(chuàng)建方式),僅供參考,主要還是以類(lèi)方法執(zhí)行操作:

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

1.注冊(cè)本地通知
在iOS8之后,以前的本地推送寫(xiě)法可能會(huì)出錯(cuò),接收不到推送的信息,
如果出現(xiàn)以下信息:
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

因此本片文章主要針對(duì)iOS8之后做出的說(shuō)明.
首先判斷當(dāng)前用戶(hù)對(duì)APP的通知權(quán)限,如果是首次運(yùn)行軟件,則會(huì)出現(xiàn)如下圖的提示,這個(gè)是iOS系統(tǒng)自帶的提示選擇方式,相信每個(gè)iOS開(kāi)發(fā)的程序員都知道的,具體操作,我就不多做解釋了.

在注冊(cè)通知權(quán)限的情況,主要是iOS8之后版本的設(shè)置:

+ (void)registLocalNotification {
//創(chuàng)建本地通知對(duì)象
UILocalNotification * localNotif = [[UILocalNotification alloc]init];
//判斷本地通知中是否已經(jīng)注冊(cè)過(guò)通知了
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// 通知的類(lèi)型,設(shè)置聲音,彈框等等......
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//執(zhí)行通知注冊(cè)
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重復(fù)提示的單位,可以是天、周、月
localNotif.repeatInterval = NSCalendarUnitDay;
}else {
// 通知重復(fù)提示的單位,可以是天、周、月
localNotif.repeatInterval = NSDayCalendarUnit;
}
}

2.設(shè)置通知的重要參數(shù)
這里主要是本地通知的參數(shù)設(shè)置,主要包括內(nèi)容,觸發(fā)時(shí)間等等

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

3.本地通知的實(shí)現(xiàn)
這里測(cè)試系統(tǒng)的可行性,我們可以通過(guò)本地通知與NSTimer結(jié)合,從而更形象化的進(jìn)行說(shuō)明.
1).首先注冊(cè)通知:

[LocalNotificationManager registLocalNotification];

2).點(diǎn)擊事件觸發(fā)NSTimer的倒計(jì)時(shí),倒計(jì)時(shí)結(jié)束后,執(zhí)行本地通知

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

3).NSTimer執(zhí)行的方法(這里是以秒為單位,每秒執(zhí)行)

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

彈框的示例圖:


后臺(tái)運(yùn)行,發(fā)送通知的樣式:


運(yùn)行APP的樣式:

目前為止,發(fā)送通知的基本內(nèi)容就如上所講,至于豐富內(nèi)容,大家自己擴(kuò)展.
4.查看通知具體內(nèi)容
這部分也是最重要的部分,也是比較容易忽略的部分,猶如這個(gè)涉及到APP的運(yùn)行狀態(tài),所以,這里需要在APPDelegate中進(jìn)行設(shè)置
1).首先在發(fā)送通知的同時(shí),在APP上面會(huì)出現(xiàn)強(qiáng)迫癥最討厭的小1的標(biāo)志,所以,我們首先應(yīng)該先消除提醒個(gè)數(shù),

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

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

- (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];
// 更新顯示的徽章個(gè)數(shù)
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
// 在不需要再推送時(shí),可以取消推送
[LocalNotificationManager cancelLocalNotificationWithKey:@"key"];
}

Demo演示地址:
https://github.com/zhangfurun/FRLocalNotificationDemo.git
如果寫(xiě)的還行,記得點(diǎn)贊哦

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容