MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.43k stars 1.38k forks source link

Exception while trying to read notification channels #2309

Open ueman opened 4 months ago

ueman commented 4 months ago

Describe the bug

The following code

final _localNotifications = FlutterLocalNotificationsPlugin();
final androidPlugin = _localNotifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
final allChannels = await androidPlugin?.getNotificationChannels();

results in the following exception

PlatformException: PlatformException(getNotificationChannelsError, Unable to find resource ID #0x7f14000d, android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f14000d
    at android.content.res.ResourcesImpl.getResourceEntryName(ResourcesImpl.java:493)
    at android.content.res.Resources.getResourceEntryName(Resources.java:2441)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.getMappedNotificationChannel(FlutterLocalNotificationsPlugin.java:170)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.getNotificationChannels(FlutterLocalNotificationsPlugin.java:32)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:399)
    at be.j$a.a(MethodChannel.java:18)
    at pd.c.l(DartMessenger.java:19)
    at pd.c.m(DartMessenger.java:42)
    at pd.c.h(Unknown Source:0)
    at pd.b.run(Unknown Source:12)
    at android.os.Handler.handleCallback(Handler.java:966)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loopOnce(Looper.java:205)
    at android.os.Looper.loop(Looper.java:293)
    at android.app.ActivityThread.loopProcess(ActivityThread.java:9832)
    at android.app.ActivityThread.main(ActivityThread.java:9821)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:586)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1201)
, null)

To Reproduce Unfortunately, we're seeing this only in production without any actionable reproduction steps. The app is in foreground and active though.

Expected behavior I expect no exception to be thrown.

Sample code to reproduce the problem See above

imtoori commented 3 months ago

Same here, we found out it happens on some Samsung devices

MaikuB commented 2 months ago

Unless you can provide some way to reproduce this then it'll be difficult to find out what's going on. Out of interest, what's the earliest version of the plugin that the app used?

ueman commented 2 months ago

I'm currently on vacation, but I'll try to get some more details when I'm back next week

ueman commented 2 months ago

We're using the plugin for two years already. Not sure, what the first version with the issue is, though.

We still get the issue with the current, latest version (17.1.2):

PlatformException(getNotificationChannelsError, Unable to find resource ID #0x7f14000a, android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f14000a
    at android.content.res.ResourcesImpl.getResourceEntryName(ResourcesImpl.java:412)
    at android.content.res.Resources.getResourceEntryName(Resources.java:2229)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.getMappedNotificationChannel(FlutterLocalNotificationsPlugin.java:170)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.getNotificationChannels(FlutterLocalNotificationsPlugin.java:32)
    at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:399)
    at ne.j$a.a(MethodChannel.java:18)
    at be.c.l(DartMessenger.java:19)
    at be.c.m(DartMessenger.java:42)
    at be.c.i(Unknown Source:0)
    at be.b.run(Unknown Source:12)
    at android.os.Handler.handleCallback(Handler.java:900)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:219)
    at android.app.ActivityThread.main(ActivityThread.java:8349)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
, null)

It happens on a Huawei Mate 20 Pro (LYA-AL00) for example. The app is in the foreground when it happens. The app is installed via Google Play.

MaikuB commented 2 months ago

We're using the plugin for two years already. Not sure, what the first version with the issue is, though.

My question was asking about the earliest/first version of the plugin that your app uses in general. That is, it's not about the first version with this issue. Presumably, the app you're working on is in source control that this can be looked up

It happens on a Huawei Mate 20 Pro (LYA-AL00) for example. The app is in the foreground when it happens. The app is installed via Google Play.

Is your app using a custom sound? Do you by chance have access to a device that can reproduce the issue to see if it can be reproduced with the example app?

Note asking these questions as I suspect there's two possible issues but not sure what's the cause

  1. plugin use to save the specified sound by the resource ID. In general, IDs can change with app updates but I don't know/remember if a channel could end up retaining an old resource ID
  2. a difference in implementations when it comes to various Android OEMs

If you're able to help out, you could try forking to put in a fix and submit a PR back. I suspect the fix of sorts is to catch the exception thrown when calling https://github.com/MaikuB/flutter_local_notifications/blob/5375645b01c845998606b58a3d97b278c5b2cefa/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java#L2068 such that the code soundSource is same but sound is null

ueman commented 2 months ago

My question was asking about the earliest/first version of the plugin that your app uses in general. That is, it's not about the first version with this issue. Presumably, the app you're working on is in source control that this can be looked up

The earliest version we were using is flutter_local_notifications: ^9.7.1.

Is your app using a custom sound? Do you by chance have access to a device that can reproduce the issue to see if it can be reproduced with the example app?

No, we don't use custom sounds. Unfortunately, we only see this issue in prod, not on our test devices. We use a custom icon though: AndroidInitializationSettings('ic_notification');

MaikuB commented 2 months ago

Sounds more like an issue with IDs changing after an app is updated then. Are you able to try make a fork with the changes I mentioned to see if it fixes the issue and submit a PR back? Would say that's the best way to confirm

lColinDl commented 3 weeks ago

We would like to try out the following: https://github.com/Goddchen/flutter_local_notifications/commit/a6316c18246ede6f685cb06bafccb12c069ec7ec Fine with you?

MaikuB commented 3 weeks ago

@lColinDl makes sense to me. Have you been running into this issue yourself? It should at least catch the exception and allow data to be returned back

lColinDl commented 3 weeks ago

Have you been running into this issue yourself?

@ueman and me are working on the same project. So no, I also cannot reproduce this myself. But we will try out the fix mentioned above and come back to you.