【发布时间】:2020-09-24 07:58:36
【问题描述】:
我已经为我的 Android 应用上传了这个云功能到 Firebase Functions。
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
//Get name
const fromUser = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
var from_user_id = fromUserResult.val().from;
if(from_user_id === null){
from_user_id = "BeBetter"
}
console.log('You have a new notification from :' + from_user_id);
const userQuery = admin.database().ref(`/Users/${from_user_id}/name`).once('value');
return userQuery.then(userResult => {
const userName = userResult.val();
const afterData = change.after.val();
//New Type of Notification.
if (afterData.type === "friend request") {
const userToken = admin.database().ref('Users/' + user_id + '/user_token').once('value');
return userToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request",
body: `${userName} sent you a Friend Request! <3`,
icon: "default",
click_action : "BeBetter_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendToDevice(token_id,payload).then(response => {
return console.log('This was the notification feature for friend request');
});
});
//New Type of Notification.
} else if(afterData.type === "friend request accepted") {
const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
return userToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request Accepted",
body: `You've got a new friend! ${userName} <3`,
icon: "default",
click_action : "BeBetter_TARGET_NOTIFICATION"
},
};
return admin.messaging().sendToDevice(token_id,payload).then(response => {
return console.log('This was the notification feature for friend request accept');
});
});
//New Type of Notification.
} else if(afterData.type === "experience invite") {
const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
return userToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Experience Invite!",
body: `${userName} invited you to an experience!`,
icon: "default",
click_action : "BeBetter_TARGET_NOTIFICATION"
},
};
return admin.messaging().sendToDevice(token_id,payload).then(response => {
return console.log('This was the notification feature for experience invite');
});
});
}else if(afterData.type === "experience completed") {
const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
return userToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Experience Completed!",
body: `${userName} joined! <3`,
icon: "default",
click_action : "BeBetter_TARGET_NOTIFICATION"
},
};
return admin.messaging().sendToDevice(token_id,payload).then(response => {
return console.log('This was the notification feature for experience completed');
});
});
} else {
console.log('Not a friend request');
return null;
}
});
});
});
我的 iOS 应用可以使用相同的功能吗?如何?我还没有找到一个很棒的指南。 我的 AppDelegate,是超标准的。我已将其设置为能够发送云消息。但是希望它在我写入数据库的通知部分时自动发送通知。 最好的问候
【问题讨论】:
标签: ios swift firebase google-cloud-functions