我尝试为我的Parse应用程序添加可操作通知,iPrayed 4 U.在应用程序中,有人可以通过单击按钮为您“祷告”。这样做会运行包含APNS有效负载类别的Cloud Code。在我的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上收到通知,向下拖动并显示“Say Thanks”按钮。我点击它,但没有任何反应。这是踢球者。通常我会说,“好吧,我弄乱了一些东西,开始调试”。不过,我也有Apple Watch。当我单击Watch上通知附带的Say Thanks按钮时,它会发送Push,而不是在我点击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上点击而不是观看时才会发生这种情况吗?
答案 0 :(得分:3)
因为您在completionHandler()
完成之前致电PFCloud
。您需要在completionHandler()
completionBlock
这样做。
- (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();
}
}