我已经尝试为我的 Parse 应用 iPrayed 4 U 添加可操作通知。在该应用中,有人可以通过单击按钮为您“祈祷”。这样做会运行包含 APNS 负载类别的云代码。在我的 AppDelegate 中,我有:
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"THANKS_IDENTIFIER";
acceptAction.title = @"Say Thanks";
// Given seconds, not minutes, to run in the background
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = @"TAGGED_CATEGORY";
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"THANKS_IDENTIFIER"]) {
[self handleAcceptActionWithNotification:notification];
NSLog(@"Handled");
}
// Must be called when finished
completionHandler();
}
-(void) handleAcceptActionWithNotification:(NSDictionary *)notification {
NSLog(@"SendingThanks");
PFUser *me = [PFUser currentUser];
NSString *theirname = me[@"additional"];
NSString *myAlertString=notification[@"loc-args"];
NSLog(@"%@", notification);
[PFCloud callFunctionInBackground:@"thankYou"
withParameters:@{@"recipientId": myAlertString, @"theirName": theirname}
block:^(NSString *success, NSError *error) {
if (!error) {
// Push sent successfully
}
}];
}
所以,我开始测试。当有人为我祈祷时,我会在我的 iPhone 上收到通知,向下拖动就会看到“说谢谢”按钮。我点击它,但没有任何反应。这是踢球者。通常我会说,“嗯,我搞砸了,开始调试”。不过,我也有 Apple Watch。当我点击 Watch 上通知附带的“说谢谢”按钮时,它会发送推送,而当我在 iPhone 上点击同一个按钮时,它什么都不做。
对这里发生的事情有什么想法吗?
更新:
在控制台中,当它失败时,我得到:
[Error]: The request timed out. (Code: 100, Version: 1.8.2)
2015-10-08 13:42:49.424 iPrayed[2231:712503] [Error]: Network connection failed. Making attempt 1 after sleeping for 1.463493 seconds.
最终我接到了一个成功电话,但到那时它仍然没有真正发送推送。知道为什么只有在从 iPhone 上点击而不是观看时才会发生这种情况吗?
最佳答案
因为您在 PFCloud
完成之前调用了 completionHandler()
。您需要在 completionBlock
completionHandler()
这样做。
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"THANKS_IDENTIFIER"]) {
PFUser *me = [PFUser currentUser];
NSString *theirname = me[@"additional"];
NSString *myAlertString=notification[@"loc-args"];
[PFCloud callFunctionInBackground:@"thankYou" withParameters:@{@"recipientId": myAlertString, @"theirName": theirname} block:^(NSString *success, NSError *error) {
completionHandler();
}];
} else {
completionHandler();
}
}
关于ios - 解析不是从 iPhone 发送的可操作通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33023519/