使用 GCM 在应用程序上发送通知,returns InvalidRegistration 错误

Using GCM to send notifications on app, returns InvalidRegistration error

我正在尝试在我的 android 设备上使用 GCM 发送通知,但我总是收到 InvalidRegistration 错误。

这是应该发送通知的 PHP 代码:

<?php
define('API_ACCESS_KEY', 'API KEY HIDDEN');

$registrationIds = array( $_GET['id']);

// prep the bundle
$msg = array
(
    'message'   => 'TestMessage',
    'title'     => 'TestTitle',
    'subtitle'  => 'TestSubtitle',
    'tickerText'    => 'TestTicker',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);

$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 );

echo $result;
?>

注册是这样完成的:

private void registerInBackground() {
new AsyncTask<Void, Object, String>() {
    @Override
    protected String doInBackground(Void... params) {
        String msg = "";
        try {
            if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(context);
            }
            regid = gcm.register(SENDER_ID);
            msg = "Device registered, registration ID=" + regid;

            //Here I save the registration ID, and when I try to use it manually, by running the PHP script above, I get the error.
            sendRegistrationIdToBackend();

            // Persist the registration ID - no need to register again.
            storeRegistrationId(context, regid);
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
        }
        return msg;
    }

    @Override
    protected void onPostExecute(String msg) {
        Toast.makeText(context, msg + "\n", Toast.LENGTH_SHORT).show();
    }

}.execute(null, null, null);

在运行应用程序之后,调用注册应用程序的方法,我得到了一个注册ID。然后,当我输入 localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx 我得到

{"multicast_id":8460288429151356251,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

Invalid Registration ID Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the com.google.android.c2dm.intent.REGISTRATION intent and that you're not truncating it or adding additional characters. Happens when error code is InvalidRegistration.

所以基本上您在将 phone 收到的 DeviceID 发送到服务器时出错了。确保您的代码不会以任何方式改变 deviceId。您尚未发布接受 ID 的代码。该部分或 DeviceId 的存储存在错误。我认为您作为错误日志显示的无效注册发布的代码中没有任何错误

如果您从数据库中检索令牌,请确保该列的数据类型是文本或非常长的 varchar,例如 200。令牌的长度超过 100 个字符,因此它可能会被切成两半存储在数据库中

我通过发送设备注册 ID 解决了这个问题,例如

<?php
require_once('loader.php');

// return json response 
 $json = array();

$registatoin_ids = array("APA91bHhzESU4GQKwS_I-sMMpAPZ44GbC0wvDAO-Dch87_jwJzaB1gK5PPeaGmt6vFZx2Bvd6AC8EkYDl6PurNUDO6zLoxX50q7gLc8GMOLGcwmQgiWwSZS-PPOFn1MpA5hz_TFQ6S-4tstLO");
$message = array("product" => "shirt");

$result = send_push_notification($registatoin_ids, $message);
echo $result;
?>

在 register.php 文件中。 您将从 google 云端获取此设备注册 ID。