Flutter iOS goes into background, timer not running

Viewed 519

Hi I have an iOS app for flutter. I have a timer scheduled to run every 30 seconds to display a push notification. When the app is in foreground it works fine.

When the app goes into background (AppLifecycleState.paused), the push notification does not appear anymore every 30 seconds. Does the timer stop running after going into background? Am I doing it wrongly?

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

class _HomeState extends State<Home>{ 
  Timer notiTimer;

  @override
  initState() {
    super.initState();

    startTimer();
  }

  @override
  void dispose(){
    notiTimer.cancel();
  }

  void startTimer(){
    notiTimer = Timer.periodic(Duration(seconds: 30), (timer) {
      showNotification()
    });
  }

  Future<void> showNotification() async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.Min, priority: Priority.Min, ticker: 'ticker');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();

    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await _flutterLocalNotificationsPlugin.show(
        0,
        'Mobile App',
        'Notification Appear',
        platformChannelSpecifics,
        payload: '');
  } 
}
0 Answers
Related