1渐裂、iOS8引入了地點通知(location notifications)繁疤。它其實也是本地通知(local notifications),但是他們只會在用戶一個特定的地理或者iBeacon區(qū)域時,才會被觸發(fā)揣炕。雖然我們看不到什么細(xì)節(jié)帘皿,地點通知 (location notifications)實現(xiàn)起來也很容易。
2畸陡、從iOS8開始鹰溜,通知被加入了新的特性。簡單地說丁恭,從現(xiàn)在開始曹动,當(dāng)一個通知被展示時,開發(fā)者可以指定用戶可觸發(fā)的具體的動作(actions)牲览,而且甚至不用啟動App也可以處理這個通知墓陈。動作(Actions)可以被歸類到一個類目(categories)下。特別是當(dāng)App安排了不少通知顯示的時候相當(dāng)方便竭恬。用類目 (categories)跛蛋,一個通知所有相關(guān)的動作(actions)都可以一次性的被捆綁和指定。反之痊硕,處理動作(actions)也非常簡單赊级,只需要實現(xiàn)其代理方法即可。每一個動作(actions)都有一個特殊的屬性標(biāo)示符(identifier)岔绸,被App用來辨別收到的動作(actions)然后適當(dāng)?shù)靥幚硭?/p>
3理逊、可以被安排的本地通知的數(shù)量并不是無限的,最多有64個本地通知可以被安排和展示盒揉。如果多余這個數(shù)字晋被,所有超過這個數(shù)字的通知都會被廢棄。盡管如此刚盈,無論通知是以什么樣的形式被安排的羡洛,最早的那個會被最先展示。
4藕漱、具體代碼:
在ViewController.m中添加代碼:
//創(chuàng)建通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//滑動提醒
localNotification.alertAction = @"查看消息";
localNotification.hasAction = YES;
//提醒內(nèi)容
localNotification.alertBody = @"推送消息";
//category的identifier
localNotification.category = @"COMPLETE_CATEGORY";
//推送聲音
localNotification.soundName = nil;
//觸發(fā)時間
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 = @"再工作一會兒";
//是否取消提醒
notificationActionOk.destructive = NO;
//是否需要權(quán)限欲侮,例如鎖屏的時候崭闲,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionOk.authenticationRequired = NO;
//啟動app還是后臺執(zhí)行
notificationActionOk.activationMode = UIUserNotificationActivationModeBackground;
notificationActionOk.behavior = UIUserNotificationActionBehaviorDefault;
UIMutableUserNotificationAction *notificationActionRestNow = [[UIMutableUserNotificationAction alloc]init];
notificationActionRestNow.identifier = @"relaxNow";
notificationActionRestNow.title = @"休息";
//是否取消提醒? 當(dāng)設(shè)置為yes時,通知中相應(yīng)地按鈕的背景色會變成紅色
notificationActionRestNow.destructive = YES;
//是否需要權(quán)限威蕉,例如鎖屏的時候刁俭,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionRestNow.authenticationRequired = NO;
//啟動app還是后臺執(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]]]];
實現(xiàn)協(xié)議方法:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
if ([identifier isEqualToString:@"completeRemindRater"]) {
NSLog(@"點擊的是%@",identifier);
}
if ([identifier isEqualToString:@"relaxNow"]) {
NSLog(@"點擊的是%@",identifier);
}
if ([identifier isEqualToString:@"inline-reply"]) {
NSLog(@"點擊的是%@",identifier);
}
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"接收到通知");
}
參考鏈接: