【问题标题】:Expo Push Notifications - Disable notifications in a specific screenExpo Push Notifications - 在特定屏幕中禁用通知
【发布时间】:2021-06-10 20:14:11
【问题描述】:

当用户在特定屏幕中时,如何禁用在我的 Expo 应用中显示通知?

我目前正在设计一个聊天屏幕,我想在用户在其中时禁用推送通知。

目前,我在 PushNotifications 模块中实现了以下方法:

export function setNotificationHandler(handler) {
  ExpoNotifications.setNotificationHandler(handler);
}

export function hidePushNotifications() {
  setNotificationHandler({
    handleNotification: async () => ({
      shouldShowAlert: false,
      shouldPlaySound: false,
      shouldSetBadge: false,
    }),
  });
}

export function unhidePushNotifications() {
  setNotificationHandler({
    handleNotification: async () => ({
      shouldShowAlert: true,
      shouldPlaySound: true,
      shouldSetBadge: true,
    }),
  });
}

在我的聊天屏幕中,在 useEffect 中,我正在做:

useEffect(() => {
  // Do not show push notifications when the user is inside this screen
  hidePushNotifications();

  return () => {
    // When unmounted, unhide push notifications
    unhidePushNotifications();
  };
}, []);

对我来说,这很有意义,因为我在用户进入聊天屏幕时“隐藏”推送通知,并在他离开时“取消隐藏”它们

但由于某些原因,通知仍显示在聊天屏幕内。为什么?

我该如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs react-native push-notification expo


    【解决方案1】:

    如果不了解应用程序结构的更多信息,则无法 100% 确定,但您应该使用一些简单的 console.log 语句或类似的语句来验证您的屏幕是否正在卸载。

    如果您的屏幕没有按预期卸载,并且您使用 React Navigation 之类的工具来处理您的屏幕,您应该查看 Navigation Lifecycle 以解决该问题。

    这显然是对您的问题做出一些假设,但希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2022-08-22
      • 2020-07-09
      • 1970-01-01
      • 2021-03-06
      • 2021-06-04
      相关资源
      最近更新 更多