【发布时间】:2019-05-24 18:39:57
【问题描述】:
我使用了 flutter_local_notifications,推送通知成功接收,当我点击它时,我试图重定向到特定屏幕,但它总是重定向到主屏幕。
下面是我的代码。
@override
void initState() {
// TODO: implement initState
super.initState();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
var data = message['data'] as Map;
var msg1 = data['message'] as String;
var response = json.decode(msg1) as Map;
String str_title = response['title'] as String;
String str_body = response['body'] as String;
_showNotification(str_title, str_body, msg1);
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
}
_showNotification 功能代码
void _showNotification(String title, String body, String pay_load) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
AppConstant.notify_channel_id,
AppConstant.notify_channel_name,
AppConstant.notify_channel_desc,
importance: Importance.Max,
priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: pay_load);
}
onSelectNotification 功能代码
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
// here set and put condition for property id
var response = json.decode(payload) as Map;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PropertyDetailScreen()),
);
PropertyDetailScreen 是我点击通知时要重定向的屏幕,但它总是重定向到主屏幕,所以请指导我哪里错了,或者为什么我的代码不起作用。
【问题讨论】:
-
您的代码在 Android 和 iOS 上都适用于我,我尝试通过预定通知来使用它,它会在点击时打开第二页。此外,您在
Navigator.push之前错过了await,如插件文档中所示(但无论如何它对我来说都有效)所以也许它还有其他一些奇怪的怪癖。你的有效载荷是空的吗?它可能无法解码为 json 吗? -
@ciprianoss,我应该使用scheduled notifications吗?,来实现,并且payload不为null。
-
不,我只是用预定的通知进行了测试,所以我不必设置一个 firebase 项目来测试,你的 json 解析是否在 onSelectNotification 中工作?
-
@ciprianoss,是的,它工作正常,我已经调试过了,导航器推送线也被执行了,但没有工作。
-
找到解决方案了吗?
标签: flutter