【问题标题】:Multiple Firebase Functions not working多个 Firebase 功能不起作用
【发布时间】:2018-11-19 09:31:47
【问题描述】:

我尝试在创建某些事件时使用多个 Cloud Functions 来触发。它们都写入 Firebase 数据库的不同位置。但是如何指定将触发哪些 Cloud Functions,然后检索将写入数据库的某些数据?

下面是我的代码。

我的 JS

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

admin.initializeApp();

exports.sendNotification = functions.database.ref('/Lecture_Materials/{MIS}/{Systems_Devt}/{Systems_DevtId}/name')
.onWrite(( change,context) =>{

// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str1 = "Lecture material uploaded: " + eventSnapshot;
console.log(eventSnapshot);

var topic = "Management.Information.System";
var payload = {
    data: {
        name: str1,
    }
};

// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
    .then(function (response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return;
    })
    .catch(function (error) {
        console.log("Error sending message:", error);
    });
});

exports.sendNotification1 = functions.database.ref('/Lecture_Materials/{MIS}/{Enterprise_Sys}/{Enterprise_SysId}/name')
.onWrite(( change,context) =>{

// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str1 = "Lecture material uploaded: " + eventSnapshot;
console.log(eventSnapshot);

var topic = "Management.Information.System";
var payload = {
    data: {
        name1: str1,
    }
};

// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
    .then(function (response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return;
    })
    .catch(function (error) {
        console.log("Error sending message:", error);
    });
});

我的 onMessage 已收到

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        showNotification(remoteMessage.getData().get("name"));
        showNotification1(remoteMessage.getData().get("name1"));
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {

    }
}

private void showNotification1(String name1) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Lecture Notes(Enterprise Systems)")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText(name1)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

private void showNotification(String name) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Lecture Notes(System Dev't)")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText(name)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

可能是什么问题?

【问题讨论】:

    标签: javascript android firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您有两个 Cloud Functions,在这两个路径上触发:

    /Lecture_Materials/{MIS}/{Systems_Devt}/{Systems_DevtId}/name
    /Lecture_Materials/{MIS}/{Enterprise_Sys}/{Enterprise_SysId}/name
    

    包裹在{} 中的每个段都定义了一个变量。如果删除变量名,则这两个都是/Lecture_Materials/*/*/*/name。这意味着就云函数而言,这些函数在同一路径上触发。

    如果你想让每个函数触发不同的操作,你需要保证路径是唯一的。一个典型的方法是使用这样的路径:

    /Lecture_Materials/Systems_Devt/{MIS}/{Systems_Devt}/{Systems_DevtId}/name
    /Lecture_Materials/Enterprise_Sys/{MIS}/{Enterprise_Sys}/{Enterprise_SysId}/name
    

    如果{Systems_Devt} 和您当前的路径实际上不是通配符,而是文字字符串,您还可以保留当前路径并删除那些{}

    /Lecture_Materials/{MIS}/Systems_Devt/{Systems_DevtId}/name
    /Lecture_Materials/{MIS}/Enterprise_Sys/{Enterprise_SysId}/name
    

    【讨论】:

    • 非常感谢弗兰克。我的 OnMessageReceived 类呢?对吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2021-08-03
    • 2018-08-13
    相关资源
    最近更新 更多