我在我的应用程序中进行了一对一的聊天功能,用户可以在其中发送消息但是医生和患者之间正在进行聊天,因此医生和应用程序的不同应用程序都有不同的应用程序Firebase中的同一个数据库。
所以我所做的就是在患者应用程序中编写了一个云功能来发送 将新消息发送到Doctor应用程序时推送通知。
//sends notification from patient app to Therapist
exports.sendNotifications = functions.database.ref('/conversation/{conversationId}/messages/{messageId}').onWrite(event =>{
const snapshot = event.data;
if(snapshot.previous.val()){
return;
}
var user_col = admin.database().ref('/users')
//Notification details
const message_text = snapshot.val().message;
const sender_name = snapshot.val().name;
const receive_therapist_uid = snapshot.val().receive_therapist;
const receive_support_uid = snapshot.val().receive_support;
const sender_uid = snapshot.val().sender;
const attachmentType = snapshot.val().attachmentType;
const timewrite = snapshot.val().timewrite;
const conversation_Id = snapshot.val().conversation_id;
//therapist_id , therapist_name, therapist_email
const payload = {
data:{
title: sender_name,
username:sender_name,
iduser:sender_uid,
message:message_text ? (message_text.length <= 100 ? message_text : message_text.substring(0, 97) + '...') : 'sent a photo',
conversation_id:conversation_Id
}
};
//send notification to therapist
user_col.child(receive_therapist_uid).child('Fcm_id').once("value",function(dataSnapshot)
{
const receive_therapist_fcm_id = dataSnapshot.val();
if(receive_therapist_fcm_id != null){
return admin.messaging().sendToDevice(receive_therapist_fcm_id,payload).then(response => {
const tokensToRemove = [];
response.results.forEach((result) =>{
const error = result.error;
if(error){
console.error('Failure sending notification to',receive_therapist_fcm_id, error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered'){
tokensToRemove.push(receive_therapist_fcm_id).remove();
}
}
});
return Promise.all(tokensToRemove);
});
}
});
});
但问题是除了在患者应用程序中发送通知功能之外还有许多其他功能,因此当我在Doctor应用程序中部署发送通知功能时,我的患者的其他云功能将被删除。我知道为什么会发生这种情况,因为这两个应用程序共享相同的数据库,所以有任何方式,以便我不会在患者应用程序中丢失我的其他云功能,我可以在Doctor应用程序中成功部署我的发送通知云功能。