Skip to content

Instantly share code, notes, and snippets.

@zaprogrammer
Created April 24, 2020 13:44
Show Gist options
  • Save zaprogrammer/6e324898e4256b4cdfa73a009b120016 to your computer and use it in GitHub Desktop.
Save zaprogrammer/6e324898e4256b4cdfa73a009b120016 to your computer and use it in GitHub Desktop.
[Flutter] Firebase receive message and save it in sqflite db #flutter #dart #sqflite #firebase
//set click_action for notification and user clicks on the notification,
// data send to onLaunch method configure
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
prepareFirebaseCloudMessaging();
super.initState();
}
void prepareFirebaseCloudMessaging() {
if (Platform.isIOS) IOSPermission();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
onResume: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
onLaunch: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
);
}
void notificationDataHandler(Map<String, dynamic> message) async {
try {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'MY_DATABASE_FILE.db');
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE Messages (id INTEGER PRIMARY KEY, message TEXT, date TEXT, seen INTEGER)');
});
Message messageStore = Message(
null, message["data"]["message"], message["data"]["date"], false);
await database.insert("Messages", messageStore.toMap());
database.close();
}catch(e){
debugPrint(e.toString());
}
}
@marcos-bah
Copy link

is it work for background notification?

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