使用Apple Watch通知: - 因此,如果我在通知界面中添加按钮(来自对象Lib),那么错误是:
通知界面中不支持按钮
PushNotificationPayload.apns
有WatchKit Simulator Actions
喜欢这个:
"WatchKit Simulator Actions":
[
{
"title": "View",
"identifier": "firstButtonAction"
}
],
并且模拟器向我显示
现在我的问题是,如何从服务器发送View
时处理此PushNotification
按钮,
如果aps
文件包含操作按钮是Apple Watch的唯一选项,
如何使用指定的密钥从通知字典中的服务器发送它?
如何更改操作按钮BG颜色?
请有人向我提供包含aps
设备ActionButton
的示例the Apple Watch
文件,而不是Simulator
我只是通过将WatchKit Simulator Actions
键更改为WatchKit Actions
键来测试,但它没有显示操作按钮。
正如@vivekDas在答案中所建议的那样,我通过将aps
替换为:
"alert" : {
"body" : "Acme message received from Johnny Appleseed",
"action-loc-key" : "VIEW",
"action" : [
{
"id" : "buttonTapped",
"title" : "View"
}
]
}
但Xcode中的模拟器确实显示了一个动作按钮。
我认为这可以在设备Apple Watch
上运行,这是......?
你对此有什么建议。
答案 0 :(得分:17)
我已经在这两个小时内苦苦挣扎,所以这就是我通过真正的APNS Serv为生产中的通知按钮做的事情,
1)在appDelegate的父应用中注册该类别:
- (void)registerSettingsAndCategories {
// Create a mutable set to store the category definitions.
NSMutableSet* categories = [NSMutableSet set];
// Define the actions for a meeting invite notification.
UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.title = NSLocalizedString(@"Repondre", @"Repondre commentaire");
acceptAction.identifier = @"respond";
acceptAction.activationMode = UIUserNotificationActivationModeForeground; //UIUserNotificationActivationModeBackground if no need in foreground.
acceptAction.authenticationRequired = NO;
// Create the category object and add it to the set.
UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextDefault];
inviteCategory.identifier = @"respond";
[categories addObject:inviteCategory];
// Configure other actions and categories and add them to the set...
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
2)从您的Apns服务器添加类别(对我而言#34;回复")
{"aps":{"alert":"bla","category":"respond","badge":2}}
3)在WatchKitExtention中,您传入了数据:
- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification{
if ([identifier isEqualToString:@"respond"]) {
//Do stuff Here to handle action...
}
}
4)在您的家长应用appDelegate:
中- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
completionHandler();
}
警告!你也必须在你的父应用程序中处理这个动作(因为当你平息通知时,响应按钮也会在iphone上可见。在:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
答案 1 :(得分:-3)
使用Json Payload,如下所示
{
"aps": {
"badge": 10,
"alert": "Your message",
"sound": "cat.caf"
}
}
通知有效负载部分。