【问题标题】:Receiving push notification in iOS when the app is closed using GCM使用 GCM 关闭应用程序时在 iOS 中接收推送通知
【发布时间】:2015-12-07 09:13:10
【问题描述】:

我的应用使用 GCM 接收推送通知。我的 android 应用程序完美运行,当我完全关闭应用程序时,我仍然会收到通知,但是如果我对我的 iOS 版本做同样的事情,我什么也不会收到。

请注意,这当然是在应用首次注册然后关闭应用之后。我找不到太多关于这个的信息。有些帖子说你不能这样做。但 facebook 和 facebook messenger 之类的都这样做。有谁知道这是怎么做到的?

这是我的

DidReceiveRemoteNotification method which is taken from the GCM documentation

// [START ack_message_reception]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
    NSLog(@"Notification received: %@", userInfo);
    // This works only if the app started the GCM service
    [[GCMService sharedInstance] appDidReceiveMessage:userInfo];
    // Handle the received message
    // [START_EXCLUDE]
    [[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
                                                        object:nil
                                                      userInfo:userInfo];
    // [END_EXCLUDE]
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler 
{
    NSLog(@"Notification received: %@", userInfo);
    // This works only if the app started the GCM service
    [[GCMService sharedInstance] appDidReceiveMessage:userInfo];
    // Handle the received message
    // Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
    // [START_EXCLUDE]
    [[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
                                                        object:nil
                                                      userInfo:userInfo];
    handler(UIBackgroundFetchResultNoData);
    // [END_EXCLUDE]
}
// [END ack_message_reception]

我发送的有效载荷如下所示

$payload =  array('registration_ids' => $Regids,
                  'content_available' => false,
                  'notification' => array("body"=> $_POST['message'],
                                          "sound" => "default",
                                          'badge' => '1'
                                          )
                  );

请注意,如果应用程序在后台,则此方法有效

【问题讨论】:

  • 请评论否决票,这是一个有效的问题
  • 我不是反对票....但我认为您被反对票是因为您没有提供有关您当前正在做什么的任何详细信息(即代码和推送内容) ,以及 iOS 版本)。
  • 公平点 :-) 我添加了更多信息
  • 太好了 - 现在足够详细,我可以对您的问题以及相关答案进行投票。

标签: ios push-notification google-cloud-messaging


【解决方案1】:

我设法通过将此添加到我的工作负载中来使其正常工作

"priority" => "high"

也许有人可以评论为什么这会起作用,因为我不知道

【讨论】:

    【解决方案2】:

    当应用程序关闭时,它也可以在 iOS 上工作 - 如果没有,那就是有问题。

    但是,当应用被用户关闭时(在任务切换器中向上滑动),您不能在 iOS 上使用 静默推送 - 因此,如果您的应用想要在用户没有的情况下对推送做出反应要点击它,当应用关闭时你不能这样做。

    【讨论】:

    • 太棒了,至少我知道问题出在我的代码上,而不是无法完成。当我发现问题时,我会更新问题。谢谢
    【解决方案3】:

    我认为您没有在通知负载中指定 alert 键。当应用关闭时,通知负载的alert 键中提供的消息将显示在设备上。

    {
        "aps" : { "alert" : "Message received from Bob" },
        "acme2" : [ "bang",  "whiz" ]
    }
    

    在上面的有效载荷中检查alert 键。 收到来自 Bob 的消息会在应用关闭时显示在 iPhone 的通知中心。

    【讨论】:

    • 我认为你是绝对正确的,我正在使用通知而不是正文
    • @Rob85 试试这种方式,如果可行,请为答案投票。
    • 试试这个$payload = array('registration_ids' => $Regids, 'content_available' => false, 'alert' = $_POST['message'], 'notification' => array("body"=> $_POST['message'], "sound" => "default", 'badge' => '1' ) );
    • 这似乎没有用,我仍然成功发送但没有任何结果
    • @Rob85 应用打开时是否来了?
    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    相关资源
    最近更新 更多