Search code examples
flutterflutter-notification

how to push notification in flutter and web api


I want the program to be able to send a request to the server every minute and return a list: My List: [{"NoteId": "Id", "Title": "Note Title", "Description": "Note Description", "AtDateTime": "1/7/2021 10:10:15"}]

Display an alarm with list record values if the value of the AtDateTime field is equal to the current time

I use a node js to connect the data. Please Help Me


Solution

  • I found a solution that performs a task in the background periodically.
    But this only happens once every 15 minutes

    for schedule task need to add package :workmanager

    for local notification add package:flutter_local_notifications

     dependencies:
      flutter:
        sdk: flutter
    ....
      # Use with the Workmanger for background jobs headless execution.
      workmanager: ^0.2.3
      # Use with FlutterLocalNotificationsPlugin for local push notifications.
      flutter_local_notifications: ^1.4.4+2
    

    add this code in :-> android -> app -> src -> main -> AndroidManifest.xml
    <!-- Add below permission inside 'manifest' tag -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <!-- Add below permission inside 'application' tag -->
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
        </intent-filter>
    </receiver>
    

    lib -> main.dart
    import 'package:flutter/material.dart'; 
    import 'package:flutter_local_notifications/flutter_local_notifications.dart'; 
    import 'package:workmanager/workmanager.dart'; 
    
    void main() { 
        
    // needed if you intend to initialize in the `main` function 
    WidgetsFlutterBinding.ensureInitialized(); 
    Workmanager.initialize(         
        // The top level function, aka callbackDispatcher 
        callbackDispatcher,         
        // If enabled it will post a notification whenever 
        // the task is running. Handy for debugging tasks 
        isInDebugMode: true
    ); 
    // Periodic task registration 
    Workmanager.registerPeriodicTask( 
        "2",        
        //This is the value that will be 
        // returned in the callbackDispatcher 
        "simplePeriodicTask", 
        
        // When no frequency is provided 
        // the default 15 minutes is set. 
        // Minimum frequency is 15 min. 
        // Android will automatically change 
        // your frequency to 15 min 
        // if you have configured a lower frequency. 
        frequency: Duration(minutes: 15), 
    ); 
    runApp(MyApp()); 
    } 
    
    void callbackDispatcher() { 
    Workmanager.executeTask((task, inputData) { 
        
        // initialise the plugin of flutterlocalnotifications. 
        FlutterLocalNotificationsPlugin flip = new FlutterLocalNotificationsPlugin(); 
        
        // app_icon needs to be a added as a drawable 
        // resource to the Android head project. 
        var android = new AndroidInitializationSettings('@mipmap/ic_launcher'); 
        var IOS = new IOSInitializationSettings(); 
        
        // initialise settings for both Android and iOS device. 
        var settings = new InitializationSettings(android, IOS); 
        flip.initialize(settings); 
        _showNotificationWithDefaultSound(flip); 
        return Future.value(true); 
    }); 
    } 
    
    Future _showNotificationWithDefaultSound(flip) async { 
        
    // Show a notification after every 15 minute with the first 
    // appearance happening a minute after invoking the method 
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails( 
        'your channel id', 
        'your channel name', 
        'your channel description', 
        importance: Importance.Max, 
        priority: Priority.High 
    ); 
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails(); 
        
    // initialise channel platform for both Android and iOS device. 
    var platformChannelSpecifics = new NotificationDetails( 
        androidPlatformChannelSpecifics, 
        iOSPlatformChannelSpecifics 
    ); 
    await flip.show(0, 'GeeksforGeeks', 
        'Your are one step away to connect with GeeksforGeeks', 
        platformChannelSpecifics, payload: 'Default_Sound'
    ); 
    } 
    
    class MyApp extends StatelessWidget { 
    // This widget is the root of your application. 
    @override 
    Widget build(BuildContext context) { 
        return MaterialApp( 
        title: 'Geeks Demo', 
        theme: ThemeData( 
            
            // This is the theme 
            // of your application. 
            primarySwatch: Colors.green, 
        ), 
        home: HomePage(title: "GeeksforGeeks"), 
        ); 
    } 
    } 
    
    class HomePage extends StatefulWidget { 
    HomePage({Key key, this.title}) : super(key: key); 
    
    final String title; 
    
    @override 
    _HomePageState createState() => _HomePageState(); 
    } 
    
    class _HomePageState extends State<HomePage> { 
    @override 
    Widget build(BuildContext context) { 
        return Scaffold( 
        appBar: AppBar( 
            title: Text(widget.title), 
        ), 
        body: new Container(), 
        ); 
    } 
    }