被杀死时从推送通知中打开Flutter应用

时间:2020-01-28 04:24:18

标签: flutter push-notification onesignal

我是新手,正在尝试实施推送通知。我已使用一个信号进行推送通知

在这里,我已经初始化了一个信号并获取了playerId,并将其发送到服务器。

    void oneSignalInit() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    OneSignal.shared.init('one signal id');
    OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);

    status = await OneSignal.shared.getPermissionSubscriptionState();

    var playerId = status.subscriptionStatus.userId;
    print(playerId);
    preferences.setString(Constants.PLAYER_ID, playerId);
}

在这里,我收到一个信号的通知,并从中获取所需的数据。

 notificationHandler() {
OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
  // will be called whenever a notification is received

  var data = notification.payload.additionalData;
  print(data['body']['tripID'].toString());
  showNotification(message);

});

现在,我正在使用flutter_local_notification依赖项提出我的自定义通知

      showNotification(var msg) async {
    print("show notification is working");
    AndroidNotificationDetails androidPlatformChannelSpecifics =
    new AndroidNotificationDetails(
        msg, msg, msg);
    IOSNotificationDetails iOSPlatformChannelSpecifics =
    new IOSNotificationDetails();
    NotificationDetails platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
        0, msg, msg, platformChannelSpecifics,
        payload: 'item id 2');
  }

在这里,notificatin将同时在android和ios平台上引发,并且还在此处声明onNotificationcClick回调。

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

当点击通知时

      Future onNotification(String payload) {
    print(payload);
    navigatorKey.currentState.pushNamed("/notification");

  }

当应用程序在后台运行或未被杀死时,这可以正常工作。被杀死后,我将无法打开我的Flutter应用。

任何帮助都将不胜感激.. !!

1 个答案:

答案 0 :(得分:0)

FirebaseMessaging.instance.getInitialMessage() .then((RemoteMessage message) {}});

当应用程序被杀死时收到消息时,此函数将运行。在函数体里面写你想做的任何事情。