颤音通知不显示弹出

发布于 2025-01-22 13:28:13 字数 4070 浏览 0 评论 0原文

当应用程序打开时,我会在Firebase中发送通知,并在后台打开,并且封闭的工作正常。

但我想弹出,这样:

我的代码;

local_notification_service.dart:

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static void initialize(BuildContext context) {
    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: AndroidInitializationSettings("@mipmap/launcher_icon"),
            iOS: IOSInitializationSettings(
              requestSoundPermission: false,
              requestBadgePermission: false,
              requestAlertPermission: false,
            ));

    _notificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String? route) async {
      if (route != null) {
        Navigator.of(context).pushNamed(route);
      }
    });
  }

  static void display(RemoteMessage message) async {
    try {
      final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;

      final NotificationDetails notificationDetails = NotificationDetails(
          android: AndroidNotificationDetails(
        "kasifkocaeli",
        "kasifkocaeli channel",
        channelDescription: "kasifkocaeli guzel",
        importance: Importance.max,
        priority: Priority.high,
      ));

      await _notificationsPlugin.show(
        id,
        message.notification!.title,
        message.notification!.body,
        notificationDetails,
        payload: message.data["route"],
      );
    } on Exception catch (e) {
      print(e);
    }
  }
}

main.dart:

Future<void> backgroundHandler(RemoteMessage message) async{
  print(message.data.toString());
  print(message.notification!.title);
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  runApp(const MyApp());
}

select_screen.dart:

class _ChoosePageState extends State<ChoosePage> {
  @override
  void initState() {
    super.initState();

    LocalNotificationService.initialize(context);

    //Bildirime tıklandığında gösterilecek mesaj
    FirebaseMessaging.instance.getInitialMessage().then((message) {
      if (message != null) {
        final routeFromMessage = message.data["route"];

        Navigator.of(context).pushNamed(routeFromMessage);
      }
    });

    //forground (uygulama önplandayken)
    FirebaseMessaging.onMessage.listen((message) {
      if (message.notification != null) {
        print(message.notification!.body);
        print(message.notification!.title);
      }

      LocalNotificationService.display(message);
    });

    //Uygulama arkaplandayken ve kullanıcı tıkladığında
    FirebaseMessaging.onMessageOpenedApp.listen((message) {
      final routeFromMessage = message.data["route"];

      Navigator.of(context).pushNamed(routeFromMessage);
    });
  }

pubspec.yaml:

firebase_messaging: ^11.2.13
flutter_local_notifications: ^9.4.0
firebase_core: ^1.13.1
cloud_firestore: ^3.1.10

androidmanifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="notification_app"/>

,这是我的观看了视频(他与我做同样的事情,但通知屏幕在他的应用程序上打开,但对我来说不。显示通知的地方

Notifications I send with Firebase when the app is open, in the background, and closed work fine.

enter image description here

But I want to pop up, like this:
enter image description here

My codes;

local_notification_service.dart:

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static void initialize(BuildContext context) {
    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: AndroidInitializationSettings("@mipmap/launcher_icon"),
            iOS: IOSInitializationSettings(
              requestSoundPermission: false,
              requestBadgePermission: false,
              requestAlertPermission: false,
            ));

    _notificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String? route) async {
      if (route != null) {
        Navigator.of(context).pushNamed(route);
      }
    });
  }

  static void display(RemoteMessage message) async {
    try {
      final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;

      final NotificationDetails notificationDetails = NotificationDetails(
          android: AndroidNotificationDetails(
        "kasifkocaeli",
        "kasifkocaeli channel",
        channelDescription: "kasifkocaeli guzel",
        importance: Importance.max,
        priority: Priority.high,
      ));

      await _notificationsPlugin.show(
        id,
        message.notification!.title,
        message.notification!.body,
        notificationDetails,
        payload: message.data["route"],
      );
    } on Exception catch (e) {
      print(e);
    }
  }
}

main.dart:

Future<void> backgroundHandler(RemoteMessage message) async{
  print(message.data.toString());
  print(message.notification!.title);
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  runApp(const MyApp());
}

choose_screen.dart:

class _ChoosePageState extends State<ChoosePage> {
  @override
  void initState() {
    super.initState();

    LocalNotificationService.initialize(context);

    //Bildirime tıklandığında gösterilecek mesaj
    FirebaseMessaging.instance.getInitialMessage().then((message) {
      if (message != null) {
        final routeFromMessage = message.data["route"];

        Navigator.of(context).pushNamed(routeFromMessage);
      }
    });

    //forground (uygulama önplandayken)
    FirebaseMessaging.onMessage.listen((message) {
      if (message.notification != null) {
        print(message.notification!.body);
        print(message.notification!.title);
      }

      LocalNotificationService.display(message);
    });

    //Uygulama arkaplandayken ve kullanıcı tıkladığında
    FirebaseMessaging.onMessageOpenedApp.listen((message) {
      final routeFromMessage = message.data["route"];

      Navigator.of(context).pushNamed(routeFromMessage);
    });
  }

pubspec.yaml:

firebase_messaging: ^11.2.13
flutter_local_notifications: ^9.4.0
firebase_core: ^1.13.1
cloud_firestore: ^3.1.10

AndroidManifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="notification_app"/>

And this is my watched video (he did the same with me but the notification screen opens on his app, but not for me. The scene where the notification is shown

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲凉≈ 2025-01-29 13:28:13

如果您在设置对最大的重要性之前运行了该应用程序,则它可能已经被缓存了。我遇到了同样的问题,不得不擦除Android仿真器的数据并再次运行,然后起作用。

If you ran the app before setting the Importance to Max, it may have been cached without that. I had the same issue and had to wipe the data of the Android emulator and run it again, and then it worked.

泅人 2025-01-29 13:28:13

您需要打开设置并选择通知,转到您的应用程序,然后在屏幕底部选择“打开频道名称”,然后tick允许浮动通知

“在此处输入图像描述”

“在此处输入图像说明”

You need to open settings and select notifications, go to your app and select open channel name at the bottom of the screen then tick allow floating notifications

enter image description here

enter image description here

只有一腔孤勇 2025-01-29 13:28:13

对我来说,我的代码编写正确,我要做的就是运行:

  1. flutter Clean
  2. flutter Pub get
  3. flutter Run

使其正常工作。

基本上,@Ricky Kresslein说的话。

For me, my code was written correctly, all I had to do was run:

  1. flutter clean
  2. flutter pub get
  3. flutter run

to make it work.

Basically what @Ricky Kresslein said.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文