Android 设备未收到推送通知

时间:2021-06-03 19:40:54

标签: android firebase-cloud-messaging

我的 Android 设备没有收到 FCM 推送通知。我已经在 Firebase Cloud Messaging 控制台上进行了测试,并收到了测试通知。但是,当我尝试通过更改文档来触发推送通知来发送时,我没有在物理设备或模拟器上收到通知。我没有收到错误消息,日志显示消息发送“正常”。

是否需要在模拟器、物理设备或 Firebase 端进行任何设置?

这是有效的,但在某些时候它停止了。我进行了很多更改,因此无法回溯。

这是函数的代码。

/*  eslint-disable */
const functions = require("firebase-functions");
//const addTrxns = require("./trxnAdd.js");
const admin = require("firebase-admin");
const { user } = require("firebase-functions/lib/providers/auth");
const { event } = require("firebase-functions/lib/providers/analytics");

//exports.addTrxns.addTrxns;

admin.initializeApp();

const db = admin.firestore();

/* ====================   USER PROFILE CHANGED   ============================================ */

exports.userProfileChanged = functions.firestore.document('/agents/{userId}').onWrite( async (change, context) => {

    const userId = context.params.userId;
    const afterData = change.after.data();
    const agentId = afterData.agentId;

    console.log('A change has been made to ' + userId + ' profile');

       /**** GET DEVICE INFORMATION ****/
    const deviceDoc = db.collection('device').doc(agentId);
    if (deviceDoc == null) {
        console.log('No device document found');
    } 
    const deviceData = await deviceDoc.get();  
    const deviceId = deviceData.get('token');
    console.log('deviceId: ', deviceId);
    
    if (deviceId == null) {
        return console.error('No device tokens found');
    }

    let title = "Changes have been made your Tonnah profile";
    let body = "Changes have been made to your user profile";

    const payload = {

        notification: { title: title, body: body},
        data: {click_action: 'FLUTTER_NOTIFICATION_CLICK' }

    };

    const response = await admin.messaging().sendToDevice(deviceId, payload);

    if (response.error) {

        console.error("Error sending message: ", response.error);

    } else {

        return console.log("Message sent successfully!");

    };

    return Promise.all(console.log('End of function'));

});

/* ====================   ADD NEW TRXN   ==================================================== */

exports.onTrxnCreate = functions.firestore.document('/trxns/{trxnId}').onCreate(async(snap, context) => {

    /*const userId = context.params.userId;*/
    
    const agentId = snap.data().agentId;
    console.log('A new transaction has been added');
    
        /**** GET DEVICE INFORMATION ****/
    const deviceDoc = db.collection('device').doc(agentId);
    if (deviceDoc == null) {
        console.log('No device document found');
    } 
    const deviceData = await deviceDoc.get();  
    const deviceId = deviceData.get('token');
    console.log('deviceId: ', deviceId);
    
    if (deviceId == null) {
        return console.error('No device tokens found');
    }

    /**** GET TRXN INFORMATION ****/
    const trxnId = context.params.trxnId;
    /*console.log('trxnId');*/
    const trxnDoc = db.collection('trxns').doc(trxnId);
    if (trxnDoc == null) {
        console.log('No trxn document found');
    } 
    const trxnData = await trxnDoc.get();  
    const clientFName = trxnData.get('clientFName');
    const clientLName = trxnData.get('clientLName');
    console.log('Trxn: ' + clientFName + ' ' + clientLName + ' updated ');

    let title = "Transaction added";
    let body = "A new transaction has been added for " + clientFName + ' ' + clientLName;

    const payload = {

        notification: { title: title, body: body},
        data: {click_action: 'FLUTTER_NOTIFICATION_CLICK', screen: 'TransactionDetailScreen' }

    };

    const response = await admin.messaging().sendToDevice(deviceId, payload);

    if (response.error) {

        console.error("Error sending message: ", response.error);

    } else {

        return console.log("Message sent successfully!");

    };

    return Promise.all(console.log('End of function'));

});

/* ====================   UPDATE TRXNS   ==================================================== */

exports.onTrxnUpdate = functions.firestore.document('/trxns/{trxnId}').onUpdate(async (change, context) => {
    
    const afterData = change.after.data();
    const agentId = afterData.agentId;
    /*console.log('context: ', context);*/
    console.log('A transaction has been updated');
    
    /**** GET DEVICE INFORMATION ****/
    const deviceDoc = db.collection('device').doc(agentId);
    /*console.log('deviceDoc: ', deviceDoc);*/
    if (deviceDoc == null) {
        console.log('No device document found');
    } 
    
    const deviceData = await deviceDoc.get(); 
    const deviceId = deviceData.get('token');
    /*console.log('deviceId: ', deviceId);*/
    
    if (deviceId == null) {
        return console.error('No device tokens found');
    }

    /**** GET TRXN INFORMATION ****/
    const trxnId = context.params.trxnId;
    /*console.log('trxnId');*/
    const trxnDoc = db.collection('trxns').doc(trxnId);
    if (trxnDoc == null) {
        console.log('No trxn document found');
    } 
    const trxnData = await trxnDoc.get();  
    const clientFName = trxnData.get('clientFName');
    const clientLName = trxnData.get('clientLName');
    console.log('Trxn: ' + clientFName + ' ' + clientLName + ' updated ');

    let title = "Transaction updated";
    let body = "Trxn: " + clientFName + " " + clientLName + " updated ";

    const payload = {

        notification: { title: title, body: body},
        data: {click_action: 'FLUTTER_NOTIFICATION_CLICK' }

    };

    const response = await admin.messaging().sendToDevice(deviceId, payload);

    if (response.error) {

        console.error("Error sending message: ", response.error);

    } else {

        return console.log("Message sent successfully!");

    };

    return Promise.all(console.log('End of function')); 

});

0 个答案:

没有答案