node.js - 尝试通过 Firebase Cloud Functions (Android) 发送通知后出错

标签 node.js firebase firebase-cloud-messaging google-cloud-functions

我是 Firebase 和 nodejs 的新手。我正在尝试使用 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);
    })
})

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

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

            }
        });

    }

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

enter image description here

通知出现在数据库中: enter image description here

这是函数在控制台中的显示方式:

enter image description here

问题是没有发送通知。 我得到这个:{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}

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

最佳答案

正如评论中所建议的,我使用了这个: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}

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

关于node.js - 尝试通过 Firebase Cloud Functions (Android) 发送通知后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45401922/

相关文章:

javascript - 将 props 传递给服务器中 React 路由器的处理程序

firebase - Firestore 按字段分组

带有 Firebase 主题消息的 iOS 推送通知未发送到设备

ios - 如何使用 FCM 为富文本通知创建有效负载

javascript - 在 node.js 中将数组传递给 jade 模板时出现问题

node.js - Meteor 中的持久 session

firebase - Firebase用户号显示为空

javascript - 使用 Firebase Auth (NextJS) 实现用户角色

firebase - FCM(Firebase 云消息)中的服务器 key 和旧服务器 key 有什么区别

node.js - 创建新元素时,我的外键始终为空。 Sequelize , Node ,Postgresql