0

私はアンドロイドクライアントアプリケーション管理アプリケーションをFirebaseを使用しています。ユーザーがクライアントアプリケーションに登録するたびに、Adminアプリケーションにプッシュ通知を送信する必要があります。私はCloud Functions for Firebaseを使用しようとしています。私はその関数をエクスポートしました。そして、私はfirebaseコンソールでもそれを見ることができます。Google Cloud機能とGoogle Firebaseを使用したプッシュ通知

enter image description here

これは、ここに私のindex.js

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 


exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}/{regToken}').onWrite(event => { 

    if (event.data.previous.exists()) { 
    return; 
    } 

    const userId = event.params.userId; 
    const regToken = event.params.regToken; 

    // Notification details. 
    const payload = { 
     notification: { 
     title: 'You have a new User.', 
     body: `${userId} is the id.`, 
     } 
    }; 
    return admin.messaging().sendToDevice(regToken, payload); 
}); 

である私のデータベース構造はfirebaseである:

enter image description here

私はプッシュを送信するために、任意のオンラインポータルを使用している場合またはテスト目的のための管理アプリにプッシュを送信するFCMさえ、私は押す。しかし、このクラウド機能はプッシュを送信していません。私がやっていることが間違っている人を案内することができますか?

EDIT

私は、次の機能を変更すると、それが動作します。しかし、私はまだ上記の機能がうまくいかないのかと疑問に思っています。元のコードで

exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}').onWrite(event => { 


    if (event.data.previous.exists()) { 
    return; 
    } 

    const userId = event.params.userId; 
    var eventSnapshot = event.data; 
    const regToken = eventSnapshot.child("regToken").val(); 

    Notification details. 
    const payload = { 
     notification: { 
     title: 'You have a new User.', 
     body: `${userId} is the id.`, 
     } 
    }; 
    return admin.messaging().sendToDevice(regToken, payload); 
}); 
+0

成功またはエラー応答を受け取っていますか? –

+0

FirebaseコンソールのLogsペインでlog文を追加し、出力を探します。 'console.log( 'userId:'、userId、 'token:'、regToken);' –

+0

Firebaseのログ出力は何を表していますか? Firebaseコンソールの 'Functions - > Logs'にあります。 –

答えて

1

、あなたが持っている:

const regToken = event.params.regToken; 

event.params.regTokenは、それがあなたの参考にwildcard path segmentの値を返すregTokenの値を返しません。

関連する問題