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

前端之家收集整理的这篇文章主要介绍了适用于Android的Firebase云消息传递中的InvalidRegistration错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在开发一个使用推送通知功能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数据,如下面的屏幕截图所示.

enter image description here

正如您所看到的,它提供了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()

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

@Override
public 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相关文章