you*_*low 6 push-notification firebase flutter firebase-cloud-messaging
我假设我遵循了在 flutter 中处理来自 firebase 的后台通知的所有步骤。我创建了一个顶级函数,希望在收到通知时触发该函数。但是,该函数永远不会被触发。
这是我的主页小部件中存在但在类之外的顶级后台处理程序函数:
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// ignore: avoid_print
print('A background message just showed up : ${message.messageId}');
// update SQLite
var result = await PageService.instance
.add(PageService.instance.convertToPage(message.data));
print('added to db: ${result}');
}
Run Code Online (Sandbox Code Playgroud)
这是我的主页初始化状态,它调用一个函数来初始化 firebase 消息:
@override
void initState() {
super.initState();
_initializeFirebaseMessaging();
}
Run Code Online (Sandbox Code Playgroud)
这里也是在主页类中定义的 _initializeFirebaseMessaging 函数:
void _initializeFirebaseMessaging() {
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('new notification arrived');
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
// update SQLite
var result = await PageService.instance
.add(PageService.instance.convertToPage(message.data));
print('added to db: ${result}');
if (notification != null && android != null) {
// show notification
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
color: Colors.blue,
playSound: true,
icon: '@mipmap/ic_launcher',
),
));
}
});
}
Run Code Online (Sandbox Code Playgroud)
当我在应用程序中收到通知并处理它们时, onmessage.listen 工作正常,但后台处理程序根本没有触发。
我将不胜感激任何帮助!
| 归档时间: |
|
| 查看次数: |
4184 次 |
| 最近记录: |