android - java.lang.IllegalArgumentException:服务未注册:ls@1ff721a
问题描述
我正在使用firebase_messaging 7.0.3和flutter_local_notifications 4.0.0在 android 中发送通知,一切正常,我还使用 SQLite 保存通知,它仍然可以正常工作,除非后台模式我可以看到通知,但看起来像函数我为保存未调用的通知而创建的。
代码句柄通知
_firebaseMessaging.configure(
onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
onMessage: (Map<String, dynamic> message) async {
MessageNotification notification = MessageNotification(
id: null,
title: message['notification']['title'],
body: message['notification']['body'],
status: 'U',
createdDate: formatted);
await dbHelper.save(notification);
showNotification(
message['notification']['title'], message['notification']['body']);
messages = await dbHelper.getMessages();
if (messages.isEmpty) {
messages = [];
}
update();
print("onLaunch: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
showNotification(
message['notification']['title'], message['notification']['body']);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
showNotification(
message['notification']['title'], message['notification']['body']);
messages = await dbHelper.getMessages();
update();
},
);
}
代码句柄后台保存
Database db;
const String ID = 'id';
const String TITLE = 'title';
const String BODY = 'body';
const String STATUS = 'status';
const String CREATEDDATE = 'createdDate';
const String TABLE = 'Message';
const String DB_NAME = 'message2.db';
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
print('on background running');
MessageNotification notification = MessageNotification(
id: null,
title: "Hello",
body: "hello",
status: 'U',
createdDate: "2020-10-2020");
await db.insert(TABLE, notification.toMap());
return Future<void>.value();
// Or do other work.
}
应用程序.java
public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"));
SqflitePlugin.registerWith(registry.registrarFor("com.tekartik.sqflite.SqflitePlugin"));
}
}
错误日志
W/ConnectionTracker(25834): Exception thrown while unbinding
W/ConnectionTracker(25834): java.lang.IllegalArgumentException: Service not registered:
ls@1ff721a
W/ConnectionTracker(25834): at
android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1757)
W/ConnectionTracker(25834): at
android.app.ContextImpl.unbindService(ContextImpl.java:1874)
W/ConnectionTracker(25834): at
android.content.ContextWrapper.unbindService(ContextWrapper.java:792)
W/ConnectionTracker(25834): at
ci.f(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):1)
W/ConnectionTracker(25834): at
ci.d(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):2)
W/ConnectionTracker(25834): at
lt.E(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):9)
W/ConnectionTracker(25834): at
ld.a(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):3)
W/ConnectionTracker(25834): at
ef.run(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):3)
W/ConnectionTracker(25834): at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(25834): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(25834): at
iy.run(:com.google.android.gms.dynamite_measurementdynamite@205016100@20.50.16 (150700-0):5)