awesome_notifications
awesome_notifications copied to clipboard
Notifications popup are shown twice
As title says, on both Android and iOS, following the official Awesome Notifications - Firebase implementation the notifications messages pop up twice.
The only solution I found to avoid this is not creating a AwesomePushNotification when I receive a Firebase one.
Below the code that causes the error:
Future<void> main() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen(
(RemoteMessage message) {
_onPushReceived(message);
},
);
Future<void> _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();
log("Firebase Push Received");
_onPushReceived(message);
}
void _onPushReceived(RemoteMessage message) {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 0,
channelKey: 'myNotif',
title: message.notification?.title ?? "",
body: message.notification?.body ?? "",
),
);
}
Same happening to me on latest version of the plugin.
void _foregroundMessageListener() {
FirebaseMessaging.onMessage.listen((message) {
final notification = message.notification;
_showNotification(notification);
});
}
void _showNotification(RemoteNotification? notification) {
if (notification != null) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: notification.hashCode,
channelKey: _channelKey,
title: notification.title,
body: notification.body,
wakeUpScreen: true,
autoDismissible: true,
category: NotificationCategory.Event,
notificationLayout: NotificationLayout.Default,
),
);
}
}
}
If you want to use Firebase, you can send a data message instead (leave notification field empty). You can then use Awesome Notifications to create the notifications based on the data
Send from firebase:
{
"notification": {} // REMOVE THIS FIELD (note: cannot use Firebase console)
"data" : {
"id": "1",
"title": "Notification Title",
"body": "Notification body",
}
}
Then create notification based on data
Future<void> initializeFirebaseMessaging() async {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: message.data['id'],
channelKey: 'some_channel',
title: message.data['title'],
body: message.data['body'],
actionType: ActionType.Default,
category: NotificationCategory.Reminder,
),
);
}
Hi! Sorry for taking so long to respond to your issue on GitHub. I was focused on releasing the new 0.7.0 version of awesome_notifications
and awesome_notifications_fcm
and i didn't had time enough to do both. But now I can answer all your questions.
So now i'm asking you to recreate this topic using the new issue template. There's a lot of missing informations that i need to understand your problem, and all the instructions are already in the new issue template. Also, remember to check beforehand if your issue was posted by another user.
So I will automatically close all previous issues so far. Sorry for the inconvenience and i will be waiting for your new issue request.
Thank you so much for your support!