当应用程序处于非活动状态时,GCM推送通知在iOS上无法正常工作

3

我已在Xcode中启用了后台模式,并勾选了“远程通知”。

但是,我是否还需要启用GCM的“推送通知”?

这是来自我的REST客户端的有效载荷:

{
    "data": {


            "displayMessage": {
                "message": "Package delivered",

            }

    },
    "registration_ids": [
        "dgfdghdfghgfhhfjhgjghjghjhjghjghjghjghjghjghjhgggh"
    ],
    "content-available" : true,
    "priority": "high"
}

当应用程序在前台运行时,此方法可行;而当应用程序不在前台运行时,则此方法无效。

如有任何帮助使得GCM在iOS上工作,将不胜感激。


1
看一下这个问题 https://dev59.com/RJDea4cB1Zd3GeqPYTNz - mojo
可能是GCM在iOS后台模式下无法接收通知的重复问题。 - Yusuf K.
4个回答

0

您的密钥有误。请使用content_available而不是content-available

此外,您的JSON无效。具体来说,这里:

"message": "Package delivered",

末尾不应该有逗号。


content_available 总是会给我返回 "InternalServerError"。我已经阅读了文档,并同意它要求我们在使用 gcm 时使用 content_available。 - Santhosh Adari
我正在使用GCM向iOS设备推送消息,对我来说很有效。我正在使用content_available选项。 - Doug Richardson

当我使用content_available时,响应总是`{ multicast_id: 6973668141157543000 success: 0 failure: 1 canonical_ids: 0 results: [1] 0: { error: "InternalServerError" }-

}`。然而,当我使用content-available时,它无法唤醒应用程序。
- Santhosh Adari
HTTP响应是否也是5xx错误?如果是,文档建议联系android-gcm组报告错误。 https://developers.google.com/cloud-messaging/http-server-ref 该组的URL为https://groups.google.com/forum/#!forum/android-gcm - Doug Richardson
哦,你知道吗。你的 JSON 也有语法错误,因为在“Package delivered”后面有一个逗号。删除逗号然后再试一次。更新答案以包含此部分。 - Doug Richardson
显示剩余2条评论

0

在发布这个问题之前,我已经尝试过了,但没有成功。你能想到我可能遗漏的其他事情吗? - Santhosh Adari
我想这与服务器端有关,而不是客户端,请问您能否提供一些代码展示如何向GCM发送请求?@SanthoshAdari - Serhan Oztekin
我刚刚使用了谷歌浏览器中的“高级 REST 客户端”来进行 REST 请求。 - Santhosh Adari
{ "to": "dfgdfgdfgdfc:APA91bGAmf3utL-k3BaZtzVXYayceu6XfW28ewdsgdgdfgdfhgdfgdf86zxxVyi9sIvJqRBZxz18-6PzZxjDeX_Bdn-ZmXaeTslB8qCGsH4m79iUK1bueN2bzTpQ3tx", "data": { "sound": "default", "badge": "2", "title": "default", "body": "测试推送!" }, "content-available":true } - Santhosh Adari
当我使用“notification”而不是“data”时,我会收到内部服务器错误。使用“data”时,我可以在应用程序处于活动状态时打开通知。 - Santhosh Adari
您可以同时使用notificationdata - ztan

0

这个通知对我有用

{
  "to":"ID",
  "notification":{  
    "sound":"default",
    "title":"TITLE",
    "body":"BODY"
  },
  "priority": "high"
}

0

这个问题来自服务器端。通知消息正文应该是这样的。

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "content_available" : "true"
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark"
    }
}

这是我已经实现并且运行良好的代码。

<?php

// Message to send
$notificationTitle = "Notification title";
$notificationtBody  = "Notification arrived";

$registrationId = 'GCM_DEVICE_TOKEN';
$apiKey = "GCM_SERVER_KEY";

$response = sendNotification( 
                $apiKey, 
                $registrationId, 
                array(  
                    'sound' => "default", 
                    'badge' => 1, 
                    'body' => $notificationtBody,
                    'title' => $notificationTitle
                )
            );              

echo $response;


function sendNotification( $apiKey, $registrationId, $messageData )
{   
    $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
    $data = array(
        'to' => $registrationId,
        'content_available' => true,
        'notification' => $messageData
    );

    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 
    curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接