Abhijit leihaorambam
Abhijit leihaorambam

Reputation: 383

Flutter local notification schedule - not working as expected

I want to show notification daily on a specified time and I have implemented the code to show daily notification but it is not working not showing a notification at the time I want to call. I am using this plugin link

When I run The App I got this warning on Console here is the screenshot a busy cat

Is my implementation of the code wrong? Here is the code

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

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}


class _AppState extends State<App> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

  showNotification() async{
    var time = new Time(3,51, 0);//at 3.30 
    var androidPlatformChannelSpecifics =
    new AndroidNotificationDetails('repeatDailyAtTime channel id',
        'repeatDailyAtTime channel name', 'repeatDailyAtTime description');
    var iOSPlatformChannelSpecifics =
    new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.showDailyAtTime(
        0,
        'Teer Result Time',
        'Open The App and check for the Result',
        time,
        platformChannelSpecifics);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    showNotification();
  }
  @override
  Widget build(BuildContext context) {
    return Container(

    );
  }
}

Upvotes: 14

Views: 12743

Answers (3)

Basel Abuhadrous
Basel Abuhadrous

Reputation: 1660

I just had the same problem, I found that I didn't add some required permissions and receivers in my manifest, all the details are written in the docs under the Scheduled notifications title, as well as this link to the example project manifest as a reference.

Hope it helps someone.

Upvotes: 5

Raj Dhakad
Raj Dhakad

Reputation: 932

You have to first initialize Local Notification plugin.

Just add the initialization code to your initstate, as shown below:

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

    class App extends StatefulWidget {
      @override
      _AppState createState() => _AppState();
    }


    class _AppState extends State<App> {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

      showNotification() async{
        var time = new Time(3,51, 0);//at 3.30 
        var androidPlatformChannelSpecifics =
        new AndroidNotificationDetails('repeatDailyAtTime channel id',
            'repeatDailyAtTime channel name', 'repeatDailyAtTime description');
        var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails();
        var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.showDailyAtTime(
            0,
            'Teer Result Time',
            'Open The App and check for the Result',
            time,
            platformChannelSpecifics);
      }

      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        //-------------------- Initialization Code---------------------
        FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
   //--------------------------------------
        showNotification();
      }
      @override
      Widget build(BuildContext context) {
        return Container(

        );
      }
    }

Upvotes: 2

MaikuB
MaikuB

Reputation: 56

I've heard of one report where this issue happened as their app hadn't been migrated to AndroidX, although the error wouldn't imply that. Have you looked at that yet? See https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility#how-to-migrate-a-flutter-app-to-androidx

Upvotes: 0

Related Questions