-1

私はfirebaseデータベースに何か追加した後にプッシュ通知を送ります。"プッシュ通知を送信しようとすると、証明書オブジェクトに" private_key "プロパティの文字列が含まれている必要があります

これはthis blogを参照して行っています。何かがfirebaseでデータベースを作成するためのコードの下には、私のデータベース

に追加された場合、私はプッシュ通知を与えるためにしようとしている:

// I'm passing my message content and user here it will create a child in database 

public static void sendNotificationToUser(String user, final String message) { 
     DatabaseReference ref; 
     ref = FirebaseDatabase.getInstance().getReference(); 
     final DatabaseReference notifications = ref.child("notificationRequests"); 

     Map notification = new HashMap<>(); 
     notification.put("username", user); 
     notification.put("message", message); 

     notifications.push().setValue(notification); 
    } 

これは私のindex.jsスクリプトコードです:

var firebase = require('firebase-admin'); 
var request = require('request'); 

var API_KEY = ".AAA...iEmIG"; 

var serviceAccount = require(".google-services.json"); 

firebase.initializeApp({ 
    credential: firebase.credential.cert(serviceAccount), 
    databaseURL: "https://chitchatapp-73060.firebaseio.com/" 
}); 
ref = firebase.database().ref(); 

function listenForNotificationRequests() { 
    var requests = ref.child('notificationRequests'); 
    requests.on('child_added', function (requestSnapshot) { 
     var request = requestSnapshot.val(); 
     sendNotificationToUser(
      request.username, 
      request.message, 
      function() { 
       requestSnapshot.ref.remove(); 
      } 
     ); 
    }, function (error) { 
     console.error(error); 
    }); 
}; 

function sendNotificationToUser(username, message, onSuccess) { 
    request({ 
     url: 'https://fcm.googleapis.com/fcm/send', 
     method: 'POST', 
     headers: { 
      'Content-Type': ' application/json', 
      'Authorization': 'key=' + API_KEY 
     }, 
     body: JSON.stringify({ 
      notification: { 
       title: message 
      }, 
      to: '/topics/user_' + username 
     }) 
    }, function (error, response, body) { 
     if (error) { console.error(error); } 
     else if (response.statusCode >= 400) { 
      console.error('HTTP Error: ' + response.statusCode + ' - ' + response.statusMessage); 
     } 
     else { 
      onSuccess(); 
     } 
    }); 
} 
listenForNotificationRequests(); 

私はこれを展開しようとすると、以下のようなエラーが表示されます。どうすればその問題を解決できますか?

Error: Error occurred while parsing your function triggers. 

Error: Certificate object must contain a string "private_key" property. 
    at FirebaseAppError.FirebaseError [as constructor] (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\utils\error.js:25:28) 
    at new FirebaseAppError (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\utils\error.js:70:23) 
    at new Certificate (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\auth\credential.js:108:19) 
    at new CertCredential (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\auth\credential.js:174:33) 
    at Object.cert (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\firebase-namespace.js:175:58) 
    at Object.<anonymous> (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\index.js:12:37) 
    at Module._compile (module.js:569:30) 
    at Object.Module._extensions..js (module.js:580:10) 
    at Module.load (module.js:503:32) 
    at tryModuleLoad (module.js:466:12) 

原因は何ですかこのエラーはどのように解決できますか?

+0

1つの提案は、任意のAPIキーや機密がxxxxx' 'として計上しなければならないということです。 –

+0

あなたは何を意味するのですか? – HemalHerath

+0

あなたのAPIキーは機密データなので、そのまま投稿しないでください。 –

答えて

2

google-services.jsonファイルに問題があるようです。

ブログの記事がダウンロードされたので、そのファイルをダウンロードしましたか?もしそうなら、どこに置いたのですか?そしてあなたはファイルを置くマッチのパスを確かめていますか?

var serviceAccount = require("./google-services.json"); 

あなたのコメントでは、クラウド機能を使用していると言います。その場合、コードはGoogleの環境で実行され、google-services.jsonは必要ありません。このサンプルから:

firebase.initializeApp(functions.config().firebase); 
+0

ええ、私はそれをダウンロードしました。その関数のフォルダ内 – HemalHerath

+0

その場合、あなたのパスが間違っているように見えます。私が与えたパスを試してみるか、更新された答えを使用してください。これは、Firebaseのクラウド機能でこれを試しているとずっと簡単です。 –

+0

私はそれを試みたが、展開していない。どうすればいいのですか – HemalHerath

関連する問題