Actionable Notifications
整理的内容有不够准确的,望大家积极排雷交流指正。
Actionable Notifications是什么
Actionable Notifications是watchKit专门为Local Notification和remote Notificatio提供的接口。
Actionable Notifications分为两种运行方式:
静态(static)和动态(dynamic)运行方式。
* 静态运行方式:
只能展示静态内容,在storyboard中可以自定义添加其他控件,可以响应通知按钮回调。只是静态的加载static interface controller。Notification interface controller类中各个回调方法都不会走,真不愧是静态呀。(冷加载)
* 动态运行方式:
Notification interface controller 类中各个回调方法都会走,可以在接收到推送的回调方法中,设置storyboard中动态推送UI。(热加载)
接收到推送的回调方法如下代码:
/**
*@description 接收Remote Notification回调
*@param remoteNotification 推送内容
*@param 回调
*@returns void
*/
///接收到推送,展示前调用
- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
//获得apns推送内容,可更新storyboard中 dynamic ntoficaiotn controller 的 UI
//设置采用那个ntoficaiotn controller 去展示推送的数据。
//static 默认的
completionHandler(WKUserNotificationInterfaceTypeDefault);
//dynamic 自定义的
//completionHandler(WKUserNotificationInterfaceTypeCustom);
}
Local notification对应回调方法:
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
}
如何设置静态和动态运行?
* 推送展示的UI上的按钮是由推送内容(josn)定制的,如下定制了两个按钮。
"WatchKit Simulator Actions": [
{
"title": "First Button",
"identifier": "firstButtonAction"
}
,{
"title": "Second Button",
"identifier": "secondButtonAction"
}
],
响应推送按钮事件
无论静态还是动态方式都可以自定义多个按钮,dismiss 按钮是系统定制,无法修改,点击后不会走如下回调。
在main app 主controller中的对应的回调方法:
-
远程推送(remote notificaiton):
#pragma mark - Notification Action //remote (identifier:事件id;remoteNotification:推送内容) - (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification{ NSArray* identifiersArr = @[@"firstButtonAction",@"secondButtonAction"]; NSInteger tag = NSNotFound; if ([identifiersArr containsObject:identifier]) { tag = [identifiersArr indexOfObject:identifier]; } switch (tag) { case 0:{ NSLog(@"firstButtonAction is taped"); break; } case 1:{ NSLog(@"secondButtonAction is taped"); break; } default:{ NSLog(@"未找到action 对应的 identifier:%@",identifier); break; } } }
-
本地推送(remote notificaiton):
//local (identifier:事件id;remoteNotification:推送内容) - (void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(NSDictionary *)remoteNotification { }
notificaiton controller 上不可添加的UI控件(静态动态都不可以)
UIButton、