【问题标题】:Expo SDK 39 Push Notifications Not WorkingExpo SDK 39 推送通知不起作用
【发布时间】:2020-12-17 21:28:18
【问题描述】:

谁能看到我在这里做错了什么?我升级到 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",

【问题讨论】:

    标签: react-native push-notification expo react-native-push-notification expo-notifications


    【解决方案1】:

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

    如果如您所说,消息正在通过,那么我不能 100% 确定问题出在哪里,但如果我不得不猜测的话,它可能在您的通知侦听器 (addNotificationReceivedListener) 中的某个地方。最好的办法是查看the example from the docs 并从那里调试

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

    {
      "expo": {
        ...
        "android": {
          ...
          "useNextNotificationsApi": true,
        }
      }
    }
    

    【讨论】:

    • 对不起,当我说“消息正在通过”时,我的意思是 console.log("matchExpoToken", matchExpoToken, messageBody); 正在显示正确的数据。根本没有任何通知被触发。我在一个物理设备上,它是 iOs。
    • 它显示的数据正确吗?或者不是?如果通知没有通过设备,您应该检查推送票和推送收据-docs.expo.io/push-notifications/sending-notifications/…
    猜你喜欢
    • 2013-06-08
    • 2023-03-19
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多