【问题标题】:Open Flutter app from push notification , when it is killed从推送通知打开 Flutter 应用程序,当它被杀死时
【发布时间】:2020-05-13 11:25:03
【问题描述】:

我是新来的颤振并试图实现推送通知。我已使用 ONE SIGNAL 进行推送通知

这里我已经初始化了一个信号并获取了 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 应用程序。

任何形式的帮助将不胜感激..!!

【问题讨论】:

标签: flutter push-notification onesignal


【解决方案1】:

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

当 App 被杀死时收到消息时,该函数将运行。在函数体内写你想做的任何事情。

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 2022-01-22
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2015-12-30
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多