四、long-look的静态界面和动态界面
上面提到过,long-look分为静态界面和动态界面两种,当我们在storyBoard中拉入一个Notification Interface Controller的时候,可以选择同时创建动态界面,勾选 Has Dynamic Interface:
这时,在storyBoard中是如下模样:
我们在创建一个文件,继承于WKUserNotificationInterfaceController,并将storyBoard中动态的的推送controller的class设置为我们创建的类:
注意,这里设置的是动态的Interface,也就是上面右边的controller。之后运行,你会发现效果并没有什么改变,那是因为系统默认会从静态界面加载推送界面,我们需要在NotifacationController代码中做一些操作:
//在NotificationController中重写下面两个方法
//这个用于本地推送
override func didReceiveLocalNotification(localNotification: UILocalNotification, withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
//在这里做一些动态界面的加载操作,比如可以根据推送的数据 设置图片 文字等
//下面这个方法决定是加载静态的界面还是动态的界面
//Custom是加载动态界面
//default是加载静态界面
completionHandler(.Custom)
}
//设个用于远程推送 和上面方法类似
override func didReceiveRemoteNotification(remoteNotification: [NSObject : AnyObject], withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
completionHandler(.Custom)
}
五、触发推送点击事件
首先,我们多配置几个点击按钮,在apns文件中如下配置:
"WatchKit Simulator Actions": [
{
"title": "第一",
"identifier": "one"
},
{
"title": "第二",
"identifier": "two"
},
{
"title": "第三",
"identifier": "three"
}
],
在我们watch App的InterfaceController中实现如下的方法:
//重写下面两个方法来响应点击事件
//远程推送的方法
override func handleActionWithIdentifier(identifier: String?, forRemoteNotification remoteNotification: [NSObject : AnyObject]) {
//通过我们配置的按钮id来区分点击的按钮 处理响应的逻辑
print(identifier)
}
//本地推送的方法
override func handleActionWithIdentifier(identifier: String?, forLocalNotification localNotification: UILocalNotification) {
}