2

I'm trying to send a notification using the package flutter locale notifications

I can see the notification if i show it right away with the .show() but when i try to schedule on with the .schedule() It doesn't appear. I can see it's scheduled with the await flutterLocalNotificationsPlugin.pendingNotificationRequests();

I tried the same code in a new flutter project and it does work over there so i must be doing something different in my actual project, i just don't know what.

My manifest looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bap_todo_flutter">

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="bap_todo_flutter"
        android:icon="@mipmap/ic_launcher">
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

My state looks like this:

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      new FlutterLocalNotificationsPlugin();

  @override
  void initState() {
    super.initState();
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
   }

And i'm using a simple RaisedButton to schedule the notification:

RaisedButton(
  child: Text("notification"),
  onPressed: () async {
        var scheduledNotificationDateTime =
    new DateTime.now().add(new Duration(seconds: 2));
        var androidPlatformChannelSpecifics =
    new AndroidNotificationDetails('myalarmapplication.alarm',
       'Alarm Channel', 'Flutter Todo Alarm');
    var iOSPlatformChannelSpecifics =
       new IOSNotificationDetails();
    NotificationDetails platformChannelSpecifics =
       new NotificationDetails(androidPlatformChannelSpecifics,
          iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin
      .schedule(
      1,
      'Alarm',
      'Alarm going off',
      scheduledNotificationDateTime,
      platformChannelSpecifics);
   },
),
Jonas
  • 1,153
  • 2
  • 7
  • 7

3 Answers3

0

I had the same problem but couldn't find the solution online, however, later I did figure out running flutter clean then flutter run did the trick for me.

You have to essentially clean your build and rebuild it again. Hope that works.

0

If you are using iOS you should add this line to AppDelegate.m/AppDelegate.swift in ios/Runner

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
}

Got this solution from the documentation

kyu
  • 111
  • 1
  • 10
0

Add this extra line in
flutterLocalNotificationsPlugin .schedule(
...
...

androidAllowWhileIdle: true);

Aashak Rs
  • 1
  • 2