0

谁能看到我在这里做错了什么?我升级到 expo SDK 39 并且推送通知停止工作。我已经调试了几天,似乎无法弄清楚。我可以看到令牌和消息正确输入,并且我可以看到 EXPO 在设置下具有通知访问权限。在 EXPO 之前,我能够使用推送通知运行测试,所以不确定 SDK v 39 出现了什么问题。任何帮助将不胜感激。

当用户发送聊天消息时运行的函数

const handleMessageSend = (msg) => {
    const index = messages.length ? messages[messages.length - 1].index + 1 : 0;
    const chat = {
      key: uid,
      index,
      receiver: [match.id],
      ...msg[0],
    };
    addChat(chat);
    setMessages([chat].concat(messages));
    let matchExpoToken = match.info.pushNotificationToken;
    if (matchExpoToken != "") {
      let messageBody = user.infoData.fullName + " sent you a new message";
      PushNotifications(matchExpoToken, messageBody);
    }
};

PushNotifications.js

// import * as Notifications from "expo-notifications"; //I have tried both of the imports with no luck
// import { Notifications, Permissions } from "expo";

const PushNotifications = async (matchExpoToken, messageBody) => {
  console.log("matchExpoToken", matchExpoToken, messageBody);
  const message = {
    to: matchExpoToken,
    sound: "default",
    title: "New Message",
    body: messageBody,
    data: { data: "new message" }, //todo: what is this "data"?
    _displayInForeground: true,
  };

  const response = await fetch("https://exp.host/--/api/v2/push/send", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Accept-encoding": "gzip, deflate",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(message),
  });    
};

export default PushNotifications;

世博诊断

Expo CLI 4.0.4 environment info:
System:
  OS: macOS 11.0.1
  Shell: 5.8 - /bin/zsh
Binaries:
  Node: 15.3.0 - /usr/local/bin/node
  Yarn: 1.22.10 - /usr/local/bin/yarn
  npm: 7.0.14 - /usr/local/bin/npm
SDKs:
  iOS SDK:
    Platforms: iOS 14.3, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
  Android SDK:
    API Levels: 29, 30
    Build Tools: 30.0.2
    System Images: android-30 | Google APIs Intel x86 Atom
IDEs:
  Android Studio: 4.1 AI-201.8743.12.41.6953283
  Xcode: 12.3/12C33 - /usr/bin/xcodebuild
npmPackages:
  expo: ^39.0.0 => 39.0.5 
  react: 16.13.1 => 16.13.1 
  react-dom: 16.13.1 => 16.13.1 
  react-native: https://github.com/expo/react-native/archive/sdk-39.0.3.tar.gz => 0.63.2 
  react-navigation: ^3.13.0 => 3.13.0 
npmGlobalPackages:
  expo-cli: 4.0.4
Expo Workflow: managed

纱线/package-lock.js 文件

"expo-notifications": "~0.1.1",
4

1 回答 1

2

首先,您应该运行expo install expo-notifications,因为 SDK 39 的正确版本是expo-notifications@~0.7.2

如果如您所说,消息正在通过,那么我不能 100% 确定问题是什么,但addNotificationReceivedListener如果我不得不猜测的话,它可能在您的通知侦听器 ( ) 中的某个位置。最好的办法是查看文档中的示例并从那里进行调试

编辑:实际上,如果这仅在 android 上,那么我认为另一个问题可能是您忘记useNextNotificationsApi在 app.json 文件中设置值:

{
  "expo": {
    ...
    "android": {
      ...
      "useNextNotificationsApi": true,
    }
  }
}
于 2020-12-17T21:54:57.983 回答