|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:developer'; |
| 3 | +import 'dart:io'; |
| 4 | + |
| 5 | +import 'package:firebase_core/firebase_core.dart'; |
| 6 | +import 'package:firebase_messaging/firebase_messaging.dart'; |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; |
| 9 | +import 'package:mvc_flutter/my_app/my_app.dart'; |
| 10 | + |
| 11 | +@pragma('vm:entry-point') |
| 12 | +Future<void> firebaseMessagingBackgroundHandler( |
| 13 | + RemoteMessage remoteMessage) async { |
| 14 | + await NotificationHandler.initialize(); |
| 15 | + log(remoteMessage.data.toString(), name: "Background Notification"); |
| 16 | + |
| 17 | + ///TODO Handling Background Message Notification |
| 18 | +} |
| 19 | + |
| 20 | +@pragma('vm:entry-point') |
| 21 | +void didReceiveBackgroundNotificationResponseCallback( |
| 22 | + NotificationResponse response) async { |
| 23 | + log(response.notificationResponseType.name, |
| 24 | + name: "onDidReceiveBackgroundNotificationResponse"); |
| 25 | + log(response.payload ?? "Empty Payload", |
| 26 | + name: "onDidReceiveBackgroundNotificationResponse Payload"); |
| 27 | + log(response.input ?? "Empty Input", |
| 28 | + name: "onDidReceiveBackgroundNotificationResponse Input"); |
| 29 | + |
| 30 | + ///TODO Handling Background Message Notification |
| 31 | +} |
| 32 | + |
| 33 | +class NotificationHandler { |
| 34 | + static final FlutterLocalNotificationsPlugin |
| 35 | + _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); |
| 36 | + static final FirebaseMessaging _messaging = FirebaseMessaging.instance; |
| 37 | + |
| 38 | + static Future<void> initialize() async { |
| 39 | + try { |
| 40 | + await Firebase.initializeApp( |
| 41 | + // options: DefaultFirebaseOptions.currentPlatform, |
| 42 | + ); |
| 43 | + await _firebaseInit(); |
| 44 | + if (Platform.isIOS) { |
| 45 | + await _flutterLocalNotificationsPlugin |
| 46 | + .resolvePlatformSpecificImplementation< |
| 47 | + IOSFlutterLocalNotificationsPlugin>() |
| 48 | + ?.requestPermissions( |
| 49 | + alert: true, |
| 50 | + badge: true, |
| 51 | + sound: true, |
| 52 | + ); |
| 53 | + } else { |
| 54 | + await _flutterLocalNotificationsPlugin |
| 55 | + .resolvePlatformSpecificImplementation< |
| 56 | + AndroidFlutterLocalNotificationsPlugin>() |
| 57 | + ?.requestNotificationsPermission(); |
| 58 | + } |
| 59 | + const androidInitializationSettings = |
| 60 | + AndroidInitializationSettings("@mipmap/ic_launcher"); |
| 61 | + const darwinInitializationSettings = DarwinInitializationSettings( |
| 62 | + requestProvisionalPermission: true, requestCriticalPermission: true); |
| 63 | + InitializationSettings settings = const InitializationSettings( |
| 64 | + android: androidInitializationSettings, |
| 65 | + iOS: darwinInitializationSettings, |
| 66 | + ); |
| 67 | + await _flutterLocalNotificationsPlugin.initialize( |
| 68 | + settings, |
| 69 | + onDidReceiveBackgroundNotificationResponse: |
| 70 | + didReceiveBackgroundNotificationResponseCallback, |
| 71 | + onDidReceiveNotificationResponse: (response) { |
| 72 | + //ToDo |
| 73 | + ///Handle notification responses; |
| 74 | + log(response.notificationResponseType.name, |
| 75 | + name: "onDidReceiveNotificationResponse"); |
| 76 | + log(response.payload ?? "Empty Payload", |
| 77 | + name: "onDidReceiveNotificationResponse Payload"); |
| 78 | + log(response.input ?? "Empty Input", |
| 79 | + name: "onDidReceiveNotificationResponse Input"); |
| 80 | + }, |
| 81 | + ); |
| 82 | + } catch (_) {} |
| 83 | + } |
| 84 | + |
| 85 | + static Future<void> _firebaseInit() async { |
| 86 | + try { |
| 87 | + NotificationSettings notificationSettings = |
| 88 | + await _messaging.requestPermission(); |
| 89 | + log(notificationSettings.authorizationStatus.name, |
| 90 | + name: "Notification Permission"); |
| 91 | + |
| 92 | + ///Whenever User Click On Background Notification App Is Terminated....Handle On Tap Through This Method!! |
| 93 | + _messaging.getInitialMessage().then((remoteMessage) { |
| 94 | + if (remoteMessage != null) { |
| 95 | + log("Notification Click Event On Background Application. Data ${remoteMessage.data}, Title ${remoteMessage.notification?.title}"); |
| 96 | + Future.delayed(const Duration(seconds: 2), () { |
| 97 | + if (navigatorKey.currentContext != null) { |
| 98 | + ///ToDo Notification Handle On background notification click; |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + ///ToDo Notification Handle On Click; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + ///Whenever Notification Coming And App Is Not Terminated And Open In Background Only For Show Notification..!! |
| 107 | + FirebaseMessaging.onMessage.listen((remoteMessage) { |
| 108 | + log("On Message Notification Data ${remoteMessage.notification.toString()}, Title ${remoteMessage.notification?.title}"); |
| 109 | + try { |
| 110 | + showLocalNotification(remoteMessage); |
| 111 | + } catch (_) { |
| 112 | + log("Error Catch $_"); |
| 113 | + } |
| 114 | + |
| 115 | + ///ToDo Notification Show On Coming From Firebase; |
| 116 | + }); |
| 117 | + |
| 118 | + ///Whenever User Click On Background Notification App Is Not Terminated Working In Background....Handle On Tap Through This Method!! |
| 119 | + FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) { |
| 120 | + log("Background App Notification Data ${remoteMessage.data}, Title ${remoteMessage.notification?.title}"); |
| 121 | + |
| 122 | + ///ToDo Notification Handle On Click; |
| 123 | + }); |
| 124 | + FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler); |
| 125 | + } catch (_) {} |
| 126 | + } |
| 127 | + |
| 128 | + static Future<String> getToken() async { |
| 129 | + try { |
| 130 | + // if (Platform.isIOS) { |
| 131 | + // String? apn = await _messaging.getAPNSToken(); |
| 132 | + // } |
| 133 | + return await _messaging.getToken() ?? ""; |
| 134 | + } catch (_, st) { |
| 135 | + log('Stack Trace $st'); |
| 136 | + return ""; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static Future<void> showLocalNotification(RemoteMessage remoteMessage) async { |
| 141 | + log("Check Data ${remoteMessage.toMap()}"); |
| 142 | + AndroidNotificationDetails androidNotificationDetails = |
| 143 | + AndroidNotificationDetails( |
| 144 | + remoteMessage.messageId ?? "notification", |
| 145 | + "Friend Notification", |
| 146 | + color: Colors.transparent, |
| 147 | + importance: Importance.max, |
| 148 | + priority: Priority.high, |
| 149 | + channelShowBadge: true, |
| 150 | + // largeIcon: const DrawableResourceAndroidBitmap('@mipmap/ic_launcher'), |
| 151 | + ); |
| 152 | + DarwinNotificationDetails darwinNotificationDetails = |
| 153 | + const DarwinNotificationDetails( |
| 154 | + presentBanner: true, |
| 155 | + presentAlert: true, |
| 156 | + presentBadge: true, |
| 157 | + presentSound: true, |
| 158 | + ); |
| 159 | + NotificationDetails notificationDetails = NotificationDetails( |
| 160 | + android: androidNotificationDetails, |
| 161 | + iOS: darwinNotificationDetails, |
| 162 | + ); |
| 163 | + String title = |
| 164 | + remoteMessage.notification?.title ?? remoteMessage.data["title"]; |
| 165 | + String body = |
| 166 | + remoteMessage.notification?.body ?? remoteMessage.data["body"] ?? ""; |
| 167 | + log("remoteMessage showLocalNotification ${remoteMessage.toString()}, Title $title Body $body"); |
| 168 | + await _flutterLocalNotificationsPlugin.show( |
| 169 | + remoteMessage.ttl ?? 0, |
| 170 | + title, |
| 171 | + body, |
| 172 | + notificationDetails, |
| 173 | + payload: jsonEncode( |
| 174 | + remoteMessage.toMap(), |
| 175 | + ), |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + static Future<void> clearNotification([int? id]) async { |
| 180 | + try { |
| 181 | + if (id != null) { |
| 182 | + await _flutterLocalNotificationsPlugin.cancel(id); |
| 183 | + } else { |
| 184 | + await _flutterLocalNotificationsPlugin.cancelAll(); |
| 185 | + } |
| 186 | + } catch (_) {} |
| 187 | + } |
| 188 | +} |
0 commit comments