skip to Main Content

I am developing an Android app using Flutter and currently working on implementing notifications. I have successfully set up weekly notifications, but I want to add a condition: the notification should be triggered only if the app has not been opened for one week. This should work even when the app is closed.

How can I implement this logic in Flutter?

I’ve explored options with flutter_local_notifications and background execution, but I’m unsure about the best approach to track the app’s last opening time and trigger the notification accordingly.

That is my Code:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin();

  Future<void> initNotification() async {
    notificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.requestNotificationsPermission();
    notificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.requestExactAlarmsPermission();
  }

  Future<void> scheduleNotification() async {
    const AndroidNotificationDetails androidNotificationDetails =
    AndroidNotificationDetails(
        'repeating channel id', 'repeating channel name',
        channelDescription: 'repeating description',
        icon: '@drawable/sn_invent');
    const NotificationDetails notificationDetails =
    NotificationDetails(android: androidNotificationDetails);
    await notificationsPlugin.periodicallyShow(0, 'Ziele in Angriff genommen?',
        'Schau doch gerne mal wieder deinen Zielplan an', RepeatInterval.weekly, notificationDetails,
        androidAllowWhileIdle: true);
  }
}

2

Answers


  1. in the logic of how to send the notification
    create schedual notification in main class set the id for this notification static like user id then when the user open the application the function run to schedual notification from DateTime.now().add(Duration(days:7))
    every time the user was open the app the date for notification was change to 7 days after this day but besure the id for notification be static

    Login or Signup to reply.
  2. You need to keep track of when the app is opened and when app is closed. You can do this by shared_preferences

    Here’s how:

    Future<void> scheduleNotification() async {
        if (await shouldTriggerNotification()) {
          const AndroidNotificationDetails androidNotificationDetails =
              AndroidNotificationDetails(
            'repeating channel id',
            'repeating channel name',
            channelDescription: 'repeating description',
            icon: '@drawable/sn_invent',
          );
          const NotificationDetails notificationDetails =
              NotificationDetails(android: androidNotificationDetails);
          await notificationsPlugin.periodicallyShow(
            0,
            'Ziele in Angriff genommen?',
            'Schau doch gerne mal wieder deinen Zielplan an',
            RepeatInterval.weekly,
            notificationDetails,
            androidAllowWhileIdle: true,
          );
    
          await updateLastOpenedTime();
        }
      }
    
      Future<bool> shouldTriggerNotification() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        DateTime lastOpenedTime =
            DateTime.parse(prefs.getString('lastOpenedTime') ?? '');
    
        int daysDifference = DateTime.now().difference(lastOpenedTime).inDays;
    
        return daysDifference >= 7;
      }
    
      Future<void> updateLastOpenedTime() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('lastOpenedTime', DateTime.now().toIso8601String());
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search