MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.45k stars 1.4k forks source link

App re-displays past scheduled notifications #356

Closed TJMusiitwa closed 4 years ago

TJMusiitwa commented 4 years ago

Hey there, I made use of the flutter_local_notifications package and it was quick and easy to set up all the required notifications for the app. However, I seem to be running into 2 issues.

  1. Each time the application is opened, after it has been killed, it re-displays all the past scheduled notifications at once, which obviously it shouldn't be doing.

  2. I have not been able to get the notification selected function to work properly where it has defaulted to open the home screen each time as opposed to the "agenda" screen that has been set.

I wonder if the issue to the 1st issue is because I set the notification initialisation in the initState method or something else.

Below is the code covering my notifications set-up `@override void initState() { super.initState(); //checkFirstSeen(); //Check for the first screen to show notificationSetup(); _scheduleNotification();

}

void notificationSetup() { var initializationSettingsAndroid = new AndroidInitializationSettings('transparent_pmi'); var initializationSettingsIOS = new IOSInitializationSettings(); var initializationSettings = new InitializationSettings( initializationSettingsAndroid, initializationSettingsIOS); flutterLocalNotificationsPlugin.initialize(initializationSettings); //flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails().toString(); }

Future onSelectNotification(String payload) async { if (payload != null) { debugPrint('Notification payload: ' + payload); } await Navigator.push( context, MaterialPageRoute(builder: (context) => AgendaPage())); analytics.logAppOpen();

//await Future.delayed(const Duration(milliseconds: 200), ()=>MyApp)

}

Future _scheduleNotification() async { var androidPlatformChannelSpecifics = AndroidNotificationDetails( 'pmi_conference', 'Agenda Notifications', 'Alert users of session', style: AndroidNotificationStyle.BigText);

var iosPlatformChannelSpecifics = IOSNotificationDetails();

NotificationDetails platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iosPlatformChannelSpecifics);

final agendaNotifications = [
  await flutterLocalNotificationsPlugin.schedule(
      0,
      'Registration & Breakfast',
      'Good morning, hurry over to Hotel Africana and get registered and enjoy the scrumptous breakfast awaiting you. See you there!',
      //conferenceDate,
      DateTime.now().add(Duration(seconds: 10)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      1,
      'Conference is beginning',
      'Just a friendly reminder that the conference is about to commence',
      DateTime.now().add(Duration(seconds: 15)),
      //conferenceDate.add(Duration(hours: 1, minutes: 45)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      2,
      'Session 1',
      'Linking Project Management to Infrastructural Development in Uganda is about to commence',
      DateTime.now().add(Duration(seconds: 20)),
      //conferenceDate.add(Duration(hours: 2, minutes: 25)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      3,
      'Health Break',
      'After that hearty session, certainly a little break is in order',
      conferenceDate.add(Duration(hours: 3, minutes: 25)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      4,
      'Keynote Address',
      'Global perspectives in Project Management: A case of hard and soft infrastructure is about to commence',
      conferenceDate.add(Duration(hours: 3, minutes: 55)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      5,
      'Session 2',
      'Case studies on economic and social benefits of Project Management is about to commence',
      conferenceDate.add(Duration(hours: 4, minutes: 55)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      6,
      'Lunch',
      'After that hearty session, certainly a filling lunch is in order',
      conferenceDate.add(Duration(hours: 6)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      7,
      'Session 3A',
      'Nexus between Infrastructure and the Environment is about to commence',
      conferenceDate.add(Duration(hours: 7, minutes: 25)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      8,
      'Session 3B',
      'Implications for Infrastructural growth in Uganda: Addressing social and cultural challenges',
      conferenceDate.add(Duration(hours: 7, minutes: 25, seconds: 30)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      9,
      'Session 3B',
      'Implications for Infrastructural growth in Uganda: Addressing social and cultural challenges',
      conferenceDate.add(Duration(hours: 7, minutes: 25, seconds: 30)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
  await flutterLocalNotificationsPlugin.schedule(
      10,
      'Wrap Up Session',
      '''Let's wrap up the conference''',
      conferenceDate.add(Duration(hours: 8, minutes: 40)),
      platformChannelSpecifics,
      payload: 'What is expected to happen',
      androidAllowWhileIdle: true),
];

return agendaNotifications;

} ` Running Flutter version 1.9.1+hotfix.4 on VS Code. Live testing the app on an Android 9 smartphone

quangduy-luong commented 4 years ago
  1. When you say "past scheduled notifications," do you perhaps mean the first 3? I wasn't able to replicate your issue with the code you provided, so if that's not what you meant, then it probably is something somewhere else. If you do mean the first 3 notifications, your code currently is re-scheduling notifications on launch, so those 3 will always appear shortly after your app is launched, since you schedule them again in your _scheduleNotification method.
  2. You're not passing the onSelectNotification callback to your plugin, so it's not doing anything. In your notificationSetup method, you should have something like flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);.
TJMusiitwa commented 4 years ago

Hey yes sorry should have updated the code slightly before posting. However, even with the made changes and testing that I did yesterday, all the notifications show up again past their scheduled time each time the app is opened.

  1. Passed the appropriate callback to the notification setup and it would always land me on the home screen and not the agenda page. However I am glad to try again and get back to you
MaikuB commented 4 years ago

I'm on vacation so will keep this brief. The problems you're describing appear to be more of an issue in your code than an issue within the plugin itself (i.e. a bug). As such I'd suggest you post on sites like stack overflow as the issues board here is more for bugs

TJMusiitwa commented 4 years ago

Okay I understand but if I could please inquire what's the best way to initialise my list of scheduled notifications without having to do a button press as shown in the example file of the package repo? Because admittedly calling it in my unit state is having slight issues for me?

MaikuB commented 4 years ago

That is something you need to determine as the best solution is one that makes sense in your context. In some cases it'll involve pressing a button and sounds like it doesn't work in your scenario for whatever reason.

Perhaps rather than asking what is the best solution you should try to debug and find out what the real cause of your issue is. At a glance, there are some odd things I see about your code such as not awaiting async method calls, rescheduling the same notifications in initState without checking if that should happen.

Regardless I'll be closing this as the these kinds of problems should be posted on places like stack overflow