OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
625 stars 214 forks source link

[Bug]: Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#initialize on channel OneSignal) #776

Closed psatler closed 1 year ago

psatler commented 1 year ago

What happened?

The Flutter OneSignal SDK version 5.0.3 is throwing missing plugin exceptions when running on Android. This is similar (if not equal) to this other issue https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/742#issuecomment-1722180113

The first time you install the app, it works as expected. However, if you stop the app and rerun it again, it starts to show the exceptions.

I created a repo so you can clone and try to reproduce it yourself. Below is also a video showing the problem found.

https://github.com/OneSignal/OneSignal-Flutter-SDK/assets/3421410/d4fd8187-659a-4c18-ada4-b4ad48ac06cc

PS: IOS is working fine so I did not set it up in the aforementioned repository.

Steps to reproduce?

If you clone the [repo](https://github.com/psatler/one_signal_push_noti_flutter_repro) I created, to reproduce you need to 

1. Update the `_appId` variable found in `lib/main.dart` with your own App ID.
2. Run the app on an Android device/emulator.
   2.1. On the first run it will open the app and one signal will run as expected
3. Stop the app and run it again
   3.1. On the second run it displays the initialization errors / missing plugin implementations

The app was tested using the following Flutter version and used `onesignal_flutter` version `5.0.3`

flutter --version
Flutter 3.13.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision ead455963c (4 weeks ago) • 2023-09-26 18:28:17 -0700
Engine • revision a794cf2681
Tools • Dart 3.1.3 • DevTools 2.25.0

### What did you expect to happen?

I expect to run the app without issues

### OneSignal Flutter SDK version

Release 5.0.3

### Which platform(s) are affected?

- [ ] iOS
- [X] Android

### Relevant log output

```Shell
❯ flutter run
Using hardware rendering with device sdk gphone64 arm64. If you notice
graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
Running Gradle task 'assembleDebug'...                           1,895ms
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
E/DartMessenger( 6245): Uncaught exception in binary message listener
E/DartMessenger( 6245): java.lang.reflect.InvocationTargetException
E/DartMessenger( 6245):     at java.lang.reflect.Constructor.newInstance0(Native Method)
E/DartMessenger( 6245):     at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:90)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getServiceOrNull(ServiceProvider.kt:79)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getService(ServiceProvider.kt:67)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:83)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getServiceOrNull(ServiceProvider.kt:79)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getService(ServiceProvider.kt:67)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:83)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getServiceOrNull(ServiceProvider.kt:79)
E/DartMessenger( 6245):     at com.onesignal.common.services.ServiceProvider.getService(ServiceProvider.kt:67)
E/DartMessenger( 6245):     at com.onesignal.internal.OneSignalImp.initWithContext(OneSignalImp.kt:419)
E/DartMessenger( 6245):     at com.onesignal.OneSignal.initWithContext(OneSignal.kt:126)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalPlugin.initWithContext(OneSignalPlugin.java:128)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalPlugin.onMethodCall(OneSignalPlugin.java:111)
E/DartMessenger( 6245):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/DartMessenger( 6245):     at android.os.Handler.handleCallback(Handler.java:942)
E/DartMessenger( 6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DartMessenger( 6245):     at android.os.Looper.loopOnce(Looper.java:201)
E/DartMessenger( 6245):     at android.os.Looper.loop(Looper.java:288)
E/DartMessenger( 6245):     at android.app.ActivityThread.main(ActivityThread.java:7872)
E/DartMessenger( 6245):     at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger( 6245):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/DartMessenger( 6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/DartMessenger( 6245): Caused by: org.json.JSONException: Value value at key of type java.lang.String cannot be converted to boolean
E/DartMessenger( 6245):     at org.json.JSON.typeMismatch(JSON.java:101)
E/DartMessenger( 6245):     at org.json.JSONObject.getBoolean(JSONObject.java:422)
E/DartMessenger( 6245):     at com.onesignal.common.modeling.Model.initializeFromJson(Model.kt:107)
E/DartMessenger( 6245):     at com.onesignal.user.internal.properties.PropertiesModel.createModelForProperty(PropertiesModel.kt:94)
E/DartMessenger( 6245):     at com.onesignal.common.modeling.Model.initializeFromJson(Model.kt:87)
E/DartMessenger( 6245):     at com.onesignal.common.modeling.SimpleModelStore.create(SimpleModelStore.kt:32)
E/DartMessenger( 6245):     at com.onesignal.common.modeling.ModelStore.load(ModelStore.kt:129)
E/DartMessenger( 6245):     at com.onesignal.common.modeling.SimpleModelStore.<init>(SimpleModelStore.kt:25)
E/DartMessenger( 6245):     at com.onesignal.user.internal.properties.PropertiesModelStore.<init>(PropertiesModelStore.kt:8)
E/DartMessenger( 6245):     ... 27 more
E/DartMessenger( 6245): Uncaught exception in binary message listener
E/DartMessenger( 6245): java.lang.Exception: Must call 'initWithContext' before use
E/DartMessenger( 6245):     at com.onesignal.internal.OneSignalImp.getInAppMessages(OneSignalImp.kt:83)
E/DartMessenger( 6245):     at com.onesignal.OneSignal.getInAppMessages(OneSignal.kt:78)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalInAppMessages.lifecycleInit(OneSignalInAppMessages.java:90)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalInAppMessages.onMethodCall(OneSignalInAppMessages.java:52)
E/DartMessenger( 6245):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/DartMessenger( 6245):     at android.os.Handler.handleCallback(Handler.java:942)
E/DartMessenger( 6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DartMessenger( 6245):     at android.os.Looper.loopOnce(Looper.java:201)
E/DartMessenger( 6245):     at android.os.Looper.loop(Looper.java:288)
E/DartMessenger( 6245):     at android.app.ActivityThread.main(ActivityThread.java:7872)
E/DartMessenger( 6245):     at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger( 6245):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/DartMessenger( 6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/DartMessenger( 6245): Uncaught exception in binary message listener
E/DartMessenger( 6245): java.lang.Exception: Must call 'initWithContext' before use
E/DartMessenger( 6245):     at com.onesignal.internal.OneSignalImp.getUser(OneSignalImp.kt:84)
E/DartMessenger( 6245):     at com.onesignal.OneSignal.getUser(OneSignal.kt:46)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalPushSubscription.onMethodCall(OneSignalPushSubscription.java:45)
E/DartMessenger( 6245):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/DartMessenger( 6245):     at android.os.Handler.handleCallback(Handler.java:942)
E/DartMessenger( 6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DartMessenger( 6245):     at android.os.Looper.loopOnce(Looper.java:201)
E/DartMessenger( 6245):     at android.os.Looper.loop(Looper.java:288)
E/DartMessenger( 6245):     at android.app.ActivityThread.main(ActivityThread.java:7872)
E/DartMessenger( 6245):     at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger( 6245):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/DartMessenger( 6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/DartMessenger( 6245): Uncaught exception in binary message listener
E/DartMessenger( 6245): java.lang.Exception: Must call 'initWithContext' before use
E/DartMessenger( 6245):     at com.onesignal.internal.OneSignalImp.getNotifications(OneSignalImp.kt:81)
E/DartMessenger( 6245):     at com.onesignal.OneSignal.getNotifications(OneSignal.kt:62)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalNotifications.onMethodCall(OneSignalNotifications.java:54)
E/DartMessenger( 6245):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/DartMessenger( 6245):     at android.os.Handler.handleCallback(Handler.java:942)
E/DartMessenger( 6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DartMessenger( 6245):     at android.os.Looper.loopOnce(Looper.java:201)
E/DartMessenger( 6245):     at android.os.Looper.loop(Looper.java:288)
E/DartMessenger( 6245):     at android.app.ActivityThread.main(ActivityThread.java:7872)
E/DartMessenger( 6245):     at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger( 6245):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/DartMessenger( 6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/DartMessenger( 6245): Uncaught exception in binary message listener
E/DartMessenger( 6245): java.lang.Exception: Must call 'initWithContext' before use
E/DartMessenger( 6245):     at com.onesignal.internal.OneSignalImp.getUser(OneSignalImp.kt:84)
E/DartMessenger( 6245):     at com.onesignal.OneSignal.getUser(OneSignal.kt:46)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalUser.addTags(OneSignalUser.java:106)
E/DartMessenger( 6245):     at com.onesignal.flutter.OneSignalUser.onMethodCall(OneSignalUser.java:48)
E/DartMessenger( 6245):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/DartMessenger( 6245):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/DartMessenger( 6245):     at android.os.Handler.handleCallback(Handler.java:942)
E/DartMessenger( 6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DartMessenger( 6245):     at android.os.Looper.loopOnce(Looper.java:201)
E/DartMessenger( 6245):     at android.os.Looper.loop(Looper.java:288)
E/DartMessenger( 6245):     at android.app.ActivityThread.main(ActivityThread.java:7872)
E/DartMessenger( 6245):     at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger( 6245):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/DartMessenger( 6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Syncing files to device sdk gphone64 arm64...                       71ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on sdk gphone64 arm64 is available at:
http://127.0.0.1:57618/bxaW8-TmYKY=/
E/flutter ( 6245): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#initialize on channel OneSignal)
E/flutter ( 6245): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245):
E/flutter ( 6245): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#lifecycleInit on channel OneSignal#inappmessages)
E/flutter ( 6245): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245): #1      OneSignalInAppMessages.lifecycleInit (package:onesignal_flutter/src/inappmessages.dart:77:12)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245):
E/flutter ( 6245): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#pushSubscriptionToken on channel OneSignal#pushsubscription)
E/flutter ( 6245): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245): #1      OneSignalPushSubscription.lifecycleInit (package:onesignal_flutter/src/pushsubscription.dart:66:14)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245):
E/flutter ( 6245): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#permission on channel OneSignal#notifications)
E/flutter ( 6245): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245): #1      OneSignalNotifications.lifecycleInit (package:onesignal_flutter/src/notifications.dart:119:19)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245):
E/flutter ( 6245): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method OneSignal#addTags on channel OneSignal#user)
E/flutter ( 6245): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245): #1      OneSignalUser.addTags (package:onesignal_flutter/src/user.dart:67:12)
E/flutter ( 6245): <asynchronous suspension>
E/flutter ( 6245):
The Flutter DevTools debugger and profiler on sdk gphone64 arm64 is available
at: http://127.0.0.1:9101?uri=http://127.0.0.1:57618/bxaW8-TmYKY=/
W/Parcel  ( 6245): Expecting binder but got null!

Code of Conduct

emawby commented 1 year ago

@psatler Thank you for providing a repo to reproduce the issue we are investigating

sur2548 commented 1 year ago

This might help https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/713#issuecomment-1692991176

psatler commented 1 year ago

Hey @sur2548 , thanks for the suggestion! I have tried adding this but my release build continued to throw the same exceptions shown above. The issue I'm facing is also happening on debug and profile mode builds, though.

One thing I noticed @emawby is that if I run the app without using the addTags (or addTagWithKey) it runs without issues even after stopping and running it again. However, if I use one of those methods, and stop and run the app again, then it starts to throw the exception I showed in the original post above.

I updated the Github repo with these changes so that you can trigger the addTags by pressing the increment counter button. (also shown in this new video below)

https://github.com/OneSignal/OneSignal-Flutter-SDK/assets/3421410/10413008-781f-4ca8-b3e3-a0956763fef8

emawby commented 1 year ago

@psatler Thank you for the investigation that is helpful! There are multiple Android crash fixes coming in the next flutter release that may address this, but I have not reproduced this in my own test app so I cannot confirm. We will continue investigating.

psatler commented 1 year ago

No problem @emawby Thanks for the attention and I'm looking forward to the next release. In the meantime, I will continue investigating and if I find something new on my end, I will post it here.

psatler commented 1 year ago

Tested again on version 5.0.4 and the exception did not happen anymore. Therefore, I'll close the issue for now. Thanks!

diallomatine commented 9 months ago

@psatler what do you mean, version 5.0.4, flutter or what ?

psatler commented 9 months ago

@psatler what do you mean, version 5.0.4, flutter or what ?

Version 5.0.4 of the OneSignal SDK @diallomatine