In a flutter application, I want to show notifications to the user by flutter_local_notifications
package. This notification appears in the Android emulator, but not in the real mobile device.
This is my source code:
File NotificationManager.dart
:
import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class NotificationManager {
static FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
static Initialize() async {
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
const initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
await flutterLocalNotificationsPlugin?.initialize(
const InitializationSettings(android: initializationSettingsAndroid));
}
static Future<void> ShowNotification(String msg, {String title = ""}) async {
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID',
'CHANNEL_NAME',
//"CHANNEL_DESCRIPTION",
importance: Importance.max,
priority: Priority.high,
playSound: true,
timeoutAfter: 5000,
styleInformation: DefaultStyleInformation(true, true),
);
var platformChannelSpecifics =
NotificationDetails(android: androidChannelSpecifics);
await flutterLocalNotificationsPlugin!.show(
0, // Notification ID
title, // Notification Title
msg, // Notification Body, set as null to remove the body
platformChannelSpecifics,
payload: 'New Payload', // Notification Payload
);
}
}
main.dart
:
...
void main() async {
...
NotificationManager.Initialize();
const duration = const Duration(seconds: 10);
new Timer.periodic(
duration,
(Timer timer) {
NotificationManager.ShowNotification("sample-title", "sample-text");
},
);
Future.delayed(const Duration(milliseconds: 500), () {
runApp(const MyApp());
});
}
...
Emulator: Pixel 6 Pro API 31
– Mobile device: Galaxy A22
I tested it on several other mobile devices, but the same problem was seen on them as well.