I installed android alarm manager package and print code on background its work well. But how can show my alarm screen like WhatsApp receive call for example, can I do this with flutter ?
void runOnBackGround() async {
final int helloAlarmID = 0;
await AndroidAlarmManager.initialize();
await AndroidAlarmManager.periodic(
const Duration(seconds: 1), helloAlarmID, callBack,
wakeup: true);
}
void callBack(i) async {
final DateTime now = DateTime.now();
print("[$now] id = $i Hello, world! ");
}
Well in my case, I modified the plugin code and needed some permission. I found this solution from geisterfurz007/random-alarm. It makes can show the app from the background.
EDIT: To open an app when an alarm goes off, Need to modify the plugin.
Open the project and find the android_alarm_manager-2.0.0
in the External Libraries/Flutter Plugins
. And find AlarmBroadcastReceiver.java
, copy-paste the following code. That code is from the flutter issue.
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugins.androidalarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.content.pm.PackageManager;
import android.view.WindowManager;
public class AlarmBroadcastReceiver extends BroadcastReceiver {
private static PowerManager.WakeLock wakeLock;
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "My wakelock");
Intent startIntent = context
.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
startIntent.setFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
);
wakeLock.acquire();
context.startActivity(startIntent);
AlarmService.enqueueAlarmProcessing(context, intent);
wakeLock.release();
}
}
There is a need for permission to run the app when the alarm goes off from the background.
You can access permissions by using permission_handler.