使用GCM在应用程序上发送通知,返回InvalidRegistration错误 [英] Using GCM to send notifications on app, returns InvalidRegistration error

查看:147
本文介绍了使用GCM在应用程序上发送通知,返回InvalidRegistration错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android设备上使用GCM发送通知,但我总是收到InvalidRegistration错误。



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

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

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

//准备包
$ msg =数组

'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;
?>

注册完成的方式如下:

< (){
$> b
保护字符串doInBackground(Void ..)pre prerivate $ {$ b $新增AsyncTask< Void,Object,String> 。params){
String msg =;
尝试{
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg =设备注册,注册ID =+ regid;

//在这里我保存注册ID,当我尝试手动使用它时,通过运行上面的PHP脚本,我得到错误。
sendRegistrationIdToBackend();

//保留注册ID - 无需再次注册。
storeRegistrationId(context,regid);
} catch(IOException ex){
msg =Error:+ ex.getMessage();
}
return msg;
}

@Override
protected void onPostExecute(String msg){
Toast.makeText(context,msg +\\\
,Toast.LENGTH_SHORT)。显示();
}

} .execute(null,null,null);

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

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


解决方案


无效的注册ID检查注册ID
,你传递给服务器。确保它与com.google.android.c2dm.intent.REGISTRATION
intent中手机收到的注册ID
相匹配,并且不会截断它或添加额外的
字符。错误代码为无效注册时发生。


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


I'm trying to send a notification using GCM on my android device, but I always get InvalidRegistration error.

Here is the PHP code that is supposed to send the notification:

<?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;
?>

This is how the registration is done:

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

After running the application, and calling the method that registers the app, I get a registration ID. Then, when I type localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx I get

{"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.

So basically you are making an error in sending the DeviceID received by the phone to the server. Make sure your code does not alter the deviceId in any way. You have not posted the code for accepting the Id's. There is a mistake in that part or the storing of the DeviceId's. I don's think there is any error in the code you have posted as the errorLog show's Invalid Registration

这篇关于使用GCM在应用程序上发送通知,返回InvalidRegistration错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆