颤动:fcm推送通知在后台时不显示为横幅

时间:2020-05-30 10:00:26

标签: firebase flutter push-notification firebase-cloud-messaging

我已经应用了Firebase云功能来使用Firestore向用户发送通知,但是当我在后台仅接收任务栏中的通知并且它没有显示为横幅时,我该怎么办?

这是我使用javaScript的功能

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

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

var newData;

exports.messageTrigger = functions.firestore.document('messages/{messagesId}').onCreate(async (snapshot, context) => {


    if (snapshot.empty) {
        console.log('No Devices');
        return;
    }

    newData = snapshot.data();

    const deviceIdTokens = await admin
        .firestore()
        .collection('tokens')
        .get();

    var tokenm = [
        'cPtlXcAORAik20by1ewdD5:APA91bGwkCCRxFC4IaepI4N2XW0X_8SNYGV6H1OCE-BAFld5qxm7cQMMr9O5FoFAt8EKGkWhtfSms96ZqvUVP_T3W6-17KMff7H3m5SF0cw6FFyapxWxOco3J2srD6nYp_UQj5RA4cxX'
    ];

    for (var one of deviceIdTokens.docs) {
        tokenm.push(one.data().token);
    }
    var payload = {
        notification: {
            title: newData.title,
            body: newData.body,
            sound: 'default',
        },
        data: {
            message: newData.message,
            click_action: 'FLUTTER_NOTIFICATION_CLICK',
        },
    };

    try {
        const response = await admin.messaging().sendToDevice(tokenm, payload);
        console.log('Notification sent successfully');
    } catch (err) {
        console.log(err);
    }
});

0 个答案:

没有答案