简体   繁体   English

Flutter 通知未弹出

[英]Flutter Notification not showing pop up

Notifications I send with Firebase when the app is open, in the background, and closed work fine.当应用程序打开、在后台和关闭时,我使用 Firebase 发送的通知工作正常。

在此处输入图像描述

But I want to pop up, like this:但我想弹出,像这样: 在此处输入图像描述

My codes;我的代码;

local_notification_service.dart: 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:主要.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:选择屏幕.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: 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这是我观看的视频(他对我做了同样的事情,但通知屏幕在他的应用程序上打开,但不是为我打开的。显示通知的场景

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您需要打开设置和 select 通知,go 到您的应用程序和 select 打开屏幕底部的频道名称,然后勾选允许浮动通知

在此处输入图像描述

在此处输入图像描述

If you ran the app before setting the Importance to Max, it may have been cached without that.如果您在将 Importance 设置为 Max 之前运行该应用程序,则它可能在没有设置的情况下被缓存。 I had the same issue and had to wipe the data of the Android emulator and run it again, and then it worked.我有同样的问题,不得不擦除 Android 模拟器的数据并再次运行它,然后它工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:主屏幕上 iOS 应用程序图标上的通知徽章计数器未显示(使用 FCM) - Flutter: The notification badge counter on the iOS app icon on the home screen is not showing up (using FCM) Flutter:通知图标显示白色圆圈 - Flutter: Notification icon showing white circle Android:FCM 通知显示不正确 - Android: FCM notification showing up incorrectly Flutter:Google 登录弹出窗口不会出现在 select 帐户中 - Flutter : Google Login pop up wont appear to select an account flutter 应用程序中将始终出现通知权限弹出窗口 - Notifications permission pop-up will always appear in flutter app Flutter Firebase 前台推送通知未显示,但后台正在工作 - Flutter Firebase foreground push notification not showing but background is working Flutter Firebase 消息 - 应用打开时不显示推送通知 - Flutter Firebase messaging - push notification is not showing when app is open Flutter 问题:在锁屏和后台屏幕通知图标显示灰色 - Flutter problem: On lock screen and background screen notification Icon showing grey Flutter 取消后谷歌登录错误 弹出选择账户 - Flutter Google Sign In error after canceling Choose an account pop-up FirebaseMessaging:应用程序在屏幕锁定或其他应用程序打开时不显示通知 iOS Flutter - FirebaseMessaging: App not showing notification when screen is locked or some other app is open iOS Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM