إجابة Flutter Dev سؤال في كيفية الاحتفاظ بأكثر من إشعار في notification bar Flutter كانت الإجابة المقبولة
28 مايو 2021
@Wael Aljamal
كل الشكر لك اخي تم حل المشكلتين بفضل الله وفضلك
وهذا الكود كامل للفائده:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
}
class ShowNotifications {
static FirebaseMessaging messaging = FirebaseMessaging.instance;
static void showNotification(String title, String body) async {
await _demoNotification(title, body);
}
static int i = 0;
static Future<void> _demoNotification(String title, String body) async {
Random random = new Random();
int randomNumber = random.nextInt(10);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_ID', 'channel name', 'channel description',
importance: Importance.max,
playSound: true,
showProgress: true,
priority: Priority.high,
ticker: 'test ticker');
var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics, iOS: iOSChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(randomNumber, title, body, platformChannelSpecifics, payload: 'test');
}
static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
static void initialization(){
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
static Future onSelectNotification(String payload) async {
showDialog(
// context: context,
builder: (_) {
return new AlertDialog(
title: Text("PayLoad"),
content: Text("Payload : $payload"),
);
},
);
}
static notification() async {
await Firebase.initializeApp();messaging = FirebaseMessaging.instance;
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
NotificationSettings settings = await messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Message title: ${message.notification?.title}, body: ${message.notification?.body}, data: ${message.data}');
// Parse the message received
PushNotification notification = PushNotification(
title: message.notification?.title,
body: message.notification?.body,
dataTitle: message.data['title'],
dataBody: message.data['body'],
);
// showNotification(data['title'], data['notification']['body']);
if(i%2==0) {
showNotification( notification.title, notification.body);
};
i++;
});
} else {
print('User declined or has not accepted permission');
}
}
}