我尝试在创建某些事件时触发多个云函数。它们都写入Firebase数据库的不同位置。但是,如何指定将触发哪些云函数,然后检索将写入数据库的某些数据?
以下是我的代码。
我的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());
}
}
可能是什么问题?
答案 0 :(得分:1)
您有两个云功能,触发这两条路径:
/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