1、iOS8引入了地點(diǎn)通知(location notifications)。它其實(shí)也是本地通知(local notifications),但是他們只會(huì)在用戶一個(gè)特定的地理或者iBeacon區(qū)域時(shí),才會(huì)被觸發(fā)。雖然我們看不到什么細(xì)節(jié),地點(diǎn)通知 (location notifications)實(shí)現(xiàn)起來(lái)也很容易。
2、從iOS8開(kāi)始,通知被加入了新的特性。簡(jiǎn)單地說(shuō),從現(xiàn)在開(kāi)始,當(dāng)一個(gè)通知被展示時(shí),開(kāi)發(fā)者可以指定用戶可觸發(fā)的具體的動(dòng)作(actions),而且甚至不用啟動(dòng)App也可以處理這個(gè)通知。動(dòng)作(Actions)可以被歸類到一個(gè)類目(categories)下。特別是當(dāng)App安排了不少通知顯示的時(shí)候相當(dāng)方便。用類目 (categories),一個(gè)通知所有相關(guān)的動(dòng)作(actions)都可以一次性的被捆綁和指定。反之,處理動(dòng)作(actions)也非常簡(jiǎn)單,只需要實(shí)現(xiàn)其代理方法即可。每一個(gè)動(dòng)作(actions)都有一個(gè)特殊的屬性標(biāo)示符(identifier),被App用來(lái)辨別收到的動(dòng)作(actions)然后適當(dāng)?shù)靥幚硭?/p>
3、可以被安排的本地通知的數(shù)量并不是無(wú)限的,最多有64個(gè)本地通知可以被安排和展示。如果多余這個(gè)數(shù)字,所有超過(guò)這個(gè)數(shù)字的通知都會(huì)被廢棄。盡管如此,無(wú)論通知是以什么樣的形式被安排的,最早的那個(gè)會(huì)被最先展示。
4、具體代碼:
在ViewController.m中添加代碼:
//創(chuàng)建通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//滑動(dòng)提醒
localNotification.alertAction = @"查看消息";
localNotification.hasAction = YES;
//提醒內(nèi)容
localNotification.alertBody = @"推送消息";
//category的identifier
localNotification.category = @"COMPLETE_CATEGORY";
//推送聲音
localNotification.soundName = nil;
//觸發(fā)時(shí)間
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
//應(yīng)用程序右上角圖標(biāo)標(biāo)記
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
在AppDelegate.m中添加代碼:
在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加
UIMutableUserNotificationAction *notificationActionOk = [[UIMutableUserNotificationAction alloc]init];
notificationActionOk.identifier = @"completeRemindRater";
notificationActionOk.title = @"再工作一會(huì)兒";
//是否取消提醒
notificationActionOk.destructive = NO;
//是否需要權(quán)限,例如鎖屏的時(shí)候,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionOk.authenticationRequired = NO;
//啟動(dòng)app還是后臺(tái)執(zhí)行
notificationActionOk.activationMode = UIUserNotificationActivationModeBackground;
notificationActionOk.behavior = UIUserNotificationActionBehaviorDefault;
UIMutableUserNotificationAction *notificationActionRestNow = [[UIMutableUserNotificationAction alloc]init];
notificationActionRestNow.identifier = @"relaxNow";
notificationActionRestNow.title = @"休息";
//是否取消提醒? 當(dāng)設(shè)置為yes時(shí),通知中相應(yīng)地按鈕的背景色會(huì)變成紅色
notificationActionRestNow.destructive = YES;
//是否需要權(quán)限,例如鎖屏的時(shí)候,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionRestNow.authenticationRequired = NO;
//啟動(dòng)app還是后臺(tái)執(zhí)行
notificationActionRestNow.activationMode = UIUserNotificationActivationModeBackground;
notificationActionRestNow.behavior = UIUserNotificationActionBehaviorDefault;
UIMutableUserNotificationCategory *notificationCompleteCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCompleteCategory.identifier = @"COMPLETE_CATEGORY";
[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextMinimal];
[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextDefault];
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
replyAction.title = @"回復(fù)";
replyAction.identifier = @"inline-reply";
replyAction.activationMode = UIUserNotificationActivationModeBackground;
replyAction.authenticationRequired = NO;
replyAction.behavior = UIUserNotificationActionBehaviorTextInput;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"REPLY_CATEGORY";
[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextMinimal];
[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge categories:[NSSet setWithArray:@[notificationCompleteCategory,notificationCategory]]]];
實(shí)現(xiàn)協(xié)議方法:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
if ([identifier isEqualToString:@"completeRemindRater"]) {
NSLog(@"點(diǎn)擊的是%@",identifier);
}
if ([identifier isEqualToString:@"relaxNow"]) {
NSLog(@"點(diǎn)擊的是%@",identifier);
}
if ([identifier isEqualToString:@"inline-reply"]) {
NSLog(@"點(diǎn)擊的是%@",identifier);
}
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"接收到通知");
}
參考鏈接: