Alert和声音在Expo推送通知中无法工作。

3

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

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])

还有一件事,这些监听器也不起作用。我没有看到来自这两个警报的任何警报。请帮帮我。

谢谢!!!


你好 @haseeb Ahmed,你能找到解决办法吗?对我来说,这些答案都没有起作用。 - undefined
2个回答

6

要使听众有效,请尝试:

将以下内容添加到您的 app.json 文件中

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

一开始对我来说没有起作用,但在指定权限后,它起作用了:

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

如果它无法工作(对于我的主项目而言确实如此,但对于另一个项目则有效),您可以尝试使用使用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' },
  };

我记得还有一个设置通知优先级的选项。尝试调整这些值,直到你得到所需结果。 在此页面中搜索“priority”:https://docs.expo.io/versions/latest/sdk/notifications/


非常感谢 @Juan Vieira,这对我也起作用了。 - undefined
对我来说,问题出在通知处理程序中的shouldPlaySound被设置为false...谢谢。 - undefined

2

对我来说,我遇到了相同的问题,上面的解决方案都没有起作用。 只有在我将声音设置为默认时才会出现这个问题。

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

在iOS上它工作得很完美,但在Android上弹出的通知和声音根本不显示,所以我最终将default仅设置为iOS,而对于Android则设置为null,就像下面的示例一样,这样我就顺利解决了问题:

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

注意:如果null不能起作用,请尝试使用undefined


对我来说不起作用。 - undefined
1
感谢@Anas。我的expo-notifications版本不允许使用null,但允许使用undefined。使用sound: Platform.OS ===“android”?undefined:“default”可以正常工作,并且我可以看到弹出通知。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接