通知未与 Notifee 粘连

问题描述 投票:0回答:2

我目前正在开发 React Native 应用程序并使用 notifee 库来管理通知。我的要求很简单:我想显示一个“粘性”的通知,这意味着用户不应该能够将其滑开。

我遵循了库文档中的 android 配置下的几个属性。然而,该通知并未按预期运行;它不粘。

这是我到目前为止所做的: 在我的组件中:

  async function onDisplayNotification() {
    await notifee.requestPermission();

    // Create a channel (required for Android)
    let channelId;
    try {
      channelId = await notifee.createChannel({
        id: 'locations',
        name: 'Location Channel',
        importance: AndroidImportance.HIGH,
        visibility: AndroidVisibility.Public,
      });
    } catch (error) {
      console.log('Failed to create Channel');
    }

    const settings = await notifee.getNotificationSettings();

    // Display a notification
    try {
      await notifee.displayNotification({
        title: 'Notification Title',
        body: 'Main body content of the notification',
        android: {
          channelId,
          sticky: true,
          ongoing: true,
          smallIcon: 'ic_launcher', // optional, defaults to 'ic_launcher'.
          // pressAction is needed if you want the notification to open the app when pressed
          // pressAction: {
          //   id: 'default',
          // },
          autoCancel: false,
          asForegroundService: true,
          color: AndroidColor.RED,
          actions: [
            {
              title: 'Stop',
              pressAction: {
                id: 'stop',
              },
            },
          ],
        },
      });
    } catch (error) {
      console.log('Failed to display notification');
    }
  }

在index.js中 我注册了一个前台服务,如下:

notifee.registerForegroundService((notification) => {
  return new Promise(() => {
    notifee.onForegroundEvent(({ type, detail }) => {
      if (type === EventType.ACTION_PRESS && detail.pressAction.id === 'stop') {
        notifee.stopForegroundService();
      }
    });
  });
});
  • 通知正确显示。
  • 我已确认频道(位置)已成功创建。

尽管如此,通知并不具有粘性。当我滑动时,它就会消失,这不是预期的行为。

有人遇到过类似的问题或者可以提供一些可能出现问题的见解吗?任何指导将不胜感激!

android reactjs react-native notifications foreground-service
2个回答
1
投票

Android 在较新版本的 Android 中更改了此行为。 “如果您的应用向用户显示不可关闭的前台通知,则 Android 14 已更改行为以允许用户关闭此类通知。”

请参阅下面的帖子了解详细更新。 https://developer.android.com/about/versions/14/behavior-changes-all


0
投票

尝试删除前台服务并仅将正在进行的属性保留为 true

© www.soinside.com 2019 - 2024. All rights reserved.