当应用程序未处于活动状态时,GCM推送通知不适用于iOS

时间:2015-10-12 17:53:16

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

我在xcode中启用了后台模式并选中了“远程通知”。

我是否还必须启用“推送通知”?对于GCM

这是来自我的休息客户端的有效负载

{
    "data": {


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

            }

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

当应用程序处于前台时这种方法有效,而当应用程序不在前台时则不起作用。

非常感谢任何有助于此iM工作的GCM的帮助。

4 个答案:

答案 0 :(得分:0)

我建议检查消息的格式,如下所述:

Making GCM work for iOS device in the background

答案 1 :(得分:0)

你的钥匙错了。使用content_available,而不是content-available

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

"message": "Package delivered",

最后不应该有逗号。

答案 2 :(得分: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;
}

答案 3 :(得分:0)

此通知对我有用

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