我试过在我的Parse应用程序iprayer 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。当我点击手表上通知附带的"说谢谢"按钮时,它会发送推送,而当我在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();
}
}