Created
September 19, 2021 17:35
-
-
Save bellrd/af070e67f07a886b628f061f574180bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | |
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = | |
FlutterLocalNotificationsPlugin(); | |
const AndroidNotificationChannel highPriorityChannel = | |
AndroidNotificationChannel( | |
'ORDER_UPDATE', // id | |
'ORDER UPDATES', // title | |
'Real-time notification of order update,change.', // description | |
importance: Importance.max, | |
enableVibration: true, | |
playSound: true, | |
showBadge: true, | |
); | |
const AndroidNotificationChannel generalChannel = AndroidNotificationChannel( | |
'GENERAL', // id | |
'GENERAL NOTIFICATIONS', // title | |
'General notification about offers, alert, promotions etc', // description | |
importance: Importance.defaultImportance, | |
playSound: true, | |
); | |
void _onMessageHandler(RemoteMessage message) { | |
print("************************************************"); | |
print("on message called ${message.notification}"); | |
print("on message called ${message.data}"); | |
print("************************************************"); | |
RemoteNotification? notification = message.notification; | |
AndroidNotification? android = message.notification?.android; | |
if (notification != null && android != null) { | |
flutterLocalNotificationsPlugin.show( | |
notification.hashCode % 128, | |
notification.title, | |
notification.body, | |
NotificationDetails( | |
android: AndroidNotificationDetails( | |
highPriorityChannel.id, | |
highPriorityChannel.name, | |
highPriorityChannel.description, | |
// other properties... | |
), | |
)); | |
} | |
} | |
Future<void> _backgroundMessageHandler(RemoteMessage message) async { | |
print("**************************************************"); | |
print("Handling a background message: ${message.notification}"); | |
print("Handling a background message: ${message.data}"); | |
print("**************************************************"); | |
} | |
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project | |
const AndroidInitializationSettings initializationSettingsAndroid = | |
AndroidInitializationSettings('notification'); | |
void initializeNotification() async { | |
final InitializationSettings initializationSettings = | |
InitializationSettings(android: initializationSettingsAndroid); | |
await flutterLocalNotificationsPlugin.initialize(initializationSettings); | |
await flutterLocalNotificationsPlugin | |
.resolvePlatformSpecificImplementation< | |
AndroidFlutterLocalNotificationsPlugin>() | |
?.createNotificationChannel(highPriorityChannel); | |
await flutterLocalNotificationsPlugin | |
.resolvePlatformSpecificImplementation< | |
AndroidFlutterLocalNotificationsPlugin>() | |
?.createNotificationChannel(generalChannel); | |
FirebaseMessaging.onMessage.listen(_onMessageHandler); | |
FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment