Skip to content

Instantly share code, notes, and snippets.

@yshean
Last active November 5, 2021 14:40
Show Gist options
  • Save yshean/b33cc75512b27d50c95fcabfe4b6aa05 to your computer and use it in GitHub Desktop.
Save yshean/b33cc75512b27d50c95fcabfe4b6aa05 to your computer and use it in GitHub Desktop.
import 'package:firebase_messaging/firebase_messaging.dart';
class PushNotificationService {
final FirebaseMessaging _fcm;
PushNotificationService(this._fcm);
Future initialise() async {
if (Platform.isIOS) {
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
// If you want to test the push notification locally,
// you need to get the token and input to the Firebase console
// https://console.firebase.google.com/project/YOUR_PROJECT_ID/notification/compose
String token = await _fcm.getToken();
print("FirebaseMessaging token: $token");
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}
}
@ok200paul
Copy link

Hey Yong, I'm reading your great tutorial at https://medium.com/comerge/implementing-push-notifications-in-flutter-apps-aef98451e8f1 - but the Platform reference is causing trouble in my IDE. Should this gist use import 'dart:io'; at the top to enable access to this class? Thanks! Paul G - Melbourne

@victkarangwa
Copy link

Yes @ok200paul, you should use import 'dart:io' to resolve the Platform reference issue found in this gist.

@ok200paul
Copy link

Brilliant, thanks so much @victorkarangwa4!

@abdulrojak288
Copy link

If the application is closed, the notification does not appear

@devnullpointer
Copy link

devnullpointer commented Jul 13, 2021

_fcm.requestNotificationPermissions and _fcm.configure are undefined. Using flutter_messaging 10.0.3, also IosNotificationSettings doesn't resolve to any library.

@MatildaZhou
Copy link

https://firebase.flutter.dev/docs/migration/ There is something updated.

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