標籤:des style blog io ar color os 使用 sp
在ios8中,有了關於本地通知和遠程通知的新改動,下面來看看。
先看一段網上抄來的代碼
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init]; notificationAction1.identifier = @"Accept"; notificationAction1.title = @"Accept"; notificationAction1.activationMode = UIUserNotificationActivationModeBackground; notificationAction1.destructive = NO; notificationAction1.authenticationRequired = NO; UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init]; notificationAction2.identifier = @"Reject"; notificationAction2.title = @"Reject"; notificationAction2.activationMode = UIUserNotificationActivationModeBackground; notificationAction2.destructive = YES; notificationAction2.authenticationRequired = YES; UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init]; notificationAction3.identifier = @"Reply"; notificationAction3.title = @"Reply"; notificationAction3.activationMode = UIUserNotificationActivationModeForeground; notificationAction3.destructive = NO; notificationAction3.authenticationRequired = YES; UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init]; notificationCategory.identifier = @"Email"; // [notificationCategory setActions:@[notificationAction3] forContext:UIUserNotificationActionContextDefault]; [notificationCategory setActions:@[notificationAction1] forContext:UIUserNotificationActionContextMinimal]; NSSet *categories = [NSSet setWithObjects:notificationCategory, nil]; UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; localNotification.alertBody = @"Testing"; localNotification.category = @"Email"; // Same as category identifier [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
這是一段發送本地通知的代碼,其中涉及到了這次更新的幾個類,
UIMutableUserNotificationAction,UIMutableUserNotificationCategory,UIUserNotificationSettings
我們需要依次產生這幾個對象,最後使用
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];把程式的通知設定註冊到系統中去。
注意,當應用程式在前台時,通知到來,系統不會為程式彈出alert,而是會調用相應的
didReceiveLocalNotification 和
didReceiveRemoteNotification 這2個代理函數,把通知內容交給程式自身處理。而當程式不在前台時,系統才會為使用者彈出alert,提示使用者進行相應操作。 這裡
UIMutableUserNotificationAction就代表了一個操作,在介面上的表示形式就是一個按鈕,當點擊按鈕時,就會調用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);
不會再調用
didReceiveLocalNotification 和
didReceiveRemoteNotification 這2個代理函數了。
在
UIMutableUserNotificationAction 中有一個屬性,
activationMode,當它是
UIUserNotificationActivationModeBackground時,系統會選在在後台執行代碼,執行的時間是有限的,我測試的是30s,如果超過30s你的程式還在後台執行,那麼系統會終止調你的程式,並拋出異常。異常如下
<Warning>: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> has active assertions beyond permitted time: {( <BKProcessAssertion: 0x17d6b4f0> id: 42-2B9D290F-7F21-4DCB-955B-9D80DE693382 name: Notification action process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> permittedBackgroundDuration: 30.000000 reason: notificationAction owner pid:42 preventSuspend preventThrottleDownUI preventIdleSleep preventSuspendOnSleep , <BKProcessAssertion: 0x17d6de50> id: 42-707C3B54-51BC-47DA-B779-B11888416FE4 name: Deliver Message process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> permittedBackgroundDuration: 10.000000 reason: suspend owner pid:42 preventSuspend preventThrottleDownCPU preventThrottleDownUI preventSuspendOnSleep )}
注意,如果你沒有點擊action按鈕,而是通過點擊通知內容本身,那麼系統扔會像ios7一樣,調用
didReceiveLocalNotification 和
didReceiveRemoteNotification 這2個代理函數。
如果程式沒有啟動,使用者點擊action按鈕後,系統會先調用
didFinishLaunchingWithOptions啟動程式,再調用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);
來處理action,這一點和以前點擊通知內容本身的效果一致,都會先啟動程式,再調用相應函數。
iOS8 中的通知:Notification 草稿,正在整理中。。。。