1

Firebase/Node JS経由でプッシュ通知を送信しようとしています。私は現在、このコードをHeroku上で実行しています。 fcm.send()が呼ばれるたびノードJSでのFirebase認証の失敗:NotAuthorizedError

var Firebase = require('firebase'); 
var request = require('request'); 
var express = require('express'); 
var FCM = require('fcm-node'); 
var app = express(); 

app.set('port', (process.env.PORT || 5000)); 

app.use(express.static(__dirname + '/public')); 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 

app.get('/push', function(request, response) { 
    response.send("running"); 
}); 

app.listen(app.get('port'), function() { 
    console.log('Node app is running on port', app.get('port')); 
}); 

// Planet Firebase API Key 
var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key 
var fcm = new FCM(API_KEY); 

var config = { 
    apiKey: "AIz...", 
    authDomain: "XXX.firebaseapp.com", // Authentication > Sign-In Method > OAuth Redirect Domains 
    databaseURL: "https://XXX.firebaseio.com/", // Database > Url at the top 
    storageBucket: "gs://XXX.appspot.com", // Storage > Url at the top 
}; 
var firebase = Firebase.initializeApp(config); 
var ref = firebase.database().ref(); 

function listenForNotificationRequests() { 
    var requests = ref.child('notifications'); 
    requests.on('child_changed', function(requestSnapshot) { 
     var objectAdded = requestSnapshot.val(); 
     var uid = receiver["uid"] 
     var notificationMessage = objectAdded["message"] 
     sendNotificationToUser(uid, notificationMessage); 
    }, function(error) { 
     console.error(error); 
    }); 
}; 

function sendNotificationToUser(receiverID, notificationMessage) { 
    var message = { 
     to: '/notifications/'+receiverID,  
     notification: { 
      title: "App Title", 
      body: notificationMessage, 
      badge: 1 
     } 
    }; 

    fcm.send(message, function(err, response){ 
     if (response) { 
      console.log("Successfully sent with response: ", response); 
     } else { 
      console.log("Something has gone wrong! Error: " + err); 
     } 
    }); 
} 

// start listening 
listenForNotificationRequests(); 

、私は戻って取得:

Something has gone wrong! Error: NotAuthorizedError 

これはFirebaseが正しく初期化されていないと信じてすることにつながるが、私は、リンクとキーを複数回チェックしました。何を私は間違ってやったのですか?あなたのコードで

+0

詳細なエラー応答がありますか? –

+0

残念ながら、私はしません。これが私に与えるすべてです。 – user1072264

+0

試しましたか?私の答えは意味する。サーバーキーの使用後にメッセージを送信できましたか? –

答えて

3

、私はこの部分(特別コメント)気づい:

// Planet Firebase API Key 
var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key 
var fcm = new FCM(API_KEY); 

おそらく401 - Authentication Errorを受けています。

FCMを使用する場合は、Web鍵ではなくサーバーキーを使用してください。
Firebase Console: プロジェクト設定>クラウドメッセージ>サーバーキーで表示されます。

異なるキーについては、私の答えhereを参照してください。

+0

それは助けに見えなかった。私はまだ認証エラーを取得しています。 – user1072264

+0

©user1072264 Odd。あなたは簡単なcURLを介して送信し、問題がまだ同じかどうか確認できますか? –

関連する問題