简体   繁体   中英

Scheduled local notifications are not coming in flutter

I Have an app that will show a notification at a certain time say, every day at 10 AM. I have used flutter_local_notifications: ^1.4.1 for notification purposes. From the GitHub page, I have created the notification and I got the notification in real-time, When I active the button for scheduled notification say from 10 seconds from now, I haven't got the response. Tried in an emulator in android.

 import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void main() => runApp(AwesomeTime());

class AwesomeTime extends StatefulWidget {
  @override
  _AwesomeTimeState createState() => _AwesomeTimeState();
}

class _AwesomeTimeState extends State<AwesomeTime> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  void initState() {
    super.initState();
    initNotification();
  }

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

  Future<void> onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
  }

  showNotification() async {
    var scheduledNotificationDateTime =
        DateTime.now().add(Duration(seconds: 5));
    var android = new AndroidNotificationDetails(
      'Channel ID',
      'Channel Name',
      'channelDescription',
      importance: Importance.Max,
      priority: Priority.High,
    );
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    // await flutterLocalNotificationsPlugin.show(
    //     0, 'current reminder', 'current reminder', platform,
    //     payload: 'Awesome');

    await flutterLocalNotificationsPlugin.schedule(0, 'Schedule Reminder',
        'Schedule reminder', scheduledNotificationDateTime, platform);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: RaisedButton(
            child: Text('push notifications'),
            onPressed: () {
              showNotification();
            },
          ),
        ),
      ),
    );
  }
}

In the doc, they mentioned some updates needed to be done for the AndroidManifest.xml I have updated in android/app/src/main/AndroidManifest.xml Below are the changes

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

<receiver android:name="com.example.first_app.ScheduledNotificationReceiver" />
        <receiver android:name="com.example.first_app.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <service   android:name="com.example.first_app.localnotifications.services.LocalNotificationsService"

Please read documentation in flutter_local_notifications carefully.In order to schedule notifications work, you have to add the lines below. Solution is this: add these lines after in AndroidManifest.xml

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
    </receiver>
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM