gpt4 book ai didi

android - 应用程序被杀死时没有触发 FCM 通知

转载 作者:行者123 更新时间:2023-12-04 23:48:45 25 4
gpt4 key购买 nike

当应用程序处于后台或前台时,FCM 通知对我有效,但在应用程序被终止时无效。
这是我的 FCM 配置代码:

 Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
class CategoriesScreen extends StatefulWidget {
static const routeName = '/view-cateogries';
_CategoriesScreenState createState() => _CategoriesScreenState();
}

class _CategoriesScreenState extends State<CategoriesScreen> {
Future _screenFuture;
void initState() {
_saveDeviceToken(FirebaseMessaging.instance);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
super.initState();
}
我已经在网上和 stackoverflow 上阅读了许多文章。例如,其中一项建议是禁用电池保护程序。我试过了,但没有运气。有什么我想念的想法吗?
我正在使用 firebase-messaging 版本 ^10.0.2

最佳答案

看起来您正在调用 FirebaseMessaging.onBackgroundMessage()内部方法initState()如文档中所述,不允许使用有状态小部件:

Since the handler runs in its own isolate outside your applicationscontext, it is not possible to update application state or execute anyUI impacting logic. You can however perform logic such as HTTPrequests, IO operations (updating local storage), communicate withother plugins etc.


您应该在 runApp() 之前设置后台消息处理程序:
/// Define a top-level named handler which background/terminated messages will
/// call.
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();
print("Handling a background message: ${message.messageId}");
}

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

// Initialize Firebase App
await Firebase.initializeApp();

// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

// Run your app
runApp(HomeScreen());
}
查看 this详情。

关于android - 应用程序被杀死时没有触发 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67860332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com