Skip to content

Instantly share code, notes, and snippets.

@OmarYehiaDev
Created April 5, 2022 14:04
Show Gist options
  • Save OmarYehiaDev/c7c2cbcca982650736fb4a18778eb9f5 to your computer and use it in GitHub Desktop.
Save OmarYehiaDev/c7c2cbcca982650736fb4a18778eb9f5 to your computer and use it in GitHub Desktop.
Future<void> openOffersPage(RemoteMessage? message) async {
if (message != null) {
if (message.data['type'] == 'offers') {
Navigator.push(
key.currentContext!,
MaterialPageRoute(
builder: (_) => HotOffers(),
),
);
}
}
await messaging.getInitialMessage().then((msg) {
if (msg!.data['type'] == 'offers') {
Navigator.push(
key.currentContext!,
MaterialPageRoute(
builder: (_) => HotOffers(),
),
);
}
});
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification
badge: true,
sound: true,
);
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
// local notification to show to users using the created channel.
if (notification != null && android != null) {
await openOffersPage(message);
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: 'notification',
),
),
payload: "offers",
);
}
print("Handling a background message: ${message.messageId}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment