【问题标题】:Alert and sound is not working in expo push notification警报和声音在博览会推送通知中不起作用
【发布时间】:2020-11-10 13:41:23
【问题描述】:

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

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

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

谢谢!!!

【问题讨论】:

    标签: android firebase react-native push-notification expo


    【解决方案1】:

    为了让听众工作,请尝试:

    将此添加到您的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/

    【讨论】:

    • 非常感谢@Juan Vieira 这对我也很有效
    【解决方案2】:

    对我来说,我面临同样的问题,上述解决方案中没有一个对我有用。 只有当我像这样将声音设置为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",
          }
    

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2023-04-03
      • 2021-03-29
      • 2012-05-08
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      相关资源
      最近更新 更多