使用GCM推送通知到应用程序时,返回无效注册错误。

9

我正在尝试在我的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"}]}
3个回答

14

无效的注册ID,请检查您向服务器传递的注册ID的格式是否正确。确保它与手机在com.google.android.c2dm.intent.REGISTRATION intent中接收到的注册ID匹配,且您没有截断它或添加额外的字符。当错误码为InvalidRegistration时发生。

简而言之,您在将手机接收到的设备ID发送到服务器时出现了错误。请确保您的代码不会以任何方式更改设备ID。您没有发布接受ID的代码。那一部分可能存在错误或者是存储设备ID的问题。我认为您所发布的代码并没有错误,因为错误日志显示为“InvalidRegistration”。


就是这样,我遇到了一个小问题,现在已经解决了。现在我收到了 success:1 的提示,但我还有另一个问题,我该如何开始在应用程序中接收消息呢?它们没有到达。 - Val
请访问以下网址:https://developer.android.com/google/gcm/client.html,查看有关“接收下行消息”的部分。 - Msk
我也遇到了类似的问题,你是如何解决的 @Val - Jay Zee
你是怎么解决的?我也遇到了同样的问题。 - Nikita Agrawal
检查您传递给服务器的注册令牌格式。确保它与客户端应用程序从 Firebase 通知注册中接收到的注册令牌匹配。不要截断或添加其他字符。当您使用 Rest 客户端发送此设备时,可能会改变其格式。对于我来说,":A" 更改为 "%3A"。但是在接收者端解密时没有问题。但是为了测试,请确保不要从 Rest 客户端的 URL 参数复制 device_id。 您可以打印设备 ID 并复制它进行测试。 - Naveen Kumar M
我在将应用上传到商店后遇到了相同的问题,之前存档构建都很好。有人可以帮忙吗? - Abhishek

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

谢谢!我以为128个字符长度的列足够了,但实际上不够。扩展它解决了我的问题。 - Yerke
就这样!非常感谢。 - xurshid29
我把我的字段设为了64个字符,我当时在想什么?!好东西。 - William

0

我通过发送设备注册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 Cloud获取此设备的注册ID。

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