Tuesday, February 6, 2024
HomeiOS Developmentflutter - FirebaseMessaging notification duplicated on iOS when together with an imageUrl

flutter – FirebaseMessaging notification duplicated on iOS when together with an imageUrl


I am doing a Flutter instance app for FirebaseMessaging to check out how server notification would work. So I’ve adopted the information on FlutterFire and setup all the things and it really works nice on Android.

So far as iOS goes, it appears to work nice so long as I do not embody pictures within the notification. After I do embody an imageUrl, the app exhibits a notification with all the content material apart from the Picture, and instantly after, one other with the identical content material and the picture.

I am fairly misplaced, I do not perceive why it might try this. Whether or not I exploit the take a look at notification from the firebase console or my very own backend utilizing FirebaseMessaging.ship, the conduct is identical.

Listed below are snippets of the code:

important.dart

import 'dart:developer';
import 'dart:io';

import 'package deal:firebase_messaging/firebase_messaging.dart';
import 'package deal:flutter/materials.dart';
import 'package deal:firebase_core/firebase_core.dart';
import 'package deal:flutter_local_notifications/flutter_local_notifications.dart';
import 'package deal:googlenotifpoc/notification_manager.dart';
import 'firebase_options.dart';

void important() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    identify: "...",
    choices: DefaultFirebaseOptions.currentPlatform,
  );
  await FirebaseMessaging.occasion.setAutoInitEnabled(true);

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'Excessive Significance Notifications', // title
    description:
        'This channel is used for vital notifications.', // description
    significance: Significance.max,
  );

  const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
        'high_importance_channel', // id
        'Excessive Significance Notifications',
        channelDescription: 'This channel is used for vital notifications.',
        significance: Significance.max,
        precedence: Precedence.excessive,
        ticker: 'ticker',
        icon: "ic_white",
        playSound: true,
    );

  await FirebaseMessaging.occasion.setForegroundNotificationPresentationOptions(
    alert: true, // Required to show a heads up notification
    badge: true,
    sound: true,
  ); 

  ultimate FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  FirebaseMessaging.onMessage.hear((occasion) => NotificationManager.showNotification(occasion, androidDetails),);

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({tremendous.key});

  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colours.blue,
      ),
      dwelling: const MyHomePage(title: 'Flutter Demo House Web page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({tremendous.key, required this.title});

  ultimate String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget construct(BuildContext context) {
    FirebaseMessaging messaging = FirebaseMessaging.occasion;

    messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: true,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    return Scaffold(
      appBar: AppBar(
        title: Textual content(widget.title),
      ),
      physique: Heart(
        baby: Column(
          mainAxisAlignment: MainAxisAlignment.middle,
          youngsters: <Widget>[
            FutureBuilder<String?>(
              future: getFMCToken(),
              builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
                if (snapshot.hasData) {
                  log(snapshot.data ?? "-");
                  print(snapshot.data ?? "-");
                  return Text(
                    "FMC TOKEN: n ${snapshot.data}",
                    textAlign: TextAlign.center,
                    style: const TextStyle(
                        fontSize: 16, fontWeight: FontWeight.bold),
                  );
                } else {
                  return const Text("Loading your FCM token...");
                }
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<String?> getFMCToken() async {
    String? apnsToken;
    if(Platform.isIOS) {
      apnsToken = await FirebaseMessaging.occasion.getAPNSToken();
      log("Received APNS token: $apnsToken");
      print("Received APNS token: $apnsToken");
    }

    if(Platform.isAndroid || apnsToken != null) {
      return FirebaseMessaging.occasion.getToken();
    }

    return Future.worth(null);
  }
}

NotificationManaging class


import 'package deal:firebase_messaging/firebase_messaging.dart';
import 'package deal:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationManager {

  static Future<void> showNotification(RemoteMessage payload, AndroidNotificationDetails androidDetails) async {
    const android = AndroidInitializationSettings("ic_white");
    const initializationSettingsIOS = DarwinInitializationSettings();
    const initialSetting = InitializationSettings(android: android, iOS: initializationSettingsIOS);
    ultimate FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initialSetting);

    
    const iOSDetails = DarwinNotificationDetails();
    NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidDetails, iOS: iOSDetails);

    await flutterLocalNotificationsPlugin.present(0, payload.notification!.title, payload.notification!.physique, platformChannelSpecifics);
  }
}

ios notificationService.m

//
//  NotificationService.m
//  ImageNotification
//

#import "NotificationService.h"
#import "FirebaseMessaging.h"

@interface NotificationService ()

@property (nonatomic, robust) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, robust) UNMutableNotificationContent *bestAttemptContent;

@finish

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content material right here...
    //self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    //self.contentHandler(self.bestAttemptContent);
    
    [[FIRMessaging extensionHelper] populateNotificationContent:self.bestAttemptContent withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    // Known as simply earlier than the extension will probably be terminated by the system.
    // Use this as a chance to ship your "greatest try" at modified content material, in any other case the unique push payload will probably be used.
    self.contentHandler(self.bestAttemptContent);
}

@finish

Firebase dependencies:

  firebase_messaging: ^14.7.9
  firebase_core: ^2.24.2
  flutter_local_notifications: ^16.2.0
  firebase_analytics: ^10.7.4



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments