項目中用到了本地通知功能珠增,便到網(wǎng)上搜羅了一翻,各式各樣的說法葬馋,經(jīng)過自己的驗證卖鲤,親測可用, 整理如下
首先要說明的是IOS最多允許最近本地通知數(shù)量是64個,超過限制的本地通知將被忽略畴嘶,經(jīng)過測試蛋逾,本地通知根本沒有限制,可能以前有限制吧窗悯,現(xiàn)在已經(jīng)不限制數(shù)量了区匣。
其次是在iOS8以后需要先注冊才能發(fā)送通知,所以在使用本地通知功能以前需要先注冊蒋院。
下面說說用法:
1.注冊通知(iOS8以后)
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加注冊代碼
if([[UIDevice currentDevice].systemVersion doubleValue]>=8.0){//8.0以后使用這種方法來注冊推送通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}else{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
2.構建UILocalNotification
UILocalNotification *notification = [[UILocalNotification alloc] init];
3.配置UILocalNotification
3.1屬性
-
1.觸發(fā)時間(fireDate亏钩,timeZone,repeatInterval(重復方式)欺旧,repeatCalendar)//如果發(fā)送通知方式為即時發(fā)送則配置無意義
//10秒以后通知
NSDate * now = [[NSDate alloc] init];
notification.fireDate=[now dateByAddingTimeInterval:10];//使用本地時區(qū)
notification.timeZone=[NSTimeZone defaultTimeZone];// 設置重復的方式
notification.repeatInterval = kCFCalendarUnitSecond; 2.通知行為(alertAction,hasAction)
notification.alertAction=NSLocalizedString(@"鎖屏啦姑丑,通知時間到啦", nil);3.觸發(fā)通知時的啟動畫面(alertLaunchImage)
4.通知的正文內容(alertTitle、alertBody)
notification.alertBody=@"通知內容";5.通知的聲音(soundName)
notification.soundName = UILocalNotificationDefaultSoundName;6.通知消息數(shù)的展示(applicationIconBadgeNumber)App圖標左上角的紅數(shù)字
notification.applicationIconBadgeNumber = 1;//不顯示為0-
7.其它(userInfo)辞友,可以給通知綁定一些處理通知時需要的額外信息栅哀。
給這個通知增加key 便于取消震肮。NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],@"akey",nil]; [notification setUserInfo:dict];
3.2發(fā)送方式
- (void)presentLocalNotificationNow:(UILocalNotification *)notification//立即發(fā)送
- (void)scheduleLocalNotification:(UILocalNotification *)notification//按配置時間發(fā)送
3.3取消方式
-- (void)cancelLocalNotification:(UILocalNotification *)notification//取消某個通知
-- (void)cancelAllLocalNotifications//取消所有通知
// 獲取所有本地通知數(shù)組
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根據(jù)設置通知參數(shù)時指定的key來獲取通知參數(shù)
NSString *info = userInfo[@"akey"];
// 如果找到需要取消的通知,則取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
4.處理通知
在AppDelegate.m文件中- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法添加
// 獲取通知所帶的數(shù)據(jù)
NSString *message = [notification.userInfo objectForKey:@"akey"];
//可按需求進行數(shù)據(jù)處理
NSLog(@"%@",message);
// 更新顯示的消息個數(shù)
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;//讀了一個留拾,所以減1
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;//本人最討厭這個小紅點戳晌。。痴柔。
一般在進入App后應當清除小紅點躬厌,在- (void)applicationDidBecomeActive:(UIApplication *)application方法中,添加一句[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];即可。
到這里就全部結束了竞帽,是不是很簡單呢扛施?其實我們平時用它也就進行一個類似彈框的提醒功能,注冊屹篓、發(fā)送疙渣,幾句代碼就解決了。堆巧。妄荔。
注意
1.iOS系統(tǒng)沒有自定義時間間隔的通知,如果要實現(xiàn)類似功能需要注冊多個通知
2.如果重復次數(shù)為0的話谍肤,通知了一次這個通知就會從系統(tǒng)消除
3.如果你的通知沒有消除啦租,即使卸載了程序,這依然會殘留荒揣,在下次裝入的時候會繼續(xù)運行篷角,如果想要移除本地通知可以調用UIApplication的cancelLocalNotification:或cancelAllLocalNotifications移除指定通知或所有通知
4.在使用通知之前必須注冊通知類型,如果用戶不允許應用程序發(fā)送通知系任,則以后就無法發(fā)送通知恳蹲,除非用戶手動到iOS設置中打開通知
5.通知的聲音是由iOS系統(tǒng)播放的,格式必須是Linear PCM俩滥、MA4(IMA/ADPCM)嘉蕾、μLaw、aLaw中的一種霜旧,并且播放時間必須在30s內错忱,否則將被系統(tǒng)聲音替換,同時自定義聲音文件必須放到main boundle中
6.本地通知的數(shù)量是有限制的挂据,最近的本地通知最多只能有64個以清,超過這個數(shù)量將被系統(tǒng)忽略