내 잡지 앱의 경우 Firebase 서비스를 사용하고 있습니다.이 안드로이드 앱의 한 기능은 새 문서가 게시 될 때마다 하나의 기능으로 모든 장치에 새로운 알림이 전송됩니다 . FCMToken :Firebase Cloud 등록 된 모든 장치에 알림 전송을위한 메시징
이 같은 DB에있는 모든 장치 토큰을 저장하고있다 : 새로운 노드가 중포 기지 DB에서 "게시"키에 추가됩니다 그래서 때마다
는, FCM 기능이 실행됩니다 { 사용자 ID deviceToken } 및 메시지가 모든 장치에 전송됩니다
다음는 FCM 기능을위한 자바 스크립트에 내 코드입니다 : 어떤 이유
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/published/{msg_id}').onWrite(event => {
const snapshot = event.data;
// Only send a notification when a new message has been created.
if (snapshot.previous.val()) {
return;
}
const msg_id = event.params.msg_id;
const msg_val=admin.database().ref(`messages/${msg_id}`).once('value');
return msg_val.then(msgResult =>{
const msg_title=msgResult.val().title;
const user_id=msgResult.val().userId;
console.log('msg title is',msg_title);
console.log('We have a new article : ', msg_id);
const payload={
data : {
title:"New Article",
body: msg_title,
msgid : msg_id,
userid : user_id
}
};
// const deviceToken = admin.database().ref('/FCMToken/{user_id}').once('value');
admin.database().ref('/FCMToken').on("value", function(dbsnapshot)
{
dbsnapshot.forEach(function(childSnapshot) {
//var childKey = childSnapshot.key;
const childData = childSnapshot.val();
const deviceToken=console.log("device token" + childSnapshot.val());
return admin.messaging().sendToDevice(childData,payload).then(response=>{
console.log("This was notification feature")
console.log("response: ", response);
})
.catch(function(error)
{
console.log("error sending message",error)
});
});
});
});
});
알림입니다 단 하나의 장치 (FCM 노드의 첫 번째 토큰)로만 전송됩니다. 업데이트 : 코드를 업데이트하고 약속을 사용했지만 몇 가지 이유로 여전히 작동하지 않고 첫 번째 장치 토큰에 알림을 보냅니다.
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/published/{msg_id}').onWrite(event => {
const snapshot = event.data;
// Only send a notification when a new message has been created.
if (snapshot.previous.val()) {
return;
}
const msg_id = event.params.msg_id;
const msg_val=admin.database().ref(`messages/${msg_id}`).once('value');
return msg_val.then(msgResult =>{
const msg_title=msgResult.val().title;
const user_id=msgResult.val().userId;
console.log('msg title is',msg_title);
console.log('We have a new article : ', msg_id);
const payload={
data : {
title:"New Article",
body: msg_title,
msgid : msg_id,
userid : user_id
}
};
const promises=[];
// const deviceToken = admin.database().ref('/FCMToken/{user_id}').once('value');
admin.database().ref('/FCMToken').once('value').then(function(dbsnapshot)
{
dbsnapshot.forEach(function(childSnapshot) {
//var childKey = childSnapshot.key;
const childData = childSnapshot.val();
const deviceToken=console.log("device token" + childSnapshot.val());
const promise = admin.messaging().sendToDevice(childData,payload).then(response=>{
promises.push(promise)
console.log("This was notification feature")
console.log("response: ", response);
})
return Promise.all(promises)
.catch(function(error)
{
console.log("error sending message",error)
});
});
});
});
});
응답 개체는이 출력을 제공한다 : 응답 {결과 : {오류 [개체]}] canonicalRegistrationTokenCount : 0 failureCount : 1 successCount : 0 multicastId : 6411440389982586000}
모든 장치의 FCM 토큰을 사용하여 전송하면, 특정 주제에 가입 한 후 알림을 보내 대신. 더 효율적입니다. – kunwar97