首页 > 解决方案 > 远程交互式推送通知 - 如何?

问题描述

TL;DR 如何通过 Expo 的 Push API 发送交互式通知

请提供以下信息:

SDK 版本:40 平台(Android/iOS/web/all):全部使用以下代码,我创建了一个类别并使用getNotificationCategoriesAsync,确认它的存在。

import Constants from "expo-constants";
import * as Notifications from "expo-notifications";

// Set up Category
  Notifications.setNotificationCategoryAsync("basic", [
    { identifier: "Yes", buttonTitle: "Yes " },
    { identifier: "No", buttonTitle: "No " },
  ])

// Check category is there
  Notifications.getNotificationCategoriesAsync().then((categories) => {
  console.log(categories)});

// Get experienceId
  experienceId = Constants.manifest.id;
  console.log(experienceId);

在 API 方面,我使用了下面的请求并将@username/appName 替换为上面记录的内容(experienceId)。我已经在 ios 和 android 的真实设备和模拟器上尝试过这个,即使通知确实发送了,它也缺少交互方面。

curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
  "to": "ExponentPushToken[XXXXXXXXXXXXXXXX]",
  "title":"hello",
  "body": "world",
  "_category":"@username/appName:basic",
}'

我还尝试将“_category”替换为“categoryId”或“categoryIdentifier”或“category”。这些都没有奏效。有没有人能够成功地做到这一点!如果有怎么办!提前致谢

标签: react-nativepush-notificationexpo

解决方案


你真的很亲密,experienceId 和 category 用连字符“-”连接起来。查看 iOS 上的源代码可以看到格式字符串:

https://github.com/expo/expo/blob/fd47709156ed218cd5a8c5093df113918bdb2700/ios/versioned-react-native/ABI40_0_0/Expo/ExpoKit/Core/UniversalModules/EXNotifications/ABI40_0_0EXScopedNotificationBuilder.m#L33

curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
  "to": "ExponentPushToken[XXXXXXXXXXXXXXXX]",
  "title":"hello",
  "body": "world",
  "_category":"@username/appName-basic",
}'

推荐阅读