to enable or disable the notification flutter

1,926

what you should do is that when implementing the notifications subscribe the user to a topic with

FirebaseMessaging.instance.subscribeToTopic ('news');

If this user wants to deactivate notifications, you only unsubscribe from the topic with

FirebaseMessaging.instance.unsubscribeFromTopic ('news');

Obviously when you send the notifications from the backend or console, you must specify this topic "news" or the name that you decide to put it

Share:
1,926
Mohammed Achref Machat
Author by

Mohammed Achref Machat

Updated on December 18, 2022

Comments

  • Mohammed Achref Machat
    Mohammed Achref Machat over 1 year

    I want to enable/disable the notification service with help of a button when this button is enabled, the user receives the notification, and when it's disabled he won't receive any notifications.

    import 'dart:io';
    
    import 'package:firebase_messaging/firebase_messaging.dart';
    
    class FirebaseNotifications {
      FirebaseMessaging _firebaseMessaging;
    
      void setUpFirebase() {
        _firebaseMessaging = FirebaseMessaging();
        firebaseCloudMessaging_Listeners();
    
      }
    
      void firebaseCloudMessaging_Listeners() {
        if (Platform.isIOS) iOS_Permission();
    
        _firebaseMessaging.getToken().then((token) {
          print(token);
        });
    
        _firebaseMessaging.configure(
          onMessage: (Map<String, dynamic> message) async {
            print('on message $message');
          },
          onResume: (Map<String, dynamic> message) async {
            print('on resume $message');
          },
          onLaunch: (Map<String, dynamic> message) async {
            print('on launch $message');
          },
        );
      }
    
      void iOS_Permission() {
        _firebaseMessaging.requestNotificationPermissions(
            IosNotificationSettings(sound: true, badge: true, alert: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });
      }
    }
    

    enter image description here