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

Posted

技术标签:

【中文标题】解析不从 iPhone 发送的可操作通知【英文标题】:Parse Actionable Notifications Not Sending from iPhone 【发布时间】:2015-10-08 18:39:12 【问题描述】:

我已尝试为我的 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。当我单击手表上的通知附带的“说谢谢”按钮时,它会发送推送,而当我单击 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 上点击而不是观看时才会发生这种情况吗?

【问题讨论】:

老实说,我不知道 ;-) 你看过请求了吗? (github.com/ParsePlatform/Parse-SDK-ios-OSX/wiki/… 对我很有帮助!)祝你好运! 【参考方案1】:

因为您在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();
    


【讨论】:

以上是关于解析不从 iPhone 发送的可操作通知的主要内容,如果未能解决你的问题,请参考以下文章

推送通知在 iphone 中没有收到来自 Parse.com 服务器的某些时间

并非所有 iPhone 推送通知都发送到 iPhone

iPhone 推送通知问题与 iPhone 发送

在 iphone 中解析推送通知

如何用PHP和iPhone向iPhone发送推送通知推我

临时推送通知未发送 iphone 通知