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

Has*_*med 3 android push-notification firebase react-native 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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的 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])
Run Code Online (Sandbox Code Playgroud)

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

谢谢你!!!

Jua*_*ira 6

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

将其添加到您的app.json

{
  "expo": {
    ...
    "android": {
      ...
      "useNextNotificationsApi": true,
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

一开始它对我不起作用,但指定权限后,它起作用了:

{
  "expo": {
    ...
    "android": {
      ...
      "useNextNotificationsApi": true,
      "permissions": ["RECEIVE_BOOT_COMPLETED"]
    }
  }
}
      
Run Code Online (Sandbox Code Playgroud)

而且,如果它不起作用(它不适用于我的主项目,但适用于另一个项目),您可以尝试使用旧的通知系统,该系统使用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
});
Run Code Online (Sandbox Code Playgroud)

另请检查这是否可以帮助您在应用程序在后台关闭时获取通知(就在通知导入之后)

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});
Run Code Online (Sandbox Code Playgroud)

要获取带有声音的通知: 如果您希望在应用程序处于后台时收到通知,则需要定义setNotificationHandlerwith shouldPlaySound: true。您还需要在通知中指定您想要播放声音,如下所示:

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' },
  };
Run Code Online (Sandbox Code Playgroud)

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