【问题标题】:Clicking on Notification button not opening app in flutter单击通知按钮不会在颤动中打开应用程序
【发布时间】:2020-02-01 15:44:12
【问题描述】:

我已经集成了用于通知的 firebase-messaging 插件,但是在单击通知时,如果它在后台或被杀死,则应用程序不会打开。我想在单击 natification 时打开应用程序。

下面是我的代码

    void firebaseCloudMessagingListeners() {
      FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
      _firebaseMessaging.getToken().then((token){
        print(token);
      });

     _firebaseMessaging.requestNotificationPermissions(
            const IosNotificationSettings(sound: true, badge: true, alert: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });

      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['notification']['body']);

          print('on message $message');
          return;
        },
        onResume: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['data']['body']);
          print('on message $message');
          return;
        },

        onLaunch: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['data']['body']);
          print('on message $message');
        },
      );
    }

    _showNotificationWithDefaultSound(message) async {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          new FlutterLocalNotificationsPlugin();

      var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
      var ios = new IOSInitializationSettings();
      var initSettings = new InitializationSettings(android, ios);
      flutterLocalNotificationsPlugin.initialize(initSettings,
          onSelectNotification: onSelectNotification);

      var android1 = new AndroidNotificationDetails(
          'channel id', 'channel NAME', 'channel DESCRIPTION',
          importance: Importance.Max, priority: Priority.High, ticker: 'ticker');

      var ios1 = new IOSNotificationDetails();
      var platform = new NotificationDetails(android1, ios1);
      await flutterLocalNotificationsPlugin
          .show(0, message, 'App Notification!', platform, payload: message);
    }

【问题讨论】:

    标签: android flutter dart firebase-cloud-messaging


    【解决方案1】:

    你需要添加AndroidManifest.xml

      <intent-filter>
          <action android:name="FLUTTER_NOTIFICATION_CLICK" />
          <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    

    然后,您需要在 firebase 通知的数据正文中发送 click_action:FLUTTER_NOTIFICATION_CLICK,如下所示:

      DATA='{
      "notification": {
        "body": "this is a body",
        "title": "this is a title"        
      },
      "data": {
        "att1": "value..", 
        "att2": "value..",        
        "click_action": "FLUTTER_NOTIFICATION_CLICK",
      },
      "to": "<FCM TOKEN>"
    }'
    

    当你的应用被杀死时,如果你点击通知,就会调用onLaunch方法,你必须通过message['data']获取参数。

    在此处查看更多信息:https://stackoverflow.com/a/48405551/7105694

    【讨论】:

      【解决方案2】:

      可能你需要实现onSelectNotification()方法

      Future<void> onSelectNotification(String payload) async {
              Navigator.push(
                  context, PageTransition(
                  type: PageTransitionType.leftToRight,
                  child: /* your home screen name */));
        }
      

      【讨论】:

      • 但是如果应用程序被杀死会怎样
      猜你喜欢
      • 1970-01-01
      • 2017-12-03
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多