use*_*384 5 push-notification ios swift watchkit
我想为apple watch通知添加值(当前屏幕使用硬编码数据):
我要添加的值适用于以下字段:"Amount","At"和"When".如何从PushNotificationPayload.apns文件中添加获取此值并将其显示在通知上?
这是PushNotificationPayload.apns文件:
{
"aps": {
"alert": {
"body": "New Transaction\n\n",
"title": "Optional title"
},
"category": "newTransactionCategory"
},
"WatchKit Simulator Actions": [
{
"title": "Details",
"identifier": "transactionDetailsButtonAction"
}
],
"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}
Run Code Online (Sandbox Code Playgroud)
这些是步骤,
创建一个新类,它是 的子类WKUserNotificationInterfaceController。
从情节提要中,选择要通知的动态界面场景(如果尚未创建它,请在静态场景的属性检查器中启用“具有动态界面”),并在身份检查器中设置上面创建的自定义类。
现在修改您的 PushNotificationPayload.apns 文件内容,如下所示,
{
"aps": {
"alert": {
"body": "New Transaction\n\n",
"title": "Optional title"
},
"category": "newTransactionCategory"
},
"WatchKit Simulator Actions": [
{
"title": "Details",
"identifier": "transactionDetailsButtonAction"
}
],
"Amount": "USD 20",
"At": "Mc Donalds",
"When": "Today",
}
Run Code Online (Sandbox Code Playgroud)当收到远程通知时,将在您的自定义通知接口类中调用此方法,并且您将收到字典“remoteNotification”中的自定义键,您需要使用它来在此处设置标签的文本。
-(void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
NSLog(@"remoteNotification Dictionary %@",remoteNotification);
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
Run Code Online (Sandbox Code Playgroud)最后是调试:
在顶部选择您的目标,然后选择编辑方案
单击底部的重复方案并提供您的自定义名称,例如“NOTIFICATION-Mywatchkitapp”等...
然后,选择 WatchKit Interface to Dynamic Notification、Notification Payload 到 PushNotificationPayload.apns 文件并为此目标运行。
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |