尝试通过Firebase Cloud Functions(Android)发送通知后出现错误

Tal*_*rda 5 node.js firebase google-cloud-functions firebase-cloud-messaging

我是Firebase和Node.js的新手。我正在尝试使用Firebase Cloud Functions将通知从一台设备发送到另一台设备。

这是发送通知的node.js代码:

var functions = require('firebase-functions');
var admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/sendNotification/{notificationId}')
        .onWrite(event => {

        var regToken="fYRweeL8cic:APA91bH6Q_gyKKrLL...";

        // Grab the current value of what was written to the Realtime Database.
        var eventSnapshot = event.data;

        var payload = {
            data: {
                title: eventSnapshot.child("title").val()
            }
        };

        // Set the message as high priority and have it expire after 24 hours.
        var options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
        };


    admin.messaging().sendToDevice(regToken,payload,options)
    .then(function(response){
        console.log("Successfully sent message: ", response);
    })
    .catch(function(error){
        console.log("Error sending message: ", error);
    })
})
Run Code Online (Sandbox Code Playgroud)

这是将通知添加到实时数据库以触发功能的代码:

 public void sendNotification(){
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference myRef = database.getReference("sendNotification");

        myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Toast.makeText(getApplicationContext(),
                        "sent", Toast.LENGTH_SHORT).show();

            Map data = new HashMap();
            data.put("title", "this is my title");
            data.put("message", "this is the message");
            myRef.push().setValue(data);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
Run Code Online (Sandbox Code Playgroud)

我可以看到该函数已执行,但是出现以下错误:

在此处输入图片说明

通知出现在数据库中: 在此处输入图片说明

该功能在控制台中的显示方式如下:

在此处输入图片说明

问题是未发送通知。 我明白了: {results:[{error: [Object]}]出于某种原因。导致此错误的原因是什么?


编辑:(解决方案)

如评论中所建议,我使用了此方法:JSON.stringify(response)以获得更多信息。这是响应:

 {"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":6051985890611026000}
Run Code Online (Sandbox Code Playgroud)

响应非常明确,令牌已更改。我已将其更改为有效令牌,并且它起作用了。

Tal*_*rda 4

正如评论中所建议的,我使用了这个:JSON.stringify(response)来获取更多信息。这是回应:

 {"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":6051985890611026000}
Run Code Online (Sandbox Code Playgroud)

响应非常明确,令牌已更改。我已将其更改为有效令牌并且有效。