Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
}
@JFernandoGomez

This comment has been minimized.

Copy link

@JFernandoGomez JFernandoGomez commented Mar 29, 2020

@SebastianEngel where do you initialize this class? on the main.dart or on any other view ?

@SebastianEngel

This comment has been minimized.

Copy link
Owner Author

@SebastianEngel SebastianEngel commented Mar 30, 2020

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

@moreirapontocom

This comment has been minimized.

Copy link

@moreirapontocom moreirapontocom commented Jun 7, 2020

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

@raLaaaa

This comment has been minimized.

Copy link

@raLaaaa raLaaaa commented Jul 1, 2020

@moreirapontocom

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

That's the way I do it.

@meroo36

This comment has been minimized.

Copy link

@meroo36 meroo36 commented Jul 17, 2020

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  PushNotificationsManager().init();
  runApp(MyApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment