Skip to content

Instantly share code, notes, and snippets.

@flakerimi
Created February 6, 2024 12:49
Show Gist options
  • Save flakerimi/0246e9ff6f174c9c634ca567a93ab451 to your computer and use it in GitHub Desktop.
Save flakerimi/0246e9ff6f174c9c634ca567a93ab451 to your computer and use it in GitHub Desktop.
FlutterFirebase push
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:push/firebase_options.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print('Handling a background message ${message.messageId}');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
//initialize firebase
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
print('Message data: ${message.data}');
});
runApp(const Push());
}
class Push extends StatelessWidget {
const Push({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Push Notifications',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
@override
void initState() {
super.initState();
_firebaseMessaging.getToken().then((String? token) {
assert(token != null);
print('Push Messaging token: $token');
});
_firebaseMessaging.subscribeToTopic('all');
_firebaseMessaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
_firebaseMessaging.getNotificationSettings().then((settings) {
print('User granted permission: ${settings.authorizationStatus}');
});
_firebaseMessaging.onTokenRefresh.listen((String? token) {
print('Token refreshed: $token');
});
_firebaseMessaging.getAPNSToken().then((String? token) {
print('APNs token: $token');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Push Notifications'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
_firebaseMessaging.subscribeToTopic('all');
},
child: const Text('Subscribe to all'),
),
ElevatedButton(
onPressed: () {
_firebaseMessaging.unsubscribeFromTopic('all');
},
child: const Text('Unsubscribe from all'),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment