You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0KB

  1. import 'dart:convert';
  2. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  3. import 'package:firebase_messaging/firebase_messaging.dart';
  4. import '../utils/local_storage.dart';
  5. class FirebaseNotificationService {
  6. static FirebaseMessaging messaging = FirebaseMessaging.instance;
  7. static void initService() async {
  8. var token = await messaging.getToken() ?? '';
  9. LocalStorage.save(LocalStorageKey.device_key, token);
  10. print('pushKey: $token');
  11. //config Android channel
  12. const channel = AndroidNotificationChannel(
  13. 'high_importance_channel',
  14. 'High Importance Notifications',
  15. importance: Importance.max,
  16. );
  17. final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  18. await flutterLocalNotificationsPlugin
  19. .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
  20. ?.createNotificationChannel(channel);
  21. //handle message on background android
  22. FirebaseMessaging.onBackgroundMessage(
  23. _firebaseMessagingBackgroundHandler,
  24. );
  25. //Message foreground
  26. FirebaseMessaging.onMessage.listen(
  27. (RemoteMessage message) {
  28. print('Got a message whilst in the foreground!');
  29. print('Message data: $message');
  30. // increase count unread notification
  31. var remoteNotification = message.notification;
  32. var androidNotification = message.notification?.android;
  33. //
  34. var androidDetails = const AndroidNotificationDetails(
  35. 'default_notification_channel_id',
  36. 'Notification',
  37. importance: Importance.max,
  38. priority: Priority.high,
  39. icon: '@mipmap/ic_stat_ic_notification',
  40. );
  41. const iOSDetails = IOSNotificationDetails();
  42. var platformChannelSpecifics = NotificationDetails(
  43. android: androidDetails,
  44. iOS: iOSDetails,
  45. );
  46. //Show notification foreground android
  47. if (remoteNotification != null && androidNotification != null) {
  48. flutterLocalNotificationsPlugin.show(
  49. 0,
  50. remoteNotification.title,
  51. remoteNotification.body,
  52. platformChannelSpecifics,
  53. // NotificationDetails(
  54. // android: AndroidNotificationDetails(
  55. // channel.id,
  56. // channel.name,
  57. // icon: '@mipmap/ic_stat_ic_notification',
  58. // ),
  59. // ),
  60. );
  61. }
  62. flutterLocalNotificationsPlugin.show(
  63. 0,
  64. remoteNotification?.title ?? '',
  65. remoteNotification?.body ?? '',
  66. platformChannelSpecifics,
  67. // NotificationDetails(
  68. // android: AndroidNotificationDetails(
  69. // channel.id,
  70. // channel.name,
  71. // icon: '@mipmap/ic_stat_ic_notification',
  72. // ),
  73. // ),
  74. );
  75. },
  76. );
  77. //Interaction Message from background
  78. FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  79. print('message from background: ${message}');
  80. _handleMessage(message);
  81. });
  82. //Interaction Message from terminal
  83. var initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  84. print('initialMessage: $initialMessage');
  85. if (initialMessage != null) {
  86. _handleMessage(initialMessage);
  87. }
  88. }
  89. static Future onDidReceiveLocalNotification(int? id, String? title, String? body, String? payload) async {
  90. // display a dialog with the notification details, tap ok to go to another page
  91. }
  92. static Future selectNotification(String? payload) async {
  93. try {
  94. var data = json.decode(payload ?? '');
  95. handleNavigateScreen(data);
  96. } catch (e) {
  97. print(e);
  98. }
  99. }
  100. static void _handleMessage(RemoteMessage message) {
  101. try {
  102. var data = message.data;
  103. handleNavigateScreen(data);
  104. } catch (e) {
  105. print(e);
  106. }
  107. }
  108. static void handleNavigateScreen(dynamic data) {}
  109. }
  110. Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  111. print('Handling a background message: ${message}');
  112. }