Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter local notification schedule - not working as expected

Tags:

flutter

dart

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(

    );
  }
}

like image 615
Abhijit leihaorambam Avatar asked May 29 '19 10:05

Abhijit leihaorambam


2 Answers

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.

like image 187
Basel Abuhadrous Avatar answered Nov 07 '22 15:11

Basel Abuhadrous


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(

        );
      }
    }
like image 25
Raj Dhakad Avatar answered Nov 07 '22 14:11

Raj Dhakad