Skip to content

Instantly share code, notes, and snippets.

@Slowhand0309
Last active October 7, 2020 05:01
Show Gist options
  • Save Slowhand0309/7565ec83496f4b9b6d4c51068a007f1a to your computer and use it in GitHub Desktop.
Save Slowhand0309/7565ec83496f4b9b6d4c51068a007f1a to your computer and use it in GitHub Desktop.
[Flutter firebase_messaging note] #Flutter
// 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>
// 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)
}
}
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