Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用两个Firebase项目发送推送通知_Node.js_Firebase_Push Notification_Firebase Cloud Messaging_Firebase Notifications - Fatal编程技术网

Node.js 使用两个Firebase项目发送推送通知

Node.js 使用两个Firebase项目发送推送通知,node.js,firebase,push-notification,firebase-cloud-messaging,firebase-notifications,Node.js,Firebase,Push Notification,Firebase Cloud Messaging,Firebase Notifications,我正在使用node.js的AdminSDK发送推送通知。遵循并使用与此链接类似的示例初始化多个项目 我需要知道如何使用with node.js发送两个项目的推送通知。使用以下方法发送通知:两个项目使用默认项目,但另一个项目出现如下错误 exports.send_test_mailer = function(req, res) { // Default project var registrationToken = ["f-vRsDouUFQ:APA91bGktVzu3WjKGqeXqdi

我正在使用node.js的AdminSDK发送推送通知。遵循并使用与此链接类似的示例初始化多个项目

我需要知道如何使用with node.js发送两个项目的推送通知。使用以下方法发送通知:两个项目使用默认项目,但另一个项目出现如下错误

exports.send_test_mailer = function(req, res) {
  // Default project
  var registrationToken = ["f-vRsDouUFQ:APA91bGktVzu3WjKGqeXqdiYPI8B0lQXs34TkJS4p7LaMiFGfp5LdfB1ZjEhO3CY5ci92apqgt1hEJY0ml11C4hxYUaPfDl7PeDHhcmDGur0JUx5l3M2mLEj30epwRBWVsE4xMSTls4f"];
  var payload = {
    notification: {
      title: "driver app",
      body: "driver app push notfications on the day."
    },
    data: {
      score: "850",
      time: "2:45"
    }
  };
  firebaseAdmin.messaging().sendToDevice(registrationToken, payload)
    .then(function(response) {
    console.log("Successfully sent message driver:", JSON.stringify(response));
  })
  .catch(function(error) {
    console.log("Error sending message driver:", JSON.stringify(error));
  });
  // Second project
  var registrationTokens = ["dzXRXUMIB5w:APA91bHSArtroO8M33IHxaslQTugTcEzJcfkbsXEhwbXbvVzBws-aqG4aqKNr37j8WpZev7lolX7cFQlAKYZ1QV_EgC6zTGeT41n3lvSpcDyBg6t4SZZaoPe7nUO9sbdcXA2KDguxAbk"];
  var payloads = {
    notification: {
      title: "customer app",
      body: "customer app push notfications on the day."
    },
    data: {
      score: "850",
      time: "2:45"
    }
  };
  firebaseAdmin.messaging().sendToDevice(registrationTokens, payloads)
    .then(function(response) {
    console.log("Successfully sent message customer:", JSON.stringify(response));
  })
  .catch(function(error) {
    console.log("Error sending message customer:", JSON.stringify(error));
  }); 
};
错误

{"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}},{"error":{"code":"messaging/mismatched-credential","message":"The credential used to authenticate this SDK does not have permission to send messages to the device corresponding to the provided registration token. Make sure the credential and registration token both belong to the same Firebase project."}}],"canonicalRegistrationTokenCount":0,"failureCount":2,"successCount":0,"multicastId":9014981858701063000}
var ServiceAccount = require("./path your default app file.json");
var ServiceAccount1 = require("./path your second app file.json");

var serviceAppConfig = {
  credential: firebaseAdmin.credential.cert(ServiceAccount),
  databaseURL: "https://your firebase default app url"
};

// Initialize the default app
var serviceApp = firebaseAdmin.initializeApp(serviceAppConfig);
//console.log(serviceApp.name);  // "[DEFAULT]"

// Retrieve services via the defaultApp variable...
var serviceAuth = serviceApp.auth();
var serviceDatabase = serviceApp.database();
// Get the Messaging service for the default app
global.serviceMessaging = firebaseAdmin.messaging();

var service1AppConfig = {
  credential: firebaseAdmin.credential.cert(ServiceAccount1),
  databaseURL: "https://your firebase url second app"
};

// Initialize another app with a different config
var service1App = firebaseAdmin.initializeApp(service1AppConfig, "App2 name");

// Use the otherApp variable to retrieve the other app's services
var service1Auth = service1App.auth();
var service1Database = service1App.database();
// Get the Messaging service for a given app
global.service1Messaging = firebaseAdmin.messaging(service1App);

这是我的答案

{"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}},{"error":{"code":"messaging/mismatched-credential","message":"The credential used to authenticate this SDK does not have permission to send messages to the device corresponding to the provided registration token. Make sure the credential and registration token both belong to the same Firebase project."}}],"canonicalRegistrationTokenCount":0,"failureCount":2,"successCount":0,"multicastId":9014981858701063000}
var ServiceAccount = require("./path your default app file.json");
var ServiceAccount1 = require("./path your second app file.json");

var serviceAppConfig = {
  credential: firebaseAdmin.credential.cert(ServiceAccount),
  databaseURL: "https://your firebase default app url"
};

// Initialize the default app
var serviceApp = firebaseAdmin.initializeApp(serviceAppConfig);
//console.log(serviceApp.name);  // "[DEFAULT]"

// Retrieve services via the defaultApp variable...
var serviceAuth = serviceApp.auth();
var serviceDatabase = serviceApp.database();
// Get the Messaging service for the default app
global.serviceMessaging = firebaseAdmin.messaging();

var service1AppConfig = {
  credential: firebaseAdmin.credential.cert(ServiceAccount1),
  databaseURL: "https://your firebase url second app"
};

// Initialize another app with a different config
var service1App = firebaseAdmin.initializeApp(service1AppConfig, "App2 name");

// Use the otherApp variable to retrieve the other app's services
var service1Auth = service1App.auth();
var service1Database = service1App.database();
// Get the Messaging service for a given app
global.service1Messaging = firebaseAdmin.messaging(service1App);

谢谢我需要
firebaseAdmin.messaging(service1App)@Mr.B。我可以投票给你答案,它工作得很好。