更改通知通道优先级信号和颤动

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

我需要在应用程序端创建一个通知通道。当应用程序处于活动状态时,此通知渠道应优先默认(不需要横幅)。 当应用程序处于后台时优先级应该很高(需要显示横幅通知)。

我使用

flutterLocalNotificationsPlugin

创建了一个通知通道

用于创建通知通道的代码

Future<void> createNotificationChannel({bool priority = false}) async {
  Importance importance = Importance.max;
  if (priority) {
    importance = Importance.max;
  } else {
    importance = Importance.defaultImportance;
  }

  log({"isResumed": priority, "importance": importance.name}.toString());
  AndroidNotificationChannel channel = AndroidNotificationChannel(
    'sound', // Replace with your channel ID
    'sound',
    importance: importance,
    playSound: true,
  );

  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}

删除通知通道的代码

Future<void> deleteNotificationChannel({bool isResumed = false}) async {
  log("delete channel");
  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.deleteNotificationChannel("sound");
  await createNotificationChannel();
}

然后我使用

didChangeAppLifecycleState
来检测应用程序状态并更新通道

  void didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed) {
      await deleteNotificationChannel(); // Adjust channel importance to low
    } else {
      await createNotificationChannel(priority: true); // Use default importance
    }
  }

但是,当我从 onesignal 仪表板发送类别为“在应用程序中创建”和现有频道“声音”通知横幅的通知时,不会仅在后台发出通知。

但是,如果我从 onesignal 仪表板发送高优先级的通知,它会作为横幅出现。

这里可能是什么问题以及如何解决这个问题。

android flutter firebase push-notification onesignal
1个回答
0
投票

我在我的一个项目中使用了 OneSignal,我非常确定您根本不需要 flutterLocalNotificationsPlugin。

检查这个例子

此外,this通知处理程序也很方便。另请检查代码示例,了解如何在应用程序关闭时处理推送通知。

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