【问题标题】:Using GCM to send notifications on app, returns InvalidRegistration error使用 GCM 在应用程序上发送通知,返回 InvalidRegistration 错误
【发布时间】:2015-06-24 16:56:12
【问题描述】:

我尝试在我的 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 我得到 ​​p>

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

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    无效的注册 ID 检查注册 ID 的格式 你传递给服务器。确保它与注册 ID 匹配 手机收到 com.google.android.c2dm.intent.REGISTRATION 意图并且您没有截断它或添加额外的 人物。当错误代码为 InvalidRegistration 时发生。

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

    【讨论】:

    • 就是这样,我有一个小问题,现在我解决了。我现在收到success:1,我还有一个问题,我现在如何开始在应用程序中接收消息?他们不会来
    • developer.android.com/google/gcm/client.html 检查接收下游消息的部分
    • 我也遇到了类似的问题,你是如何解决的@Val
    • 你是怎么解决的?我遇到了同样的问题
    • 检查您传递给服务器的注册令牌的格式。确保它与客户端应用通过 Firebase 通知注册收到的注册令牌相匹配。不要截断或添加其他字符。当您发送此设备以从 Rest 客户端发送时,它可能会更改格式。对我来说 :A 更改为 %3A。但是当接收器结束它解密它时很好。但为了测试,请确保不要从 Rest Client 的 URL 参数中复制 device_id。您打印 device_id 并复制它以进行测试。
    【解决方案2】:

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

    【讨论】:

    • 谢谢!我认为 128 字符长度的列就足够了,但事实并非如此。扩展它解决了我的问题
    • 就是这样!太感谢了
    • 我已将字段设置为 64 个字符,我在想什么?!好东西
    【解决方案3】:

    我通过发送设备注册 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 文件中。 你将从谷歌云获取此设备注册 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2017-01-30
      相关资源
      最近更新 更多