Я работаю над проектом Flutter, где я установил уведомления с использованием сообщений Firebase. До недавнего времени все работало отлично. После возвращения в проект после долгого времени и снова запустив код, приложение снимается с ANR (приложение не отвечает), когда получено уведомление. CHASH.ANR in com.company
PID: 24618
Reason: Broadcast of Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x11000010 pkg=com.company cmp=com.company/uk.orth.push.FirebaseMessagingReceiver (has extras) }
ErrorId: f29624e4-05d6-4b28-960f-81a2550b55eb
Frozen: false
Load: 3.89 / 3.9 / 3.99
< /code>
Другая память и статистика процессора следуют, которые, кажется, указывают на системное давление, но я больше не делаю ничего интенсивного в обработчике. Тем не менее, сбой продолжается. < /P>
Теперь я застрял и не уверен, что делать дальше.import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
// This is the background message handler. This should be run on a background isolate.
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
// Perform background processing here, but ensure no UI updates happen here.
// You can log data, fetch data, or update your database.
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Register the background message handler
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
@override
void initState() {
super.initState();
// Request notification permissions for iOS
_firebaseMessaging.requestPermission();
// Handle foreground messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("Received message while the app is in the foreground: ${message.messageId}");
// Handle foreground notification here (e.g., show a dialog, update UI)
if (message.notification != null) {
print("Notification title: ${message.notification!.title}");
print("Notification body: ${message.notification!.body}");
}
});
// Handle message when the app is resumed from background (user taps on notification)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("Message clicked! Opening app...");
// Perform navigation or specific actions on notification tap
});
// Get the FCM token (to register the device for notifications)
_firebaseMessaging.getToken().then((String? token) {
print("FCM Token: $token");
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter FCM Demo',
home: Scaffold(
appBar: AppBar(
title: Text("Flutter FCM Demo"),
),
body: Center(child: Text("Push Notification Example")),
),
);
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... -even-with
Обмен сообщениями Firebase вызывает ANR при получении уведомлений в приложении Flutter (даже с минимальным обработчиком) ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение