【发布时间】:2021-03-08 01:48:05
【问题描述】:
我想将传入的 firebase 消息存储在本地数据库中,并且它对 onMessage 、 onLaunch 和 onResume 工作正常,但是当我尝试调试时 onBackgroundMessage 根本没有被触发但是当我的应用程序处于后台时我能够在通知托盘中获取通知
推送通知
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
if (message.containsKey('data')) {
print({message});
PushNotificationService().storeInDatabase(message);
final notifications = await PushNotificationService().getNotifications();
final reversedNotifications = notifications.reversed.toList();
homeScreenBloc.activityController.add(reversedNotifications);
}
}
class PushNotificationService {
BuildContext context;
final FirebaseMessaging _fcm = FirebaseMessaging();
Future initialise() async {
// print('notifcations length = ${notifications.length}');
// context = ctx;
final notifications = await getNotifications();
if (notifications != null) {
homeScreenBloc.activityController.add(notifications);
}
if (Platform.isIOS) {
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print({message});
storeInDatabase(message);
final notifications = await getNotifications();
final reversedNotifications = notifications.reversed.toList();
homeScreenBloc.activityController.add(reversedNotifications);
}
/// called when the notification is clicked while the app is in running in foreground
/// notification fields sends title and body but for other two cases its blank
/// so need to send in data field
,
// called when the notification is clicked while the app is closed(not running)
onLaunch: (Map<String, dynamic> message) async {
print({message});
storeInDatabase(message);
final notifications = await getNotifications();
final reversedNotifications = notifications.reversed.toList();
homeScreenBloc.activityController.add(reversedNotifications);
},
// called when the notification is clicked while the app is running in the background
onResume: (Map<String, dynamic> message) async {
print({message});
storeInDatabase(message);
final notifications = await getNotifications();
final reversedNotifications = notifications.reversed.toList();
homeScreenBloc.activityController.add(reversedNotifications);
},
// called while the app is running in the background
onBackgroundMessage: Platform.isIOS ? null : backgroundMessageHandler,
// }
);
}
AndroidManifest.xml
<application android:usesCleartextTraffic="true" android:name=".Application" ....
Application.kt
package com.altorumleren.alfinityiot;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
build.gradle
dependencies {
implementation 'com.google.firebase:firebase-messaging:<21.0.0>'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation "com.google.firebase:firebase-messaging:20.2.4"
}
我运行应用程序时的日志
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
lib\main.dart:1
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 26): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 34): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 53): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 64): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 89): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 89): Function declaration must have a name
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (17, 51): Expecting an element
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (10, 3): This annotation is not applicable to target 'expression'
【问题讨论】:
-
您找到解决方案了吗?
标签: android firebase flutter push-notification