Firebaseデータベースの値が変更された場合にクラウド関数を作成しようとしていますが、Firebase通知を送信しますか?以下は私のfirebaseデータベースのスナップショットです... liveurlの変更に関する通知を送信したいようなものです
livestream
|_
liveurl: "https://www.youtube.com/watch?v=Z8agqyGIaD8"
これは、CloudFunctionsとCloudMessagingのユースケースの代表的な例です。実際、同様のシナリオは「クラウド関数で何ができるか」で説明されています。「何か面白いことが起こったときにユーザーに通知する」セクションのドキュメント:
- この関数は、フォロワーが格納されているリアルタイムデータベースパスへの書き込み時にトリガーされます。
- この関数は、FCMを介して送信するメッセージを作成します。
- FCMは、通知メッセージをユーザーのデバイスに送信します。
ありますGitHubの上のFCM通知クイックスタートサンプル利用できるが、このために。
簡単な例として、これを行うためのクラウド関数を次のように記述できます。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.liveUrlChange = functions.database.ref('/livestream/liveurl').onWrite((event) => {
// Exit if data is deleted.
if (!change.after.exists()) return null;
// Grab the current value of what was written to the Realtime Database.
const value = change.after.val();
console.log('The liveurl value is now', value);
// Build the messaging notification, sending to the 'all' topic.
var message = {
notification: {
title: 'Database change',
body: 'The liveurl value has changed to ' + value
},
topic: 'liveurl_changed'
};
// Send the message.
return admin.messaging().send(message)
.then((message) => {
console.log('Successfully sent message:', message);
})
.catch((error) => {
console.error('Error sending message:', error);
});
});
これは、クラウド機能の変化に反応する方法/livestream/liveurl
、データベース内のノードとするクラウドメッセージングを使用してトピックにメッセージを送信する(つまりliveurl_changed
)。
これを機能させるには、次のことを行う必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加