单击Firebase Flutter的推送通知时如何打开特定活动

时间:2019-01-05 11:51:07

标签: flutter firebase-cloud-messaging

我在flutter应用程序中使用firebase消息服务收到推送通知。单击通知时,需要将应用程序重定向到特定活动。大家能提出一些建议吗?提前致谢。

这是我尝试的代码。

var initializationSettingsAndroid = new AndroidInitializationSettings(
    '@mipmap/ic_launcher'); // Notification Initialization for android
var initializationSettingsIOS = new IOSInitializationSettings(); // Notification Initialization for IOS
var initializationSettings = new InitializationSettings(
    initializationSettingsAndroid,
    initializationSettingsIOS); //Initialization done according to their platform
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings);


firebaseMessaging
    .configure( //  handling notification while app is in foreground
  onLaunch: (Map<String, dynamic> message) async {
    print('onlaunch');
    //navigateTo(message);
    Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context)=> new Credits()));

  },
  onResume: (Map<String, dynamic> message) async {
    print('onmesage $message');
    print('onResume');
  //  navigateTo(message);
    Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context)=> new Credits()));

  },
  onMessage: (Map<String, dynamic> message) async {

    print('onmesage $message');
      body = message['notification']['body'];
      title = message['notification']['title'];
    Timer(Duration(seconds: 1), (){});

    showNotificationWithDefaultSound(message);
      print("title: $title");
      print("body: $body");



    //print("${message['data']['screen']}");
    //Navigator.of(context).pushNamed(message['data']['screen']);

  },

);

//Permission for IOS
firebaseMessaging.requestNotificationPermissions(

    const IosNotificationSettings(
        alert: true,
        sound: true,
        badge: true
    )
);
firebaseMessaging.onIosSettingsRegistered.listen((
    IosNotificationSettings settings) {
  print("IOS");
});

//geting token from the app
firebaseMessaging.getToken().then((token) {
  tokenId = token;
  print(tokenId);
});

//

1 个答案:

答案 0 :(得分:-1)

Firebase消息传递对象在onMessageonResumeonLaunch中提供了一种configure方法,您可以在其中运行您的遗嘱代码。根据您的要求,我相信您必须调用新页面/活动,以便当用户单击通知(应用终止时,即不在前台和后台)时,它将启动该页面而不是主页。在根页面的initState()中实现此代码

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async{
         Print('on message $message'); 
        },
        onResume: (Map<String, dynamic> message) async {
          print('on resume $message');
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('on launch $message');
          //Call your pageRoute here
        },
      );