适用于Android的Firebase云消息传递中的InvalidRegistration错误

适用于Android的Firebase云消息传递中的InvalidRegistration错误,第1张

概述我正在开发一个使用推送通知功能的Android应用程序.我需要从服务器推送.我使用Firebase.坦率地说,这是我第一次使用Firebase.我是Firebase的新手.但是当我使用PHP和CURL从服务器推送时,它给了我无效的注册错误.我在Android中获得了这样的Firebase令牌String token = FirebaseInstanceId.

我正在开发一个使用推送通知功能的Android应用程序.我需要从服务器推送.我使用Firebase.坦率地说,这是我第一次使用Firebase.我是Firebase的新手.但是当我使用PHP和CURL从服务器推送时,它给了我无效的注册错误.

我在AndroID中获得了这样的Firebase令牌

String token = FirebaseInstanceID.getInstance().getToken();

然后我将该令牌保存到服务器并保存在数据库中.

在服务器上,我这样推

class Pusher extends REST_Controller {    function __construct()    {        parent::__construct();    }    public function notification_get()    {        $rows = $this->db->get('device_registration')->result();        $tokens= array();        if(count($rows)>0)        {            foreach($rows as $row)            {                $tokens[] = $row->token;            }        }        $message = array("message"=>"FCM PUSH NOTIFICATION TESTING");        if(count($tokens)>0)        {            $result = $this->send_notification($tokens,$message);            if(!$result)            {                dIE("Unable to send");            }            else{                $this->response($result,REST_Controller::http_OK);            }        }    }    function send_notification($tokens,$message)    {        $url = 'https://fcm.GoogleAPIs.com/fcm/send';        $fIElds = array(                'registration_IDs'=>$tokens,'data'=>$message            );        $headers = array(                'Authorization:key = AIzaSyApyfgXsNQ3dFTGWR6ns_9pttr694VDe5M',//Server key from firebase                'Content-Type: application/Json'            );        $ch = curl_init();        curl_setopt($ch,CURLOPT_URL,$url);        curl_setopt($ch,CURLOPT_POST,true);        curl_setopt($ch,CURLOPT_httpheader,$headers);        curl_setopt($ch,CURLOPT_RETURNTRANSFER,CURLOPT_SSL_VERIFYHOST,0);        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);        curl_setopt($ch,CURLOPT_POSTFIELDS,Json_encode($fIElds));        $result = curl_exec($ch);        if($result==FALSE)        {            return FALSE;        }        curl_close($ch);        return $result;    }}

我正在使用CodeIgniter 3框架来构建Rest API.当我从浏览器推送访问URL时,它返回带有错误的JsON数据,如下面的屏幕截图所示.

正如您所看到的,它提供了InvalIDRegistration错误,并且消息未被推送到设备.我的代码出了什么问题?

额外

这是我的FirebaseMessagingService类,它在androID中显示通知

public class FirebaseMessagingService extends com.Google.firebase.messaging.FirebaseMessagingService {    @OverrIDe    public voID onMessageReceived(RemoteMessage remoteMessage) {        super.onMessageReceived(remoteMessage);        showNotification(remoteMessage.getData().get("message"));    }    private voID showNotification(String message)    {        Intent i = new Intent(this,MainActivity.class);        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);        PendingIntent pendingIntent = PendingIntent.getActivity(this,i,PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setautoCancel(true)                .setContentTitle("FCM Test")                .setContentText(message)                .setSmallicon(R.drawable.info)                .setContentIntent(pendingIntent);        notificationmanager manager = (notificationmanager)getSystemService(NOTIFICATION_SERVICE);        manager.notify(0,builder.build());    }}
最佳答案

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.firebase.INSTANCE_ID_EVENT
intent and that you’re not truncating it or adding additional
characters. Happens when error code is InvalIDRegistration.

请与侧面应用程序端和您的身边一起检查完全相同的注册ID是否存储在服务器上的哪个应用程序在onTokenRefresh方法上接收它.您应该已经收到与开发人员在FirebaseInstanceID.getInstance()中获得的完全相同的注册令牌.getToken()

当我收到您的评论并且您已更新代码时,您的代码中的一些更改来自谷歌文档它自己…

@OverrIDepublic voID onMessageReceived(RemoteMessage remoteMessage) {    // Todo(developer): Handle FCM messages here.    Log.d(TAG,"From: " + remoteMessage.getFrom());    // Check if message contains a data payload.    if (remoteMessage.getData().size() > 0) {        Log.d(TAG,"Message data payload: " + remoteMessage.getData());    }    // Check if message contains a notification payload.    if (remoteMessage.getNotification() != null) {        Log.d(TAG,"Message Notification Body: " + remoteMessage.getNotification().getbody());    }    // Also if you intend on generating your own notifications as a result of a received FCM    // message,here is where that should be initiated. See sendNotification method below.}

Firebase有三种消息类型:

Notification messages: Notification message works on background or
foreground. When app is in background,Notification messages are
delivered to the system tray. If the app is in the foreground,
messages are handled by onMessageReceived() or
dIDReceiveRemoteNotification callbacks. These are essentially what is
referred to as display messages.

Data messages: On AndroID platform,data message can work on
background and foreground. The data message will be handled by
onMessageReceived(). A platform specific note here would be: On
AndroID,the data payload can be retrIEved in the Intent used to
launch your activity.

Messages with both notification and data payloads: When in the
background,apps receive the notification payload in the notification
tray,and only handle the data payload when the user taps on the
notification. When in the foreground,your app receives a message
object with both payloads available. Secondly,the click_action
parameter is often used in notification payload and not in data
payload. If used insIDe data payload,this parameter would be treated
as custom key-value pair and therefore you would need to implement
custom logic for it to work as intended.

总结

以上是内存溢出为你收集整理的适用于Android的Firebase云消息传递中的InvalidRegistration错误全部内容,希望文章能够帮你解决适用于Android的Firebase云消息传递中的InvalidRegistration错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1139423.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存