I am building an Android application using Flutter. My app needs to display local notification. I am using this package, https://pub.dev/packages/flutter_local_notifications. When I show the notification on the emulator it is working. But it is not working, when I compiled the release APK and installed it on the actual device.
This is NotificationService class
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class NotificationService
{
static FlutterLocalNotificationsPlugin? flip;
static FlutterLocalNotificationsPlugin getFlipInstance()
{
if (flip == null) {
flip = FlutterLocalNotificationsPlugin();
// app_icon needs to be a added as a drawable
// resource to the Android head project.
var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
var ios = const IOSInitializationSettings();
var settings = InitializationSettings(android: android, iOS: ios);
flip!.initialize(settings);
}
return flip as FlutterLocalNotificationsPlugin;
}
static Future showNotification(String title, String body) async {
var androidPlatformChannelSpecifics = const AndroidNotificationDetails("your channel id", "my channel name");
var iOSPlatformChannelSpecifics = const IOSNotificationDetails();
// initialise channel platform for both Android and iOS device.
var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
await getFlipInstance().show(0, title,
body,
platformChannelSpecifics, payload: 'Default_Sound'
);
}
}
I show notification like this in the initState method of home page.
@override
void initState() {
super.initState();
setState(() {
NotificationService.showNotification("Local Notification", "Local Notification Message");
}
As I mentioned, it works on the emulator. But when I generated an APK and installed it on the real device, it is not working.