断言失败:第 310 行 pos 10:'data != null':在 onResume:回调颤振中处理 FCM 消息时出错



我有一个方法,它在3个firebase回调中被调用,它处理消息,并根据message['data]是否有自定义参数显示两个不同的底页,以显示消息并执行一些逻辑。。

当应用程序在前台接收消息时,一切都按预期进行,而当我在后台接收消息时点击横幅时,会出现Failed assertion: line 310 pos 10: 'data != null': A non null String must be provided to a Text Widget.错误。

为什么?传递给onLaunch:onResume:回调的消息不相同?

非常感谢。

消息处理程序方法:

void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
title: notification['title'],
body: notification['body'])
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
title: notification['title'],
body: notification['body']),
);
}

}
}

firebase消息配置:

_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
handleMessage(message);
},
//      onBackgroundMessage: myBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
handleMessage(message);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
handleMessage(message);
},
);
``

好的,我发现,当应用程序在后台,用户点击横幅时,会调用onResume:回调,但在这种情况下,message映射没有notification参数。。为什么??我一点道理都没有。。但是嘿。。我不在谷歌工作,所以…

所以我所做的是在POST请求中将titlebody也添加到'data':中,并创建了第二个方法,该方法从'data':titlebody获取标题和正文,以便从onResume:onLaunch:调用。

但话说回来。。我还更改了第一个方法,从'data':读取titlebody,这样我就可以使用一个始终有效的方法。。

现在我还在徘徊,使用'notification':有什么意义?

void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
//                title: notification['title'],
//                body: notification['body']),
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
//                title: notification['title'],
//                body: notification['body']),
title: data['title'],
body: data['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
final dynamic data = message['data'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
//                  title: notification['title'],
//                  body: notification['body'])
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
//                title: notification['title'],
//                body: notification['body']),
title: data['title'],
body: data['body']),
);
}

}
}

相关内容

  • 没有找到相关文章

最新更新