我尝试在创建某些事件时使用多个云函数来触发。它们都写入Firebase数据库的不同位置。但我如何指定哪些云函数将被触发,然后检索将写入数据库的某些数据?
下面是我的代码。
My 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());
}
}
可能是什么问题?
发布于 2018-06-10 00:53:52
您有两个Cloud函数,在这两个路径上触发:
/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
https://stackoverflow.com/questions/50775588
复制相似问题