【发布时间】:2021-06-04 22:56:35
【问题描述】:
我正在构建一个使用 FCM 接收推送通知的应用。
我想在点击通知时路由到特定屏幕(例如,用户的个人资料)。
在 Android 上,当应用程序刚刚关闭(而不是“终止”)时,它可以正常工作,但当应用程序终止(“终止”)时,它就无法正常工作。 在 iOS 上,它根本不起作用。
我正在实现它:
NotificationsHandler:
class NotificationsHandler {
static final NotificationsHandler instance = NotificationsHandler();
final _fcm = FirebaseMessaging();
void onBackgroundNotificationRecevied({Function onReceived}) {
_fcm.configure(
onResume: (message) => onReceived(message),
onLaunch: (message) => onReceived(message),
);
}
}
myMainScreen 的 initState:
@override
void initState() {
NotificationsHandler.instance.onBackgroundNotificationRecevied(
onReceived: (message) async {
final userId = message['data']['userId'];
final user = this.users.firstWhere((currentUser) => currentUser.id == userId);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserProfileScreen(
user,
),
),
);
}
);
super.initState();
}
发送通知的代码(通过外部 React 管理面板):
const payload = {
notification: {
title: `myTitle`,
body: `My message`,
sound: "default",
badge: "1",
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
userId: myUserId,
},
};
const options = {
priority: 'high',
timeToLive: 60 * 60 * 24
};
admin.messaging().sendToTopic('myTopic', payload, options);
有谁知道为什么它不起作用?
谢谢!
【问题讨论】:
-
你能显示
NotificationHandler类的代码吗? -
@Andrej 我已将其添加到我的问题中
-
能否也显示发送通知的代码?
-
@Andrej 是的,我现在确实将它添加到我的问题中。请注意,通知是通过外部 React 管理面板发送的,而不是通过 Flutter 应用程序
-
在您的 _fcm.configure() 方法中,您是否错过了“onBackgroundMessage”参数? -> onBackgroundMessage: yourBackgroundMessageHandler ,其中“yourBackgroundMessageHandler”应该是您的顶级函数?说到 iOS,您是否创建了 APNS 证书并与您的 GoogleServiceInfo.plist 文件正确链接?
标签: firebase flutter dart firebase-cloud-messaging