Created
April 24, 2020 13:44
-
-
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
This file contains 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
//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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is it work for background notification?