110 lines
4.4 KiB
Dart
110 lines
4.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
Future<void> _firebaseMessagingBackgroundHandler(message) async {
|
|
await Firebase.initializeApp();
|
|
}
|
|
|
|
const AndroidNotificationChannel channel = AndroidNotificationChannel(
|
|
'high_importance_channel',
|
|
'High Importance Notifications',
|
|
importance: Importance.high,
|
|
enableVibration: true,
|
|
playSound: true,
|
|
);
|
|
|
|
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
class FirebaseX{
|
|
FirebaseX._();
|
|
static final FirebaseX instance = FirebaseX._();
|
|
FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
|
|
|
|
void onRegisterFirebaseNotificationTopics(List<String> topics){
|
|
for(var topic in topics){
|
|
firebaseMessaging.subscribeToTopic(topic);
|
|
}
|
|
}
|
|
|
|
Future<String> _downloadAndSaveFile(String url, String fileName) async {
|
|
final Directory directory = await getApplicationDocumentsDirectory();
|
|
final String filePath = '${directory.path}/$fileName';
|
|
final http.Response response = await http.get(Uri.parse(url));
|
|
final File file = File(filePath);
|
|
await file.writeAsBytes(response.bodyBytes);
|
|
return filePath;
|
|
}
|
|
|
|
//notification received during app background
|
|
Future<void> onFirebaseNotificationReceivedInBackground() async{
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
|
|
print("background notification: ${message.data}");
|
|
RemoteNotification? notification = message.notification;
|
|
if (notification != null) {
|
|
final String largeIconPath = (notification.android?.imageUrl ?? "").isNotEmpty ? await _downloadAndSaveFile(notification.android?.imageUrl ?? "", 'largeIcon') : "";
|
|
final String bigPicturePath = (notification.android?.imageUrl ?? "").isNotEmpty ? await _downloadAndSaveFile(notification.android?.imageUrl ?? "", 'bigPicture') : "";
|
|
final BigPictureStyleInformation bigPictureStyleInformation = BigPictureStyleInformation(FilePathAndroidBitmap(bigPicturePath),
|
|
largeIcon: FilePathAndroidBitmap(largeIconPath),
|
|
hideExpandedLargeIcon: true,
|
|
contentTitle: notification.title,
|
|
summaryText: notification.body,);
|
|
final AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails(
|
|
channel.id, channel.name,
|
|
channelDescription: channel.description,
|
|
largeIcon: (largeIconPath.isEmpty) ? null : FilePathAndroidBitmap(largeIconPath),
|
|
styleInformation: (bigPicturePath.isEmpty) ? null : bigPictureStyleInformation,
|
|
icon: notification.android?.smallIcon
|
|
);
|
|
final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
|
|
flutterLocalNotificationsPlugin.show(
|
|
notification.hashCode,
|
|
notification.title,
|
|
notification.body,
|
|
platformChannelSpecifics);
|
|
}
|
|
});
|
|
}
|
|
|
|
//click event for firebase push notification
|
|
void onNotificationClicked() {
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
onClicked(message);
|
|
});
|
|
}
|
|
|
|
void onClicked(RemoteMessage message){
|
|
|
|
}
|
|
|
|
//initializing firebase push notification
|
|
Future<void> onFirebaseMessagingInitialize() async{
|
|
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
|
|
await FirebaseMessaging.instance.requestPermission(alert: true,badge: true,sound: true);
|
|
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
|
|
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
|
|
alert: true,
|
|
badge: true,
|
|
sound: true,
|
|
);
|
|
await onFirebaseNotificationReceived();
|
|
await onFirebaseNotificationReceivedInBackground();
|
|
onNotificationClicked();
|
|
}
|
|
|
|
//notification received during app terminated
|
|
Future<void> onFirebaseNotificationReceived() async{
|
|
RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage();
|
|
if(message != null){
|
|
Future.delayed(const Duration(seconds: 1),(){
|
|
onClicked(message);
|
|
});
|
|
}
|
|
}
|
|
|
|
} |