iOS8/9的Notification新特性


在iOS8和iOS9下,notification增加了一些新的特性,使之更加強大:
- iOS8增加了下拉時的Action按鈕,像微信一樣;
- iOS9增加了像信息一樣的可以下拉直接輸入;

iOS8為了實現action按鈕,增加了4個類,一起完成這個功能:
- UIUserNotificationSettings;
- UIUserNotificationType:alert/sound/badge
- UIUserNotificationCategory;
- UIUserNotificationAction;

閑話少說,還是代碼講的清楚:

    //初始化action
UIMutableUserNotificationAction* action1 = [[UIMutableUserNotificationAction alloc] init];
//設置action的identifier
[action1 setIdentifier:@"action1"];
//title就是按鈕上的文字
[action1 setTitle:@"title1"];
//設置點擊后在后台處理,還是打開APP
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
//是不是像UIActionSheet那樣的Destructive樣式
[action1 setDestructive:NO];
//在鎖屏界面操作時,是否需要解鎖
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction* action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setIdentifier:@"action2"];
[action2 setTitle:@"title2"];
[action2 setActivationMode:UIUserNotificationActivationModeForeground];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

//一個category包含一組action,作為一種顯示樣式
UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init];
[category setIdentifier:@"category1"];
//minimal作為banner樣式時使用,最多只能有2個actions;default最多可以有4個actions
[category setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal];

NSSet* set = [NSSet setWithObject:category];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
categories:set];
//注冊notification設置
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

//添加一個notification,10秒后觸發
UILocalNotification* notification = [[UILocalNotification alloc] init];
//設置notifiction的樣式為"category1"
[notification setCategory:@"category1"];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:@"this is an alert"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

結果是這個樣子的:

這時候點擊action1,alert就直接消失了,因為action1的setActivationMode是background的;而點擊action2,app就會被打開;
無論哪個action,系統打開的時候都會調用AppDelegate的-(void)application: handleActionWithIdentifier: forLocalNotification:(nonnull UILocalNotification *)completionHandler:方法;如果直接點擊alert的話,會調用application: didFinishLaunchingWithOptions:方法,而options參數里就包含了alert的信息;

幾個要注意的地方:

  • push notification的時候,如何指定alert的樣式,即category?
    只需要在push的內容里添加category的Identifier:
"aps":{
"alert":"This is a notification",
"category":<categoryIdentifier>
}
  • APP正在運行的時候,收到了notification,如何接收?
    此時會調用application:didReceiveLocalNotification:方法;而系統不會有任何操作,不會發出聲音、添加badge icon、也不會彈出alert;

  • alert可以設置一個alertLaunchImage,可以替代系統默認的launch image;

  • APP的badge number如何自動+1:
    蘋果沒有提供這個方法,因此我們要在服務器上保存某個用戶的badge number,在推送時+1;

  • 能不能像信息那樣,下拉直接回復?
    iOS8沒有這個功能,最新的iOS9可以:

action.behavior = UIUserNotificationBehaviorTextInput;
  • iOS8時,增加了在指定地點彈出的功能:[notification setRegion:];

  • 如何確定用戶有沒有禁止發送notifications:
    設置notificationCategory后,檢查AppDelegate有沒有調用下面的方法:

- (void) application: didRegisterUserNotificationSettings:

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com