Displaying a Notification When Your Flutter App is Killed

Gautier de Lataillade
2 min readApr 16, 2023

--

In this article, we will discuss how to develop a feature in Flutter that displays a notification when a user kills the application. This can be particularly useful for apps that rely on background processes or need to stay alive, such as alarm or timer apps.

Why this feature ?

When users kill an application with background processes, all processes are terminated. This can cause issues for apps that need to continue running in the background, such as an alarm app. To address this problem, we can display a notification that prompts users to reopen the app, allowing them to reschedule their alarms automatically. While this feature can be helpful, it’s essential to use it judiciously, as displaying a notification when the app is killed may be intrusive and negatively impact the user experience.

Step-by-step guide

To illustrate this feature, we are going to build an simple application with a single screen. This screen will contain a regular switch button, to enable and disable our notification:

Switch(
value: value,
onChanged: (bool _) {
setState(() => value = _);
NotifOnKill.toggleNotifOnKill(value);
},
),

Then, we can create our class NotifOnKill that will use method channels to call platform-specific code:

class NotifOnKill {
static const platform = MethodChannel(
'com.example.your_app_name/notif_on_kill');

static Future<void> toggleNotifOnKill(bool value) async {
try {
if (value) {
await platform.invokeMethod(
'setNotificationOnKillService',
{
'title': "Application killed !",
'description': "The application just got killed.",
},
);
print('NotificationOnKillService set with success');
} else {
await platform.invokeMethod('stopNotificationOnKillService');
print('NotificationOnKillService stopped with success');
}
} catch (e) {
print('NotificationOnKillService error: $e');
}
}
}

To toggle notification on kill, we’ll just have to pass true or false to our toggleNotifOnKill method. We can also pass a title and a description in order to customize the content of our notification.

Now it’s time to implement the native code for Android and iOS. Click on the files to be redirected to the full source code.

Android part

For the Android implementation, you will need to create a NotificationOnKillService.kt, and make the link with your MainActivity.kt, which will handle your method channel calls. Here are the full code of these files:

MainActivity.kt

Request notification permission and handle method channel calls.

NotificationOnKillService.kt

Prepare and show the notification when the app is killed.

AndroidManifest.xml

Insert the notification permission and add the NotificationOnKillService.

iOS part

For the iOS part, your AppDelegate will handle everything. We override the applicationWillTerminate method, which is automatically executed when the app is terminated:

AppDelegate

Request notification permission, handle method channel calls and show notification when app is killed.

Conclusion

In this article, we’ve explored how to implement a notification-on-app-kill feature in Flutter for Android and iOS. It’s important to balance functionality with user experience and use this feature wisely to ensure that it adds value to your app without becoming intrusive for your users.

For a proof-of-concept application and detailed code, please check this GitHub repository I created for this article. If you have any questions, feel free to open an issue on the repository.

Note that the notification won’t show in debug mode.

--

--