1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] flutter - FCM notification appears twice when app is in background

Discussão em 'Mobile' iniciado por Stack, Outubro 4, 2024 às 07:22.

  1. Stack

    Stack Membro Participativo

    I am working on firebase messaging for handling background message with FlutterLocalNotificationsPlugin but I received two notification when I do this. I received 1 notification is from FlutterLocalNotificationsPlugin and other one is from system notification. I need to use FlutterLocalNotificationsPlugin notification because I need to do some customization, how do I disable system default notification?

    void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    await init();
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    await setupFlutterNotifications();
    runApp(const MyApp());
    }

    @pragma('vm:entry-point')
    Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    await Firebase.initializeApp();
    await setupFlutterNotifications();
    print('Application background message: $message');
    final title = message.notification!.title;
    final body = message.notification!.body;
    await notificationService.notification(message);
    }


    Future<void> notification(RemoteMessage message) async {
    RemoteNotification? notification = message.notification!;
    flutterLocalNotificationsPlugin.show(
    notification.hashCode,
    notification.title,
    notification.body,
    const NotificationDetails(
    android: AndroidNotificationDetails(
    'channel id',
    'your channel name',
    channelDescription: 'your channel description',
    icon: 'launch_background',
    ),
    ),
    );
    }


    [​IMG]

    I am using laravel-notification-channels/fcm for my fcm notification. My solution for my issue is to comment out fcm setNotification function. Refer to my code below are example.

    public function toFcm($notifiable)
    {
    return FcmMessage::create()
    ->setData([
    'title' => json_encode($this->newMessage['title']),
    'body' => json_encode($this->newMessage['body']),
    ])
    // ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
    // ->setTitle($this->newMessage['title'])
    // ->setBody($this->newMessage['body'])
    // // ->setImage($this->newMessage['user']['image'])
    // )
    ->setAndroid(
    AndroidConfig::create()
    ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
    ->setPriority(AndroidMessagePriority::HIGH())
    // ->setNotification(AndroidNotification::create()->setChannelId('high_importance_channel'))
    )->setApns(
    ApnsConfig::create()
    ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
    );
    }

    Continue reading...

Compartilhe esta Página