![](/img/trans.png)
[英]Firebase Admin Java SDK ValueEventListener not working
[英]Firebase Admin SDK sendToTopic not working
我正在部署一個移動應用程序(適用於Android和iOS),管理員可以通過該應用程序向注冊了特定主題的用戶發送警報。 為此,我使用實時數據庫存儲警報,並使用雲功能將通知發送到主題。
我已經部署了以下雲功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNewAlertNotification = functions.database.ref('/alerts').onWrite(event => {
const getValuePromise = admin.database()
.ref('alerts')
.orderByKey()
.limitToLast(1)
.once('value');
return getValuePromise.then(snapshot => {
const { text, topics, author } = snapshotToArray(snapshot)[0];
const payload = {
data: {
title: 'Avviso',
body: text,
icon: 'ic_stat_notify',
sound: 'default',
color: '#F3E03B',
tag: 'alerts',
ticker: 'Nuovo avviso',
subtitle: 'Avvisi',
author: JSON.stringify(author)
}
};
const options = {
priority: 'high',
timeToLive: 60 * 60 * 24 * 2, // 48 hours
collapseKey: 'it.bmsoftware.caliup'
// contentAvailable: true
};
if (topics.length > 1) {
let condition = '';
topics.forEach((topic, index) => {
condition += `'${topic}' in topics`
if (index < topics.length - 1) {
condition += ' || '
}
});
console.log(`Sending alert to condition '${condition}' -> ${JSON.stringify(payload)}`);
return admin.messaging().sendToCondition(condition, payload, options);
} else if (topics.length === 1) {
let topic = topics[0];
console.log(`Sending alert to topic '${topic}' -> ${JSON.stringify(payload)}`);
return admin.messaging().sendToTopic(topic, payload, options);
} else {
console.log(`No topics found`);
}
});
});
const snapshotToArray = (snapshot) => {
let result = []
if (!snapshot || !snapshot.val())
return result
snapshot.forEach((childSnapshot) => {
let item = childSnapshot.val()
item.key = childSnapshot.key
result.push(item)
})
return result
}
當我在實時數據庫上插入新消息時,以上函數正確獲取了該消息,並且在日志部分(在Firebase控制台上)中,我看到了正確的自定義日志和status 'ok'
的日志。
盡管如此,設備上仍未收到任何通知。 如果我直接從Firebase控制台測試相同的主題,則效果很好,因此設備已正確注冊。
我缺少的雲功能有什么問題嗎?
我相信您應該取消注釋// contentAvailable: true
如果僅發送data
有效載荷(至少在iOS上),則為// contentAvailable: true
。 這樣,您將可以在應用程序代碼上自行顯示和觸發通知。 如果希望彈出通知而不必處理數據有效負載,則應在有效負載上傳遞notification
對象。
通知僅限於以下字段: https : //firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.