Is it possible to pass parameter on onSelectNotification for flutter_local_notification plugin
You can encode your Note
object to a JSON string and pass that into the method to set your reminder like below:
Say this is your Note
class:
import 'package:meta/meta.dart';
import 'dart:convert';
class Note {
final String title;
final String description;
Note({@required this.title, @required this.description});
//Add these methods below
factory Note.fromJsonString(String str) => Note._fromJson(jsonDecode(str));
String toJsonString() => jsonEncode(_toJson());
factory Note._fromJson(Map<String, dynamic> json) => Note(
title: json['title'],
description: json['description'],
);
Map<String, dynamic> _toJson() => {
'title': title,
'description': description,
};
}
Now, to set your notification, you can create the JSON string from your model and pass it as the payload
to the flutterLocalNotificationsPlugin
method like below:
Note newNote = Note(title : 'Hello', description : 'This is my first reminder');
String noteJsonString = newNote.toJsonString();
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: noteJsonString);
Next you get the payload
string in your onSelectNotification
method and use the fromJsonString
constructor (which decodes the json string and creates a Note
object) to get the Note
object:
Future onSelectNotification(String payload) async {
Note note = Note.fromJsonString(payload);
//You can then use your Note object however you want.
//e.g
print(note.title); // Hello
print(note.description); // This is my first reminder
}
Wei Jun
Updated on December 17, 2022Comments
-
Wei Jun over 1 year
Sorry, I am new to flutter and currently facing difficulties when implementing notification for my app. I am using
flutter_local_notification
plugin, as I heard it is useful for reminder purpose, as well as using it for an offline app.I'm currently facing a problem of passing my Note (model) object to my
onSelectNotification
functionMy Objective: create a reminder icon for my Note app, such that when the notification is triggered by the flutter_local_notification plugin. Tapping the notification will allow me to proceed to my
EditNotePage
activity, with the corresponding Note object parameters (title, description) appears on itHow can I modify the
onSelectNotification
such that I can pass my Note object to it.All help is deeply appreciated!
Sorry for not providing much code.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin(); var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon'); var initializationSettingsIOS = IOSInitializationSettings( onDidReceiveLocalNotification: onDidReceiveLocalNotification); var initializationSettings = InitializationSettings( initializationSettingsAndroid, initializationSettingsIOS); flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
-
Wei Jun about 4 yearsHi Victor, thanks for the suggestion! I have just tested it out and I am able to set the reminder without any error! I would have never thought of encoding it to a String object. Thanks!