4
votes

I am new to flutter and trying to implement push notification. I have used ONE SIGNAL for push notification

Here i have initialized one signal and getting the playerId, which i send to the server.

    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);
}

Here, i receive notification from one signal and get the required data from it.

 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);

});

Now, I am raising my custom notification, used flutter_local_notification dependency

      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');
  }

Here, notificatin will raise for android and ios both platform and onNotificationcClick callback is also declared here.

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

When clicked on notification

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

  }

This is working fine, when app is in background or is not killed. When it is killed, I am not able to open my Flutter app.

Any kind of help will be really appreciated..!!

1

1 Answers

0
votes

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

This function will run when a message received when App is killed. Inside Function Body write whatever you want to do.