Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void initialNotification() {
- var initializationSettingsAndroid =
- AndroidInitializationSettings('@mipmap/ic_launcher');
- var initializationSettingsIOS = IOSInitializationSettings(
- onDidReceiveLocalNotification: onDidReceiveLocalNotification);
- var initializationSettings = InitializationSettings(
- android: initializationSettingsAndroid,
- iOS: initializationSettingsIOS,
- );
- flutterLocalNotificationsPlugin.initialize(initializationSettings,
- onSelectNotification: selectNotification);
- FirebaseMessaging.onMessage.listen((RemoteMessage message) {
- _notificationController.getNotifications(false);
- print('Got a message whilst in the foreground!');
- print('msg: ${message.notification!.body}');
- print('msg: ${message.data}');
- if (message.notification != null) {
- MainController _mainController = Get.put(MainController());
- print("entro notificacion");
- if (message.data['type'] == 'message' &&
- _mainController.dataWeb.value.chatUserId !=
- int.parse(message.data['userid']))
- showNotification(message.notification!.title,
- message.notification!.body, message.data);
- else if (message.data['type'] != 'message')
- showNotification(message.notification!.title,
- message.notification!.body, message.data);
- }
- });
- }
- Future onDidReceiveLocalNotification(
- int? id, String? title, String? body, String? payload) async {
- print('onDidReceiveLocalNotification');
- }
- Future selectNotification(String? payload) async {
- MainController _mainController = Get.put(MainController());
- var dataPayload = jsonDecode(payload!);
- _mainController.typeNotification.value = dataPayload['type'];
- _mainController.urlWebSite.value = dataPayload['url'];
- if (dataPayload['type'] == 'message')
- _mainController.dataWeb.value.chatUserId =
- int.parse(dataPayload['userid']);
- else
- _mainController.dataWeb.value.chatUserId = 0;
- _mainController.changeRoute();
- }
- void showNotification(title, body, data) async {
- var androidNotificationDetails;
- BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
- body,
- htmlFormatBigText: true,
- contentTitle: title,
- htmlFormatContentTitle: true,
- summaryText: title,
- htmlFormatSummaryText: true,
- );
- if (data['type'] == 'message') {
- var largeIconPath = await _downloadAndSaveFile(
- data['imageUrl'],
- data['username'] +
- '_' +
- DateTime.now().millisecondsSinceEpoch.toString());
- androidNotificationDetails = AndroidNotificationDetails(
- channelID,
- channelName,
- groupKey: groupkeyFCM,
- importance: Importance.defaultImportance,
- priority: Priority.defaultPriority,
- playSound: true,
- largeIcon: FilePathAndroidBitmap(largeIconPath),
- styleInformation: bigTextStyleInformation,
- );
- } else {
- androidNotificationDetails = AndroidNotificationDetails(
- channelID,
- channelName,
- groupKey: groupkeyFCM,
- importance: Importance.defaultImportance,
- priority: Priority.defaultPriority,
- playSound: true,
- styleInformation: bigTextStyleInformation,
- );
- }
- var iOSNotificationDetails = IOSNotificationDetails(
- presentSound: false,
- );
- var platformChannelSpecifics = NotificationDetails(
- android: androidNotificationDetails, iOS: iOSNotificationDetails);
- await flutterLocalNotificationsPlugin.show(
- id, title, body, platformChannelSpecifics,
- payload: jsonEncode(data));
- id = id + 1;
- NotificationDetails groupNotification = getGroupNotifier();
- await flutterLocalNotificationsPlugin.show(
- 0, 'test', '$id Notificación', groupNotification);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement