項目中用到了本地通知功能隙笆,便到網(wǎng)上搜羅了一翻,各式各樣的說法稠项,經(jīng)過自己的驗(yàn)證涯雅,整理如下
首先要說明的是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.構(gòu)建UILocalNotification
UILocalNotification *notification = [[UILocalNotification alloc] init];
3.配置UILocalNotification
3.1屬性
- 觸發(fā)時間(fireDate锈遥,timeZone纫事,repeatInterval(重復(fù)方式),repeatCalendar)//如果發(fā)送通知方式為即時發(fā)送則配置無意義
//10秒以后通知
NSDate * now = [[NSDate alloc] init];
notification.fireDate=[now dateByAddingTimeInterval:10];
//使用本地時區(qū)
notification.timeZone=[NSTimeZone defaultTimeZone];
// 設(shè)置重復(fù)的方式
notification.repeatInterval = kCFCalendarUnitSecond;
- 通知行為(alertAction,hasAction)
notification.alertAction=NSLocalizedString(@"鎖屏啦所灸,通知時間到啦", nil);
- 觸發(fā)通知時的啟動畫面(alertLaunchImage)
- 通知的正文內(nèi)容(alertTitle丽惶、alertBody)
notification.alertBody=@"通知內(nèi)容";
- 通知的聲音(soundName)
notification.soundName = UILocalNotificationDefaultSoundName;
- 通知消息數(shù)的展示(applicationIconBadgeNumber)App圖標(biāo)左上角的紅數(shù)字
notification.applicationIconBadgeNumber = 1;
//不顯示為0 - 其它(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è)置通知參數(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"];
//可按需求進(jìn)行數(shù)據(jù)處理
NSLog(@"%@",message);
// 更新顯示的消息個數(shù)
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;//讀了一個侠驯,所以減1
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;//本人最討厭這個小紅點(diǎn)抡秆。。陵霉。
一般在進(jìn)入App后應(yīng)當(dāng)清除小紅點(diǎn)琅轧,在- (void)applicationDidBecomeActive:(UIApplication *)application
方法中,添加一句[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
即可。
到這里就全部結(jié)束了踊挠,是不是很簡單呢乍桂?其實(shí)我們平時用它也就進(jìn)行一個類似彈框的提醒功能,注冊效床、發(fā)送睹酌,幾句代碼就解決了。剩檀。憋沿。