2
我在Udacity中學習了這個教程,其中使用了Firebase的Cloud Functions來更改所添加數據的引用數據。 我想使用類似的功能,但發送推送通知訂閱主題的用戶。Firebase的雲端函數:將主題通知發送到Android設備不起作用
這是我正在使用的功能。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/messages/{pushId}/text').onWrite((event) => {
const data = event.data;
console.log('Message received');
if(!data.changed()){
console.log('Nothing changed');
return;
}else{
console.log(data.val());
}
const payLoad = {
notification:{
title: 'Message received',
body: 'You received a new message',
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60*60*2
};
return admin.messaging().sendToTopic("Message_Notifications", payLoad, options);
});
查看Firebase函數日誌我可以看到,當我向數據庫添加新值時,函數被調用。在我訂閱在MainActivity的話題
FirebaseMessaging.getInstance().subscribeToTopic("Message_Notification");
,並在數小時後,現在我可以看到我的話題火力通知控制檯列表中的主題。我從那裏發送通知,它工作。
我需要做的其他事情是每次添加新消息時都會顯示通知?
任何建議表示讚賞。我對Firebase相當陌生。所以,如果有更好的博客/文章關於它,請重定向我,以便我可以瞭解更多。
在此先感謝。
我在看很多東西。甚至沒有注意到我錯過了那一個。 感謝您耐心閱讀。需要改進我的證明閱讀。乾杯。 – raymanM
它發生在很多開發者身上。 :)與您的應用程序的祝你好運。乾杯! –