无法丢失gcm推送通知

时间:2017-01-18 05:24:21

标签: php cordova push-notification google-cloud-messaging

我正在从php向cordova应用程序发送gcm推送通知。当用户移动连接到互联网时效果很好。如果用户手机没有连接互联网4小时,那段时间我的服务器发送5通知。在连接互联网时,用户必须得到5个未通知但没有获得任何通知。我正在使用phonegap-plugin-push 1.6.4" PushPlugin"在cordova应用程序中提前感谢。

PHP代码

            $registrationIds = array($to);
    $msg = array
        (
        'notId' => rand(1, 999999),
        'message' => $message,
        'title' => $title,
        'vibrate' => 1,
        'sound' => 1
    );
    $fields = array
        (
        'registration_ids' => $registrationIds,
        'data' => $msg
    );
    $headers = array
        (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    curl_close($ch);

Cordova JS Code

      var push = PushNotification.init({"android": {"senderID": appsenderid, "alert": true, "badge": true, "sound": true},
    "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {}});

push.on('registration', function (data) {
    //alert(data.registrationId);
    register_device(data.registrationId);
});

1 个答案:

答案 0 :(得分:0)

也许这条消息被推迟了。请注意,当设备重新连接到互联网时,message to arrive最多可能需要15分钟。如果设备处于脱机状态,则会在重新连接后收到所有待处理的通知。如果注册ID错误或任何其他错误,Google会返回相应的回复。

检查此threadtutorial是否有帮助。

相关问题