是否可以在 flutter_local_notification 插件的 onSelectNotification 上传递参数
Is it possible to pass parameter on onSelectNotification for flutter_local_notification plugin
抱歉,我是 Flutter 的新手,目前在为我的应用实现通知时遇到了困难。
我正在使用 flutter_local_notification
插件,因为我听说它可以用于提醒目的,也可以用于离线应用程序。
我目前面临将笔记(模型)object 传递给我的 onSelectNotification
函数的问题
我的Objective:为我的笔记应用程序创建一个提醒图标,这样当通知被flutter_local_notification插件触发时。点击通知将允许我继续我的 EditNotePage
activity,上面会出现相应的注释 object 参数(标题、描述)
如何修改 onSelectNotification
以便我可以将笔记 object 传递给它。
非常感谢所有帮助!
抱歉没有提供太多代码。
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);
您可以将 Note
对象编码为 JSON 字符串并将其传递到方法中以设置您的提醒,如下所示:
说这是你的 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,
};
}
现在,要设置通知,您可以从模型中创建 JSON 字符串并将其作为 payload
传递给 flutterLocalNotificationsPlugin
方法,如下所示:
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);
接下来在 onSelectNotification
方法中获取 payload
字符串并使用 fromJsonString
构造函数(它解码 json 字符串并创建 Note
对象)得到 Note
对象:
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
}
抱歉,我是 Flutter 的新手,目前在为我的应用实现通知时遇到了困难。
我正在使用 flutter_local_notification
插件,因为我听说它可以用于提醒目的,也可以用于离线应用程序。
我目前面临将笔记(模型)object 传递给我的 onSelectNotification
函数的问题
我的Objective:为我的笔记应用程序创建一个提醒图标,这样当通知被flutter_local_notification插件触发时。点击通知将允许我继续我的 EditNotePage
activity,上面会出现相应的注释 object 参数(标题、描述)
如何修改 onSelectNotification
以便我可以将笔记 object 传递给它。
非常感谢所有帮助!
抱歉没有提供太多代码。
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);
您可以将 Note
对象编码为 JSON 字符串并将其传递到方法中以设置您的提醒,如下所示:
说这是你的 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,
};
}
现在,要设置通知,您可以从模型中创建 JSON 字符串并将其作为 payload
传递给 flutterLocalNotificationsPlugin
方法,如下所示:
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);
接下来在 onSelectNotification
方法中获取 payload
字符串并使用 fromJsonString
构造函数(它解码 json 字符串并创建 Note
对象)得到 Note
对象:
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
}