0

관리자가 특정 주제에 등록 된 사용자에게 경고를 보낼 수있는 모바일 응용 프로그램 (Android 및 iOS 용)을 배포하고 있습니다. 이를 위해 Realtime Database를 사용하여 알림 및 클라우드 기능을 저장하여 주제에 대한 알림을 보냅니다.Firebase Admin SDK sendToTopic이 작동하지 않습니다.

는 I는 다음과 클라우드 기능을 배치했습니다 내가 실시간 데이터베이스에 새 메시지를 삽입 할 때

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 
} 

은, 위의 기능이 올바르게 해당 메시지를 가져오고 로그 섹션에서합니다 (중포 기지 콘솔) 나는 올바른 사용자 지정 로그와 status 'ok'이라는 로그를 참조하십시오.

에도 불구하고 아무런 알림도 장치에 도착하지 않습니다. firebase 콘솔에서 똑같은 항목을 직접 테스트하면 장치가 올바르게 등록되므로 제대로 작동합니다.

내가 누락 된 클라우드 기능에 문제가 있습니까?

답변

0

적어도 iOS의 경우 페이로드가 data 인 경우에만 // contentAvailable: true의 주석을 제거해야한다고 생각합니다. 그렇게하면 앱 코드에서 직접 알림을 표시하고 실행할 수 있습니다. 데이터 페이로드를 처리하지 않고도 알림을 표시하려면 페이로드에 notification 객체를 전달해야합니다.

알림은 다음 필드로 제한됩니다. https://firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload