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

Posted

技术标签:

【中文标题】云功能执行成功,但通知未显示 android【英文标题】:Cloud Function executed successfully but notification not showing android 【发布时间】:2017-12-07 16:57:17 【问题描述】:

在一个安卓应用程序中,我使用 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);
            );   
            );
            );

请帮忙!提前致谢。

【问题讨论】:

您是否从 fire-base 控制台发送了消息。 是的,来自 firebase 控制台它的工作。 您在仪表板的 Cloud Functions 日志中收到什么消息? 这是我得到的响应结果:[ error: [Object] ], canonicalRegistrationTokenCount: 0, failureCount: 1, successCount: 0, multicastId: 5973854734577752000 函数执行耗时 1497 ms,完成状态:'ok' 您确定要发送到相应的令牌吗?您能否也将您的代码发布为onMessageReceived() 【参考方案1】:

遇到了同样的问题,由于未显示错误详细信息,即 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的主要内容,如果未能解决你的问题,请参考以下文章

IOS Firebase 云功能中未收到推送通知

FCM iOS 项目未收到通知 - FCM 控制台显示成功但设备未收到

收到 Firebase 注册令牌但未显示通知

带有自定义声音的推送通知未在 iOS 中显示

android firebase reg id recived但通知未显示在手机显示屏中

Paypal 交易未显示在沙箱中,但出现在通知中