在 react-native 中从客户端站点发送 firebase 推送通知的最简单方法

easiest way to send firebase push notification from client site in react-native

我是 React Native 的新手,我无法从客户端发送推送通知。我有每个用户的 FCM 令牌,但需要向特定用户发送通知。如果你想要更清楚,请评论并让我知道这个

的解决方案

我看到了你的问题,我有一个解决方案给你。我已经在做了,这是我的代码。

export const sendPushNotification = async (token, title, body,) => {
//console.log("token==>", token);

const FIREBASE_API_KEY ="<your firebase cloud messaging server key>"

const message = {
  registration_ids: [token],
  notification: {
    title: title,
    body: body,
    vibrate: 1,
    sound: 1,
    show_in_foreground: true,
    priority: "high",
    content_available: true
  },
};

let headers = new Headers({
  "Content-Type": "application/json",
  Authorization: "key=" + FIREBASE_API_KEY
});

let response = await fetch("https://fcm.googleapis.com/fcm/send", {
  method: "POST",
  headers,
  body: JSON.stringify(message)
});
   // console.log("=><*", response);
   response = await response.json();
  //  console.log("=><*", response);
};

你可以像这样使用这个函数

sendPushNotification(
    FCMToken,
    'title of message',
    `this is body of message`,
  );

希望对你有用