0

マイコード:Firebase Firestore FCMメッセージ送信の問題

exports.fcmSend = functions.firestore.document('messages/{userId}').onCreate(event => { 
    console.log("fcm send method"); 
    const message = event.data.data(); 
    const userId = event.params.userId; 
    const token_id = 'asdfsadfdsafds'; 
    let token = ""; 
    const payload = { 
     notification: { 
      title: "Test", 
      body: "Test", 
      icon: "https://placeimg.com/250/250/people" 
     } 
    }; 
    db.collection('fcmTokens').doc('token_id').get().then((doc) => { 
     console.log(doc.id, '=>', doc.data()); 
     const data = doc.data(); 
     token = data.token; 
     console.log("token", token); 
    }) 
    .then(() => { 
     return event.data.ref.set({"title": "hello"}).sendToDevice(token, payload); 
    }) 
    .catch((err) => { 
     console.log('Error getting documents', err); 
     return err; 
    }); 
}); 

エラー:

Error getting documents TypeError: event.data.ref.set(...).sendToDevice is not a function
at db.collection.doc.get.then.then (/user_code/index.js:117:50)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

答えて

2

ここに関与する2つの別々のFirebaseの製品があります。

  1. クラウドFirestoreは、 FCMトークンをストロークする場所ユーザーのために。
  2. Cloud Messaging Admin SDKsend notifications to a userに使用します。

sendToDeviceメソッドは、クラウドメッセージング用のAdmin SDKにあり、起動しようとしているFirestoreデータベース参照ではありません。

あなたが最初に管理SDKをインポートする必要があります問題を解決するには、あなたのindex.js

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

は、その後、あなたはそれは次のようになりますステップ1と2のためにあなたの機能を変更します
// Load the tokens from Firestore 
db.collection('fcmTokens').doc('token_id').get().then((doc) => { 
    console.log(doc.id, '=>', doc.data()); 
    const data = doc.data(); 
    token = data.token; 
    console.log("token", token); 

    const payload = { 
      notification: { 
      title: 'hello', 
      } 
     }; 

    return admin.messaging().sendToDevice(token, payload) 
}) 
.catch((err) => { 
    console.log('Error getting documents', err); 
    return err; 
}); 
+0

再生いただきありがとうございます。問題があります –

関連する問題