notification_when_app_is_killed 0.0.1 copy "notification_when_app_is_killed: ^0.0.1" to clipboard
notification_when_app_is_killed: ^0.0.1 copied to clipboard

Push notification when app is killed

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:notification_when_app_is_killed/model/args_for_kill_notification.dart';
import 'package:notification_when_app_is_killed/notification_when_app_is_killed.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _success = false;
  final _notificationWhenAppIsKilledPlugin = NotificationWhenAppIsKilled();

  Future<void> setNotificationOnKill() async {
    bool success;
    try {
      success = await _notificationWhenAppIsKilledPlugin.setNotificationOnKillService(
            ArgsForKillNotification(
                title: 'The app is killed', description: 'You can see this notification when the app is killed'),
          ) ??
          false;
    } on PlatformException {
      success = false;
    }
    setState(() {
      _success = success;
    });
  }

  Future<void> cancelNotificationOnKill() async {
    bool success;
    try {
      success = await _notificationWhenAppIsKilledPlugin.cancelNotificationOnKillService() ?? false;
    } on PlatformException {
      success = false;
    }
    setState(() {
      _success = success;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Notification when app is killed example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(onPressed: setNotificationOnKill, child: const Text('Set notification when app is killed')),
              TextButton(
                  onPressed: cancelNotificationOnKill, child: const Text('Cancel notification when app is killed')),
              const SizedBox(height: 20),
              Text('Last method call status: $_success\n'),
            ],
          ),
        ),
      ),
    );
  }
}