android - 云功能执行成功但通知未显示android

标签 android firebase firebase-cloud-messaging google-cloud-functions

在安卓应用中,我使用 FCM 发送通知,云函数执行成功,如 firebase 控制台日志所示,但在我的设备中没有显示任何通知,可能是什么原因?

下面是我的 index.js 的代码

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}')
.onWrite(event => {
     console.log('notifying start1');
    const message = event.data.current.val();
    const senderUid = message.from;
    const receiverUid = message.to;
    console.log('SenderId '+senderUid + ' Receiver Id '+receiverUid);
    const promises = [];
 console.log('notifying start2');
    if (senderUid == receiverUid) {
        //if sender is receiver, don't send notification
        promises.push(event.data.current.ref.remove());
        return Promise.all(promises);
    }
      console.log('notifying start3');
    const getInstanceIdPromise = admin.database().ref(`/users/${receiverUid}/accessToken`).once('value');
      console.log('notifying start4');
    const getReceiverUidPromise = admin.auth().getUser(receiverUid);
console.log('notifying start5');
    return Promise.all([getInstanceIdPromise, getReceiverUidPromise]).then(results => {
        const accessToken = results[0].val();
        const receiver = results[1];
        console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);
        const payload = {
            notification: {
                title: 'Firebase Notification',
                body: message.body,
            }
        };
        admin.messaging().sendToDevice(accessToken, payload)
            .then(function (response) {
                console.log("Successfully sent message:", response);
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
            });   
            });
            });

求助!提前致谢。

最佳答案

遇到了同样的问题,但由于未显示错误详细信息,无法理解问题所在,即 { error: [Object] }

Successfully sent message: { results: [ { error: [Object] } ], 
                             canonicalRegistrationTokenCount: 0, 
                             failureCount: 1, 
                             successCount: 0, 
                             multicastId: 5487635521698134000 
                           }

因此在云功能代码中更改/添加了一个日志以访问错误详细信息,即 console.log(response.results[0].error);

代码(在云函数中):

admin.messaging().sendToDevice(registrationToken, payload)
            .then(function(response) {
                console.log("Successfully sent message:", response);
                console.log(response.results[0].error);
            })
            .catch(function(error) {
                console.log("Error sending message:", error);
            });

错误详情:

{ Error: 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.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:130:23)
at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:154:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:80:63
at Array.forEach (native)
at mapRawResponseToDevicesResponse (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:76:26)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:223:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

errorInfo: 
   { 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.' } }

不确定你是否有和我一样的错误...

关于android - 云功能执行成功但通知未显示android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44902241/

相关文章:

android - iframe 标签在最新版本的 Ionic 中不起作用

Firebase 存储设置速率限制

android - FCM 云消息传递不再注册新主题

android - 如何从android中的子 fragment 更新 fragment 中的 View

java - 如何在java中设置android应用程序主题,

Java 模块没有 `api` ,必须使用 `compile` ?

typescript - Firestore 在对象数组字段中查找具有特定对象的文档

ios - 使用 Firebase 过滤搜索?

ios - 向除了触发通知的用户之外的所有人发送通知?

javascript - 使用 FCM 向 ios 应用程序包 id 发送通知