This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function sendNotification(token) { | |
| try { | |
| // Get custom token | |
| const tokenResponse = await fetch('http://localhost:3000/api/send-notification', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ token }) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const { sendNotification } = require('./firebase_admin'); | |
| const app = express(); | |
| app.use(express.json()); | |
| app.post('/api/send-notification', async (req, res) => { | |
| try { | |
| const { token } = req.body; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const admin = require('firebase-admin'); | |
| // Initialize Firebase Admin SDK | |
| const serviceAccount = require('<your admin SDK path>'); | |
| admin.initializeApp({ | |
| credential: admin.credential.cert(serviceAccount) | |
| }); | |
| async function sendNotification(token) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Typically you should do this in a folder to keep it well structure | |
| npm init -y | |
| # Do this after you have done the initial | |
| # This is to get the packages that will make up our backend | |
| npm install express firebase firebase-admin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<void> main() async { | |
| WidgetsFlutterBinding.ensureInitialized(); | |
| await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); | |
| NotificationService.initialize(); | |
| runApp( | |
| const MaterialApp( | |
| home: Home(), | |
| ), | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Background message handler for Firebase Cloud Messaging (FCM) | |
| // This function is triggered when a notification is received while the app is in the background or terminated. | |
| @pragma('vm:entry-point') | |
| Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { | |
| // Ensure Firebase is initialized before processing the message | |
| await Firebase.initializeApp(); | |
| await NotificationService.setupFlutterNotifications(); | |
| // Display the notification | |
| NotificationService.showFlutterNotification(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Do this first | |
| firbase login | |
| ## Do this and follow the prompt, it's really easy | |
| flutterfire configure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import Flutter | |
| import flutter_local_notifications | |
| @main | |
| @objc class AppDelegate: FlutterAppDelegate { | |
| override func application( | |
| _ application: UIApplication, | |
| didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
| ) -> Bool { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compileOptions { | |
| coreLibraryDesugaringEnabled true | |
| sourceCompatibility JavaVersion.VERSION_1_8 | |
| targetCompatibility JavaVersion.VERSION_1_8 | |
| } | |
| ..... | |
| dependencies { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <uses-permission android:name="android.permission.VIBRATE" /> | |
| <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> | |
| <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> | |
| <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> | |
| <application | |
| android:label="notification_test" |
NewerOlder