【问题标题】:Expo Push Notification Foreground not working世博会推送通知前台不工作
【发布时间】:2021-11-14 08:20:51
【问题描述】:

后台通知:工作中

已终止的通知:工作中

令牌生成:工作

权限:已验证且有效

我应该怎么做才能正确解决这个问题?我尝试过其他处理方法,并且我相信我尝试向 app.json 添加通知属性,但据我所知没有任何效果。

感谢您的宝贵时间!

// imports redacted, but contain expo notification, device etc


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

export default function App() {
  const [expoPushToken, setExpoPushToken] = useState<string|undefined>('');
  const [notification, setNotification] = useState<any>(false);
  const notificationListener = useRef<any>();
  const responseListener = useRef<any>();


  useEffect(() => {
    if(Device.isDevice){
      registerForPushNotificationsAsync().then(token => setExpoPushToken(token));

      // This listener is fired whenever a notification is received while the app is foregrounded
      notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
        setNotification(notification);
      });

      // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
      responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
        console.log(response);
      });
      return () => {
        Notifications.removeNotificationSubscription(notificationListener.current);
        Notifications.removeNotificationSubscription(responseListener.current);
      };
    } else {
      //
    }
  }, []);
  return(view stuff)
}
// outside of functional component
  async function registerForPushNotificationsAsync() {
    let token;
    if (Constants.isDevice) {
      const { status: existingStatus } = await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
        alert('Failed to get push token for push notification!');
        return;
      }
      token = (await Notifications.getExpoPushTokenAsync({ experienceId: '@Expo-project-name' })).data; // commented project name for security
    } else {
      alert('Must use physical device for Push Notifications');
    }
  
    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }
    return token;
  }

【问题讨论】:

    标签: react-native notifications expo push foreground


    【解决方案1】:

    此解决方案的修复是在 experienceId 中

    确保您的体验 ID 与您的博览会项目名称完全匹配。

    我的@username/project-name'project-name'部分是小写的,但我的项目实际上是用大写字母命名的,所以@username/PROJECT-NAME

    这就解决了!

    【讨论】:

      猜你喜欢
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多