Anonymous
Уведомление отображается дважды при флаттере
Сообщение
Anonymous » 13 окт 2024, 17:16
Я застрял. Мое уведомление в фоновом режиме отображается дважды. Но на переднем плане только одно уведомление. Это мой код
Код: Выделить всё
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
if(
!AwesomeStringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) ||
!AwesomeStringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true)
){
// print('message also contained a notification: ${message.notification.body}');
String imageUrl;
imageUrl ??= message.notification.android?.imageUrl;
imageUrl ??= message.notification.apple?.imageUrl;
print(imageUrl);
if(imageUrl == null){
var id = Random().nextInt(2147483647);
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
title: message.notification.title,
body: message.notification.body,
color: Colors.orange,
customSound: 'resource://raw/alert',
notificationLayout: NotificationLayout.BigText,
channelKey: 'basic_channel_background',
payload: {'data':message.data['payload']}
),
actionButtons: [
NotificationActionButton(
label: 'Lihat Selengkapnya',
enabled: true,
buttonType: ActionButtonType.Default,
key: 'background',
)
]
);
}else{
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: Random().nextInt(2147483647),
title: message.notification.title,
body: message.notification.body,
bigPicture: imageUrl,
color: Colors.orange,
customSound: 'resource://raw/alert',
notificationLayout: NotificationLayout.BigPicture,
channelKey: 'basic_channel_background',
payload: {'data':message.data['payload']}
),
actionButtons: [
NotificationActionButton(
label: 'Lihat Selengkapnya',
enabled: true,
buttonType: ActionButtonType.Default,
key: 'background',
)
]
);
}
}
}
// End Background Message
и это мой код переднего плана
Код: Выделить всё
// Foreground Apps
Future main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb) {
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
await AwesomeNotifications().initialize(
'resource://mipmap/launcher_icon',
[
NotificationChannel(
channelGroupKey: 'basic_tests',
channelKey: 'basic_channel',
channelName: 'My Cahaya Notification',
channelDescription: 'Notification channel for basic tests',
icon: 'resource://mipmap/launcher_icon',
importance: NotificationImportance.High,
playSound: true,
soundSource: 'resource://raw/alert'
),
NotificationChannel(
channelGroupKey: 'basic_background',
channelKey: 'basic_channel_background',
channelName: 'My Cahaya Notification Background',
channelDescription: 'Notification channel for basic tests',
icon: 'resource://mipmap/launcher_icon',
importance: NotificationImportance.High,
playSound: true,
soundSource: 'resource://raw/alert'
),
], debug: true);
AwesomeNotifications().actionStream.listen((event) async{
print('event received!');
var data = event.toMap();
if(data['buttonKeyPressed'] == "background"){
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("Navigate", data['payload']['data']);
}else{
_navigateToItemForeground(data['payload']['data']);
}
// do something based on event...
// AwesomeNotifications().actionSink.close();
});
}
FirebaseMessaging.onMessage.listen((RemoteMessage message) async{
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
if(!AwesomeStringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) ||
!AwesomeStringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true)){
print("something");
String imageUrl;
imageUrl ??= notification.android?.imageUrl;
imageUrl ??= notification.apple?.imageUrl;
if(imageUrl == null){
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: Random().nextInt(2147483647),
title: notification.title,
body: notification.body,
color: Colors.orange,
customSound: 'resource://raw/alert',
notificationLayout: NotificationLayout.BigText,
channelKey: 'basic_channel',
payload: {'data':message.data['payload']}
),
actionButtons: [
NotificationActionButton(
label: 'Lihat Selengkapnya',
enabled: true,
buttonType: ActionButtonType.Default,
key: 'test',
)
]
);
}else{
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: Random().nextInt(2147483647),
title: notification.title,
body: notification.body,
bigPicture: imageUrl,
color: Colors.orange,
customSound: 'resource://raw/alert',
notificationLayout: NotificationLayout.BigPicture,
channelKey: 'basic_channel',
payload: {'data':message.data['payload']}
),
actionButtons: [
NotificationActionButton(
label: 'Lihat Selengkapnya',
enabled: true,
buttonType: ActionButtonType.Default,
key: 'test',
)
]
);
}
}
}
});
runApp(MyApp());
}
// End Foreground Apps
а это мой скриншот
скриншот
Я пробовал изменить потрясающее уведомление, но результат тот же. Не могли бы вы помочь мне решить эту проблему? Надеюсь, вы сможете мне помочь. Спасибо большое
Подробнее здесь:
https://stackoverflow.com/questions/709 ... on-flutter
1728829015
Anonymous
Я застрял. Мое уведомление в фоновом режиме отображается дважды. Но на переднем плане только одно уведомление. Это мой код [code]Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, // make sure you call `initializeApp` before using other Firebase services. await Firebase.initializeApp(); if( !AwesomeStringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) || !AwesomeStringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true) ){ // print('message also contained a notification: ${message.notification.body}'); String imageUrl; imageUrl ??= message.notification.android?.imageUrl; imageUrl ??= message.notification.apple?.imageUrl; print(imageUrl); if(imageUrl == null){ var id = Random().nextInt(2147483647); await AwesomeNotifications().createNotification( content: NotificationContent( id: id, title: message.notification.title, body: message.notification.body, color: Colors.orange, customSound: 'resource://raw/alert', notificationLayout: NotificationLayout.BigText, channelKey: 'basic_channel_background', payload: {'data':message.data['payload']} ), actionButtons: [ NotificationActionButton( label: 'Lihat Selengkapnya', enabled: true, buttonType: ActionButtonType.Default, key: 'background', ) ] ); }else{ await AwesomeNotifications().createNotification( content: NotificationContent( id: Random().nextInt(2147483647), title: message.notification.title, body: message.notification.body, bigPicture: imageUrl, color: Colors.orange, customSound: 'resource://raw/alert', notificationLayout: NotificationLayout.BigPicture, channelKey: 'basic_channel_background', payload: {'data':message.data['payload']} ), actionButtons: [ NotificationActionButton( label: 'Lihat Selengkapnya', enabled: true, buttonType: ActionButtonType.Default, key: 'background', ) ] ); } } } // End Background Message [/code] и это мой код переднего плана [code]// Foreground Apps Future main() async{ WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); if (!kIsWeb) { await FirebaseMessaging.instance .setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); await AwesomeNotifications().initialize( 'resource://mipmap/launcher_icon', [ NotificationChannel( channelGroupKey: 'basic_tests', channelKey: 'basic_channel', channelName: 'My Cahaya Notification', channelDescription: 'Notification channel for basic tests', icon: 'resource://mipmap/launcher_icon', importance: NotificationImportance.High, playSound: true, soundSource: 'resource://raw/alert' ), NotificationChannel( channelGroupKey: 'basic_background', channelKey: 'basic_channel_background', channelName: 'My Cahaya Notification Background', channelDescription: 'Notification channel for basic tests', icon: 'resource://mipmap/launcher_icon', importance: NotificationImportance.High, playSound: true, soundSource: 'resource://raw/alert' ), ], debug: true); AwesomeNotifications().actionStream.listen((event) async{ print('event received!'); var data = event.toMap(); if(data['buttonKeyPressed'] == "background"){ SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString("Navigate", data['payload']['data']); }else{ _navigateToItemForeground(data['payload']['data']); } // do something based on event... // AwesomeNotifications().actionSink.close(); }); } FirebaseMessaging.onMessage.listen((RemoteMessage message) async{ RemoteNotification notification = message.notification; AndroidNotification android = message.notification?.android; if (notification != null && android != null && !kIsWeb) { if(!AwesomeStringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) || !AwesomeStringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true)){ print("something"); String imageUrl; imageUrl ??= notification.android?.imageUrl; imageUrl ??= notification.apple?.imageUrl; if(imageUrl == null){ await AwesomeNotifications().createNotification( content: NotificationContent( id: Random().nextInt(2147483647), title: notification.title, body: notification.body, color: Colors.orange, customSound: 'resource://raw/alert', notificationLayout: NotificationLayout.BigText, channelKey: 'basic_channel', payload: {'data':message.data['payload']} ), actionButtons: [ NotificationActionButton( label: 'Lihat Selengkapnya', enabled: true, buttonType: ActionButtonType.Default, key: 'test', ) ] ); }else{ await AwesomeNotifications().createNotification( content: NotificationContent( id: Random().nextInt(2147483647), title: notification.title, body: notification.body, bigPicture: imageUrl, color: Colors.orange, customSound: 'resource://raw/alert', notificationLayout: NotificationLayout.BigPicture, channelKey: 'basic_channel', payload: {'data':message.data['payload']} ), actionButtons: [ NotificationActionButton( label: 'Lihat Selengkapnya', enabled: true, buttonType: ActionButtonType.Default, key: 'test', ) ] ); } } } }); runApp(MyApp()); } // End Foreground Apps [/code] а это мой скриншот скриншот Я пробовал изменить потрясающее уведомление, но результат тот же. Не могли бы вы помочь мне решить эту проблему? Надеюсь, вы сможете мне помочь. Спасибо большое Подробнее здесь: [url]https://stackoverflow.com/questions/70921767/notification-show-twice-on-flutter[/url]