Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when scheduling notification with high number as id - on Android #115

Closed
samueldeschamps opened this issue Oct 26, 2018 · 5 comments

Comments

@samueldeschamps
Copy link

Hello!

I'm having an error when I try to schedule a notification in my app.
The exception is: [PlatformException(error, java.lang.Long cannot be cast to java.lang.Integer, null)].

In this case, I'm passing the hashCode of an object as the id for the notification, so that I can cancel it later passing the same id.
Investigating the case, I found that the Dart int is not like the Java int (32 bits).
The Dart int is a 64-bit int (so it matches the long type in Java).
Ref: https://api.dartlang.org/stable/2.0.0/dart-core/int-class.html
I tried to pass smaller values as IDs for scheduling notifications and the error disappeared.
So now I know a workaround to solve this problem.
But it would be good if the Flutter Local Notifications plugin treated this case in order to prevent programmers passing high numbers as IDs, because in this situation the exception occurred inside the plugin, and didn't show me anything in the Dart Analysis.

Thanks and best regards!
Samuel Deschamps.

Stack trace:

E/flutter (17422): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (17422): PlatformException(error, java.lang.Long cannot be cast to java.lang.Integer, null)
E/flutter (17422): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:551:7)
E/flutter (17422): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter (17422):
E/flutter (17422): #2 FlutterLocalNotificationsPlugin.schedule (file:///Users/samuel/Development/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_local_notifications-0.3.9/lib/src/flutter_local_notifications.dart:120:20)
E/flutter (17422):
E/flutter (17422): #3 scheduleNotification (package:barber_app/commons.dart:351:36)

@MaikuB
Copy link
Owner

MaikuB commented Oct 26, 2018

The error you're seeing is because of how Flutter handles passing numbers over to the Android side as per https://flutter.io/platform-channels/

This is platform specific as notification IDs are integers on Android and isn't a problem on iOS as it's a custom field.

I'm not sure what you mean exactly by treating this case but with the above in mind, getting a PlatformException is something I envisage remaining though I could check the type that comes over to the Android side to show a more human friendly error message, in addition to putting in extra documentation.

@samueldeschamps
Copy link
Author

samueldeschamps commented Oct 26, 2018

Well, since Flutter is a multi-platform framework and the main target OS's are Android and iOS, it should provide an API that works well on both OSs, in order to avoid platform-specific code.

In my opinion, one good solution for this problem would be to change the type of the ID parameter from int to Int32 (Int32 from "fixnum" package). Or, for back-compatibility, create an overload for that, and deprecate the old method. This way it would work well for both OSs, with no errors at runtime.
Or at least warn the programmers not to pass high values there (bigger than the int32 limits).

How it happened to me:

The first time I tested flutter_local_notifications, I used a test id, like "1000", and everything worked good. "Nice, this plugin works very well!", I thought. Then I changed the code to use an object ID's hashcode (because the object ID is a UUID in my case), and this way the notification is "associated" with the object in a way that I can cancel that notification if the object is deleted, for example, passing the same hashCode. But I didn't test it again, and had errors at runtime later.
And the tricky part was that "int" for Dart is a 64-bit integer, and "int" for Java is a 32-bit integer, so they are not compatible. Then I resolved the case by converting the number to a Int32 (to remain only the low 32 bits of the number), then to int again.

@samueldeschamps samueldeschamps changed the title Exception when scheduling notification with high number as id Exception when scheduling notification with high number as id - on Android Oct 26, 2018
@MaikuB
Copy link
Owner

MaikuB commented Oct 26, 2018

That's a good suggestion on the fixnum package, I'd have to take a look at that though having to not rely on more packages would be better. I'm aware of how it happened in your scenario as I've seen a similar issue before and if you checked the link I had posted above, you'll see that Flutter decides to pass a Dart int to a Java Integer or Long depending on the value.

I think in the past I had tried passing in 2,147,483,647 as the ID but found it was being turned into a Long over a platform channel (expected to be an Integer as per the documentation) whilst investigating #88. Hence why I suggesting a platform exception earlier (note: platform exceptions are from Flutter and can be triggered via plugins using https://docs.flutter.io/javadoc/io/flutter/plugin/common/MethodChannel.Result.html#error-java.lang.String-java.lang.String-java.lang.Object-) as the behaviour didn't seem align with what was documented. This no longer seems to be the case so may alternatively look at checking the range of values on the Dart side

@MaikuB
Copy link
Owner

MaikuB commented Nov 10, 2018

FYI, I've kept it as an int but added validation on if it's still within the range of a 32-bit in version 0.4.1. Couldn't add overloads and forcing everyone to move to using Int32 could result in a major inconvenience (e.g. having to construct an Int32 object for each ID)

@MaikuB MaikuB closed this as completed Nov 10, 2018
@samueldeschamps
Copy link
Author

OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants