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
function
My 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 it
How 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);
Click the "+New Push" button in your OneSignal dashboard. You will be redirected to a new window that will allow you to customize your push notification. Under Audience, make sure that Send to Subscribed Users is selected. Then, create your message by adding your message title, content, and image.
A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
Setting up the Notification Service Open the file and import the following at the top: import 'package:flutter_local_notifications/flutter_local_notifications. dart'; import 'package:timezone/timezone. dart' as tz; import 'package:timezone/data/latest.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With