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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With