提问人:sanjay c 提问时间:8/16/2023 最后编辑:sanjay c 更新时间:8/16/2023 访问量:304
当 flutter android app 处于终止状态时,点击通知调用哪个函数,后台通知点击 FCM
Which function is invoked on click of notification while flutter android app is in terminated state, background notification click FCM
问:
我正在使用 flutter_local_notifications:^15.1.0+1 使用 FCM 显示通知。 我能够处理前台和后台点击的通知点击,但是我找不到在应用程序处于终止状态时在通知点击时调用的函数。 欢迎任何帮助。
onDidReceiveNotificationResponse
当应用处于后台或前台时被调用,
对local_notification包使用自定义通知,
以下是我的FCM内容
{ "message":{
"token":"token_1",
"data":{
"title":"FCM Message",
"body":"This is an FCM notification message!",
}
}
答:
flutter_local_notifications
将处理通知中的传入消息。因为无论设置了什么通知通道,都会阻止显示任何 FCM 通知。foreground
Firebase Android SDK
详情请阅读此处:https://firebase.flutter.dev/docs/messaging/notifications#application-in-foreground
在状态下,无需使用本地通知。如果仍使用 To Handle 状态,则会同时收到 2 条通知。teminated
flutter_local_notification
background
要处理收到的消息,请阅读以下文档:https://firebase.google.com/docs/cloud-messaging/flutter/receive
- invoke 方法 每次 revice 通知
你可以在这里调用你的方法,如果通知处于终止状态,它将自动调用
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
/// call you method here <<<<<<<<<<<<<<<<<<<<<<
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
但是,如果您需要交互,例如,当用户单击处于“已终止”状态的通知时,您希望在特定屏幕中导航,然后阅读以下内容:
- 互动
默认情况下,当我们单击通知时,它将触发打开应用程序。
根据我的项目,我在导航到主屏幕之前使用调用我的方法。splash screen
splash_screen.dart
class SplashScreen extends StatefulWidget {
const SplashScreen({Key? key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
setupInteractedMessage();
}
Future<void> setupInteractedMessage() async {
// Get any messages which caused the application to open from
// a terminated state.
RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
// If the message also contains a data property with a "type" of "chat",
// navigate to a chat screen
if (initialMessage != null) {
_handleMessage(initialMessage);
}
// Also handle any interaction when the app is in the background via a
// Stream listener
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
void _handleMessage(RemoteMessage message) {
/// Navigate to detail
String notifType = message.data['type'] ?? '';
if (notifType == "chatiii"){
// navigate to specific screen
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => const ChatScreen()),
(Route<dynamic> route) => true);
}
}
...
}
评论