解析未从 iPhone 发送的可操作通知

Parse Actionable Notifications Not Sending from iPhone

我尝试为我的 Parse 应用 iPrayed 4 U 添加可操作通知。在该应用中,有人可以通过单击按钮为您 "Pray"。这样做会运行包含 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 上收到通知,向下拖动有 "Say Thanks" 按钮。我点击它,但没有任何反应。这是踢球者。通常我会说,"Well, I messed something up, start debugging"。不过,我也有 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();
    }

}