flutter 扑动通知未显示弹出窗口

ftf50wuq  于 2023-01-09  发布在  Flutter
关注(0)|答案(2)|浏览(309)

当应用程序打开、在后台和关闭时,我用Firebase发送的通知工作正常。

但我想弹出来,像这样:

我的密码;

本地通知服务.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);
    }
  }
}

主省道:

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());
}

选择_屏幕。箭头:

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

机器人清单:

<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"/>

这是我观看的视频(他对我做了同样的操作,但通知屏幕在他的应用程序上打开,而不是为我打开。The scene where the notification is shown

gjmwrych

gjmwrych1#

如果你在将重要性设置为Max之前运行了应用,它可能已经被缓存了,没有那个。我也遇到了同样的问题,不得不擦除Android模拟器的数据并再次运行,然后它就工作了。

6tr1vspr

6tr1vspr2#

你需要打开设置并选择通知,进入你的应用程序并选择屏幕底部的开放频道名称,然后勾选允许浮动通知

相关问题