Expo SDK 39 推送通知不工作
Expo SDK 39 Push Notifications Not Working
谁能看出我做错了什么?我升级到 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
纱线/包-lock.js个文件
"expo-notifications": "~0.1.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,
}
}
}
谁能看出我做错了什么?我升级到 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
纱线/包-lock.js个文件
"expo-notifications": "~0.1.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,
}
}
}