世博会推送通知中的警报和声音不起作用



我正在使用expo开发一个应用程序。我想使用expo通知在我的应用程序中发送和接收推送通知。我已经集成了博览会通知,我成功地收到了通知,但没有声音和弹出警报。我总是要向下滚动通知面板,然后只能看到通知。这是我注册通知的代码

async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}

这是我的UseEffect,我在这里注册并收听通知

useEffect(() => {
_gettingRestaurants();
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
Notifications.addNotificationReceivedListener((notification)=>{
alert(notification);
});
Notifications.addNotificationResponseReceivedListener((response)=>{
alert(response);
});

}, [isDataFetched])

还有一件事,这些听众也没有工作。我没有看到这两个警报中的任何警报。请帮帮我。

谢谢

要让侦听器工作,请尝试:

将其添加到您的app.json

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

起初它对我不起作用,但在指定权限后,它起了作用:

{
"expo": {
...
"android": {
...
"useNextNotificationsApi": true,
"permissions": ["RECEIVE_BOOT_COMPLETED"]
}
}
}

而且,如果它不起作用(它不适用于我的Main项目,但适用于另一个项目(,您可以尝试使用使用addListener:的遗留通知系统

import * as Notifications from 'expo-notifications';
import { Notifications as Notifications2 } from 'expo';
Notifications.addNotificationReceivedListener((notification) => {
// New Notifications. Didn't work sometimes
});
Notifications2.addListener((data) => {
// Legacy notifications. You get a deprecation warning, but works when the new Notifications don't
});

还要检查这是否有助于你在后台关闭应用程序时(在通知导入后(获得通知

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});

若要获得带声音的通知:如果您在应用程序处于后台时期望它们,则需要使用shouldPlaySound: true定义setNotificationHandler。你还需要在通知中指定你想播放的声音如下:

const message = {
to: expoPushToken,
sound: 'default', // <== the values are 'default' (sound) or null (silent)
title: 'Original Title',
body: 'And here is the body!',
data: { data: 'goes here' },
};

我记得还有一个选项可以设置通知的优先级。发挥这些价值观,直到你得到你需要的。搜索";"优先级";在此页面中:https://docs.expo.io/versions/latest/sdk/notifications/

对我来说,我面临着同样的问题,上面的解决方案都不适用。只有当我像这个一样将声音设置为default时,问题才会出现

content: {
title: "Some title",
body: "Some body",
sound: "default",
}

它在ios上运行得很好,但在android上,弹出通知和声音根本不显示,所以我最终只为ios设置了default,为android设置了null,我让它运行得很正常,就像下面的例子一样:

content: {
title: "Some title",
body: "Some body",
sound: Platform.OS === "android" ? null : "default",
}

注意:如果null不适合您,请尝试undefined而不是

最新更新