I'm trying to use the Android alarmmanager with notifications, but I am encounter difficulties. Basically, this is the behaviour I am trying to achieve:
- Fetch the point in time when the alarmmanager has to fire from sharedpreferences and/or firebase. Use this to schedule the alarmmanager.
- When the alarmmanager fires, fetch some data from sharedpreferences and/or firebase and use it to create a notification. Also perform step 1 to schedule the next alarm.
- When the notification is pressed, a specific page has to open.
I created some basic example which is available here: https://github.com/robindijkhof/flutter_noti I will include a snippet below for when the repo is deleted.
Problems I am encountering:
- I can't open a specific page when the notification is clicked.
- When the app is closed using the back button, the alarmmanager keep firing which is as expected. However, when the notification is clicked, the app opens and the alarmmanager does not fire anymore. Probably because it runs on another Isolate?
I have no clue how to solve this. Also, I have no idea if I am on the right track. I'd appreciate some help.
Snippet
import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class AlarmHelper {
static final int _REQUEST_CODE = 12377;
static final int _REQUEST_CODE_OVERTIME = 12376;
static void scheduleAlarm() async {
print('schedule');
//Read the desired time from sharedpreferences and/or firebase.
AndroidAlarmManager.cancel(_REQUEST_CODE);
AndroidAlarmManager.oneShot(Duration(seconds: 10), _REQUEST_CODE, clockIn, exact: true, wakeup: true);
}
}
void clockIn() async {
//Do Stuff
print('trigger');
//Read some stuff from sharedpreference and/or firebase.
setNotification();
//Schedule the next alarm.
AlarmHelper.scheduleAlarm();
}
void setNotification() async {
print('notification set');
//Read some more stuff from sharedpreference and/or firebase. Use that information for the notification text.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var androidPlatformChannelSpecifics = new AndroidNotificationDetails('test', 'test', 'test',
importance: Importance.Max, priority: Priority.Max, ongoing: true, color: Colors.blue[500]);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0, 'test', 'time ' + DateTime.now().toIso8601String(), platformChannelSpecifics, payload: 'item id 2');
}
from Flutter Android combine alarmmanager with notifications
No comments:
Post a Comment