Skip to content

Instantly share code, notes, and snippets.

@SebastianEngel
Created August 5, 2019 09:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SebastianEngel/0769b50cb2ba6962248341f1fbd508f4 to your computer and use it in GitHub Desktop.
Save SebastianEngel/0769b50cb2ba6962248341f1fbd508f4 to your computer and use it in GitHub Desktop.
import 'package:firebase_messaging/firebase_messaging.dart';
class PushNotificationsManager {
PushNotificationsManager._();
factory PushNotificationsManager() => _instance;
static final PushNotificationsManager _instance = PushNotificationsManager._();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
bool _initialized = false;
Future<void> init() async {
if (!_initialized) {
// For iOS request permission first.
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.configure();
// For testing purposes print the Firebase Messaging token
String token = await _firebaseMessaging.getToken();
print("FirebaseMessaging token: $token");
_initialized = true;
}
}
}
@SebastianEngel
Copy link
Author

In order to request the notification permissions on iOS on app start, I would call init() in the first screen of the application.

@moreirapontocom
Copy link

@SebastianEngel Could you share an example how to call the init() method? Thank you

@raLaaaa
Copy link

raLaaaa commented Jul 1, 2020

@moreirapontocom

Future main() async {
  PushNotificationsManager().init(); <--

That's the way I do it.

@meroo36
Copy link

meroo36 commented Jul 17, 2020

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  PushNotificationsManager().init();
  runApp(MyApp());
}

@praveenb
Copy link

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.configure();

These method calls not exist in latest flutter_message version.

firebase_messaging: ^10.0.5

@monta42
Copy link

monta42 commented Jan 3, 2024

Some replacements are needed:

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

and

      _firebaseMessaging.requestPermission();
      // _firebaseMessaging.configure(); -> configure() has been removed

See: https://firebase.flutter.dev/docs/migration/#messaging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment