不显示本地颤振通知?

时间:2020-10-06 22:59:45

标签: firebase flutter push-notification firebase-cloud-messaging

这是我在main.dart中的Firebase连接设置 它以前工作正常,但现在突然停止了。 当我从Firebase控制台发送测试消息时,它将调用displayNotification(),该消息应显示本地通知。即使关闭应用程序,我也没有看到通知。

  Connectivity connectivity;
  StreamSubscription<ConnectivityResult> subscription;
  FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      new FlutterLocalNotificationsPlugin();

  @override
  void initState() {
    super.initState();
    connectivity = new Connectivity();
    subscription =
        connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
      if (result == ConnectivityResult.wifi ||
          result == ConnectivityResult.mobile) {
        setState(() {});
      }
    });
    var android = new AndroidInitializationSettings('mipmap/ic_launcher');
    var ios = new IOSInitializationSettings();
    var platform = new InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin.initialize(platform);

    firebaseMessaging.configure(
      onLaunch: (Map<String, dynamic> message) {
        print(" onLaunch called ${(message)}");
      },
      onResume: (Map<String, dynamic> message) {
        print(" onResume called ${(message)}");
      },
      onMessage: (Map<String, dynamic> message) {
        displayNotification(message);
        print(" onMessage called ${(message)}"); //-------------------------------------this is printed
      },
    );
    firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, alert: true, badge: true));
    firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings setting) {
      print('IOS Setting Registered');
    });
    firebaseMessaging.getToken().then((token) {
      update(token);
      print("FirebaseMessaging token: $token");
    });
  }

  Future displayNotification(Map<String, dynamic> message) async {
    print("Show Notification"); //-------------------------------------------------this is also printed
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'channel-id', 'fcm', 'androidcoding.in',
        importance: Importance.Max, priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
      0,
      message['notification']['title'],
      message['notification']['body'],
      platformChannelSpecifics,
      payload: 'fcm',
    );
  }

  update(String token) {
    deviceToken = token;
    DatabaseReference databaseReference = new FirebaseDatabase().reference();
    databaseReference.child('fcm-token/${token}').set({"token": token});
    setState(() {});
  }

我应该如何解决问题?

0 个答案:

没有答案