Android版Firebase Cloud Messaging中的InvalidRegistration错误 [英] InvalidRegistration error in Firebase Cloud Messaging for Android

查看:70
本文介绍了Android版Firebase Cloud Messaging中的InvalidRegistration错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an Android App that using Push Notification feature. I need to push from server. I use Firebase for it. Frankly, this is my first time of using Firebase. I am newbie to Firebase. But when I push from server using PHP and CURL, it is giving me invalid registration error.

我在Android中像这样获得Firebase令牌

I get the Firebase token in Android like this

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

然后我保存了将该令牌发送到服务器并保存在数据库中。

Then I save sent that token to server and saved in the database.

在服务器上,我像这样

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, true);
        curl_setopt($ch, 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数据,如下面的屏幕截图所示。

I am using CodeIgniter 3 framework for building Rest API. When I push accessing url from browser, it returns JSON data with error as in the below screenshot.

如您所见,它显示InvalidRegistration错误,并且消息未推送到设备。我的代码有什么问题?

As you can see it is giving InvalidRegistration error and message is not pushed to devices. What is wrong with my code?

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

This is my FirebaseMessagingService class that show notification in 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,0,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());
    }
}


推荐答案


无效的注册ID检查传递给服务器的注册ID
的格式。确保它与电话在com.google.firebase.INSTANCE_ID_EVENT
意向中获得的注册ID
相匹配,并且您没有将其截断或添加其他
个字符。错误代码为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.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()

Please check with both the side app side and your side that the exact same registration id is stored in the server which Application on mobile receives it in on onTokenRefresh method. You should have received the exact same registration token as developer got in FirebaseInstanceId.getInstance().getToken()

当我收到您的评论,并且您更新了代码后,此处的代码有所更改,它来自Google文档本身...

As i got your comment and you've updated the code here is some change in your code it is from google doc it self...

@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具有三种消息类型:

Firebase has three message types:


通知消息:通知消息在后台或
前台起作用。当应用程序处于后台时,通知消息将
传递到系统托盘。如果应用程序在前台,则
消息由onMessageReceived()或
didReceiveRemoteNotification回调处理。这些基本上就是
,即显示消息。

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.

数据消息:在Android平台上,数据消息可以在
背景和前景。数据消息将由
onMessageReceived()处理。这里有一个特定于平台的注释:在
Android上,可以在用于
启动活动的Intent中检索数据有效载荷。

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.

带有通知和数据有效负载的消息:在
后台时,应用程序会在通知$ b $中接收通知有效负载b托盘,仅在用户点击
通知时处理数据有效负载。在前台时,您的应用会收到一条消息
对象,其中两个有效负载都可用。其次,click_action
参数通常用于通知有效负载,而不用于数据
有效负载。如果在数据有效载荷内部使用此参数,则将
作为自定义键值对,因此您需要实现
自定义逻辑,以使其按预期工作。

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 Cloud Messaging中的InvalidRegistration错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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