使用 FCM 旧服务器发送通知
了解如何使用 FCM 旧服务器发送通知。
警告 本页面已存档。有关最新信息,请参阅推送通知指南。
信息 有关如何与更新的 FCMv1 服务通信的文档,请参阅 使用 FCMv1 和 APNs 发送通知。本指南基于 Google 的文档,本节涵盖了让你入门的基础内容。
在通过 FCM 直接发送通知之前,你需要 获取设备令牌。
与 FCM 通信是通过发送 POST 请求完成的。不过,在发送或接收任何通知之前,你需要按照步骤配置 FCM以配置 FCM 并获取你的 FCM-SERVER-KEY。
🌐 Communicating with FCM is done by sending a POST request. However, before sending or receiving any notifications, you'll need to follow the steps to configure FCM to configure FCM and get your FCM-SERVER-KEY.
await fetch('https://fcm.googleapis.com/fcm/send', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `key=<FCM-SERVER-KEY>`, }, body: JSON.stringify({ to: '<NATIVE-DEVICE-PUSH-TOKEN>', priority: 'normal', data: { experienceId: '@yourExpoUsername/yourProjectSlug', scopeKey: '@yourExpoUsername/yourProjectSlug', title: "\uD83D\uDCE7 You've got mail", message: 'Hello world! \uD83C\uDF10', }, }), });
experienceId 和 scopeKey 字段是必填的。否则,你的通知将无法发送到你的应用。FCM 在 notification payload 中列出了支持的字段,你可以通过查看 Android 上的 FirebaseRemoteMessage 来了解 expo-notifications 支持哪些字段。
FCM 还提供了一些 服务器端库,支持几种不同的语言,你可以使用它们来替代原生的 fetch 请求。
🌐 FCM also provides some server-side libraries in a few different languages you can use instead of raw fetch requests.
如何查找 FCM 服务器密钥
🌐 How to find FCM server key
你的 FCM 服务器密钥可以通过确保你已经完成了配置步骤来找到,而不是将你的 FCM 密钥上传到 Expo,你可以直接在服务器中使用该密钥(如前面示例中的 FCM-SERVER-KEY)。
🌐 Your FCM server key can be found by making sure you've followed the configuration steps, and instead of uploading your FCM key to Expo, you would use that key directly in your server (as the FCM-SERVER-KEY in the previous example).
有效负载格式
🌐 Payload format
{ "token": native device token string, "collapse_key": string that identifies notification as collapsible, "priority": "normal" || "high", "data": { "experienceId": "@yourExpoUsername/yourProjectSlug", "scopeKey": "@yourExpoUsername/yourProjectSlug", "title": title of your message, "message": body of your message, "channelId": the android channel ID associated with this notification, "categoryId": the category associated with this notification, "icon": the icon to show with this notification, "link": the link this notification should open, "sound": boolean or the custom sound file you'd like to play, "vibrate": "true" | "false" | number[], "priority": AndroidNotificationPriority, // https://expo.nodejs.cn/versions/latest/sdk/notifications/#androidnotificationpriority "badge": the number to set the icon badge to, "body": { object of key-value pairs } } }
Firebase 通知类型
🌐 Firebase notification types
Firebase 云消息传递有两种类型的消息:通知消息和数据消息。
🌐 There are two types of Firebase Cloud Messaging messages: notification and data messages.
- 通知 消息仅由 Firebase 库处理(并显示)。它们不一定会唤醒应用,并且
expo-notifications不会知道你的应用已收到任何通知。 - 数据 消息不由 Firebase 库处理。它们会立即交给你的应用进行处理。在这里,
expo-notifications会解释数据负载并根据这些数据采取相应的操作。在几乎所有情况下,这都是你必须发送的通知类型。
如果你通过 Firebase 直接发送 notification 类型的消息而不是 data 类型,你将无法知道用户是否与通知进行了交互(没有可用的 onNotificationResponse 事件),并且你将无法在与你的通知事件相关的监听器中解析通知负载中的任何数据。
🌐 If you send a message of type notification instead of data directly through Firebase, you won't know if a user interacted with the notification (no onNotificationResponse event available), and you won't be able to parse the notification payload for any data in your notification event-related listeners.
当你需要一个
expo-notifications尚未提供的配置选项时,使用通知类型消息可能是有益的。通常来说,它可能比使用数据类型消息导致更不可预测的情况。然而,你可能需要将遇到的任何问题直接报告给谷歌。
以下是使用 Node.js Firebase Admin SDK 发送数据类型消息而不是通知类型的每种类型的示例:
🌐 Below is an example of each type using Node.js Firebase Admin SDK to send data-type messages instead of notification-type:
const devicePushToken = /* ... */; const options = /* ... */; // ❌ The following payload has a root-level notification object and // it will not trigger expo-notifications and may not work as expected. admin.messaging().sendToDevice( devicePushToken, { notification: { title: "This is a notification-type message", body: "`expo-notifications` will never see this 😢", }, data: { photoId: 42, }, }, options ); // ✅ There is no "notification" key in the root level of the payload // so the message is a "data" message, thus triggering expo-notifications. admin.messaging().sendToDevice( devicePushToken, { data: { title: "This is a data-type message", message: "`expo-notifications` events will be triggered 🤗", // ⚠️ Notice the schema of this payload is different // than that of Firebase SDK. What is there called "body" // here is a "message". For more info see: // https://expo.nodejs.cn/versions/latest/sdk/notifications/#android-push-notification-payload-specification body: // As per Android payload format specified above, the JSON.stringify({ photoId: 42 }), // additional "data" should be placed under "body" key. }, }, options );