Last active
March 1, 2025 02:56
-
-
Save Slowhand0309/7565ec83496f4b9b6d4c51068a007f1a to your computer and use it in GitHub Desktop.
[Flutter firebase_messaging note] #Flutter
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
// root build.gradle | |
dependencies { | |
// Add the google services classpath | |
classpath 'com.google.gms:google-services:4.3.2' | |
} | |
// app/build.gradle | |
// ADD THIS AT THE BOTTOM | |
apply plugin: 'com.google.gms.google-services' | |
// AndroidManifest.xml | |
<intent-filter> | |
<action android:name="FLUTTER_NOTIFICATION_CLICK" /> | |
<category android:name="android.intent.category.DEFAULT" /> | |
</intent-filter> |
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
// AppDelegete.swift | |
@UIApplicationMain | |
@objc class AppDelegate: FlutterAppDelegate { | |
override func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
GeneratedPluginRegistrant.register(with: self) | |
// Add | |
if #available(iOS 10.0, *) { | |
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate | |
} | |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
} |
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 'package:firebase_messaging/firebase_messaging.dart'; | |
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); | |
// request permission.(ios only) | |
_firebaseMessaging.requestNotificationPermissions( | |
const IosNotificationSettings( | |
sound: true, badge: true, alert: true, provisional: true)); | |
_firebaseMessaging.onIosSettingsRegistered | |
.listen((IosNotificationSettings settings) { | |
print("Settings registered: $settings"); | |
}); | |
// subscribe topic. | |
await _firebaseMessaging.subscribeToTopic('xxxxxxxxx'); | |
// unsubscribe topic. | |
_firebaseMessaging.unsubscribeFromTopic('xxxxxxxxx'); | |
// receiving messages. | |
// https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/README.md#receiving-messages | |
_firebaseMessaging.configure( | |
onMessage: (Map<String, dynamic> message) async { | |
}, | |
onLaunch: (Map<String, dynamic> message) async { | |
}, | |
onResume: (Map<String, dynamic> message) async { | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment