Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to enable or disable the notification flutter

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

like image 571
Mohammed Achref Machat Avatar asked Oct 15 '22 03:10

Mohammed Achref Machat


1 Answers

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

like image 154
edwinmacalopú Avatar answered Nov 04 '22 19:11

edwinmacalopú