MaikuB / flutter_local_notifications

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

NPE in setupNotificationChannel on Galaxy A70s #506

Closed dluksza closed 4 years ago

dluksza commented 4 years ago

I only know about this issue from crash reports in Google Play Console. I was not able to reproduce it on my own device nor on emulator.

I've got two or three reports from users that said that notifications doesn't work, unfortunately don't know what device they are using.

This error seems to be present since 0.8.4 up to 1.1.2. I'm going to release new app version with 1.2.0 soon. I do assume that this error is still present and probably it is related to some battery optimizations Samsung did on their side. But anyway wanted to report this, maybe there would be some kind of nice workaround for it ;)

Here is the stacktrace I've got for Google Play Console:

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3624)
  at android.app.ActivityThread.access$1300 (ActivityThread.java:240)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1808)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7094)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: 
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupNotificationChannel (FlutterLocalNotificationsPlugin.java:46)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification (FlutterLocalNotificationsPlugin.java)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification (FlutterLocalNotificationsPlugin.java)
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver.onReceive (ScheduledNotificationReceiver.java:77)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3615)
  at android.app.ActivityThread.access$1300 (ActivityThread.java:240)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1808)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7094)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:975)
MaikuB commented 4 years ago

Have you setup the appropriate configuration file so that the notification icons don't get removed by R8's resource shrinking as well as the appropriate Proguard rules file?

MaikuB commented 4 years ago

If it's Samsung specific, it's not something I know about to provide a workaround and don't have access to Samsung devices

dluksza commented 4 years ago

I had config for Proguard, but not for R8, will test this with next release.

MaikuB commented 4 years ago

Worth double checking the Proguard rules file you have though, as I added more details to readme since around 0.9.0 as it was around the time Flutter changed to use R8 compiler for Android apps and the GSON dependency had some R8 specific rules.

dluksza commented 4 years ago

Thank you, will go over integration docs and double check everything once again.

dluksza commented 4 years ago

Seems that configuring R8 has significantly reduced the amount of NPE's, but they are still present like one or two per week.

MaikuB commented 4 years ago

Afraid there's not much I can do unless know of ways to reproduce it

MichaelFerrier commented 4 years ago

I've also seen this same error in the Google Play Console. Identical stack trace.

For me, it's occurred 8 times in the past 7 days, on these devices:

Asus ZenFone V (ASUS_A006), 3840MB RAM, Android 8.0 Motorola moto g(7) (river), 3584MB RAM, Android 9 Samsung Galaxy Note8 (greatqlte), 5376MB RAM, Android 9 OnePlus OnePlus5 (OnePlus5), 5888MB RAM, Android 9 Samsung Galaxy S8 (dreamqltesq), 3584MB RAM, Android 8.0

I have the following line in my proguard-rules.pro:

-keep class com.dexterous.* { ; }

MaikuB commented 4 years ago

@MichaelFerrier What you've got is not sufficient. Please refer back to the readme at https://github.com/MaikuB/flutter_local_notifications/blob/master/flutter_local_notifications/README.md#release-build-configuration

MichaelFerrier commented 4 years ago

@MaikuB Thanks for the reply. I updated my proguard-rules.pro file to include everything in the example here. Unfortunately, this new build still shows the same NullPointerException occurring in the Google Play Console, at FlutterLocalNotificationsPlugin.java line 46.

MaikuB commented 4 years ago

Can you provide a link to minimal sample app to reproduce the problem and have you been able to reproduce this in release mode as well (e.g. try going back to an older version of your code that was before adding in the rest of the rules)? Also what versions of the plugin and Flutter SDK are you using?

The other thing I'm thinking (and I'm not 100% certain on this though it seems to make sense) is that you may continue to see for existing users who have scheduled notifications in the past as the absence of those rules before caused the details of the notification to not be serialised properly (i.e. missing details) and the application of the new rules now is more of a fix going forward. If this is the case there may not be much I can do about that

MichaelFerrier commented 4 years ago

Thanks for the reply. Interesting point about notifications scheduled before the rules were added to proguard-rules.pro possibly still causing the problem. It only schedules one notification at a time, so I will keep an eye on it to see if this error occurs at most once per user of the new version.

I'm using flutter channel stable, v1.12.13+hotfix.8, and flutter_local_notifications: ^1.3.0. I have only ever seen this problem in release builds; I've never repro'd it myself, I've only seen it appear in the Google Play console where it's occurred in a few dozen out of thousands of sessions.

If it would be useful I can make a version of my app code with everything not notification-related removed. There is very little notification-related code however; it is as follows:

void main() async {
  flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
  ...
}

class HomePageState extends State<HomePage> {
  ...
  @override
  void initState() {
    super.initState();
    ...
    if (!_notificationPluginInitialized) {
      // Initialize the notifications plugin. app_icon needs to be a added as a drawable resource to the Android head project
      var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/rvt_icon');
      var initializationSettingsIOS = new IOSInitializationSettings();
      var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
      flutterLocalNotificationsPlugin.initialize(initializationSettings,
          onSelectNotification: staticOnSelectNotification);
      _notificationPluginInitialized = true;
    }
  }

  static Future staticOnSelectNotification(String payload) async {
    if (currentState != null) {
      return currentState.onSelectNotification(payload);
    }
  }
}

/// Schedules a notification that specifies a different icon, sound and vibration pattern
Future scheduleNotification(DateTime scheduledNotificationDateTime) async {
  var vibrationPattern = new Int64List(4);
  vibrationPattern[0] = 0;
  vibrationPattern[1] = 1000;
  vibrationPattern[2] = 5000;
  vibrationPattern[3] = 2000;

  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'rv_tournament_feedback_ready',
      'Target Image Feedback',
      'This notification occurs when the target image feedback arrives for a round that you entered.',
      importance: Importance.Max,
      priority: Priority.High,
      vibrationPattern: vibrationPattern,
      largeIcon: '@mipmap/rvt_icon',
      color: const Color.fromARGB(255, 67, 12, 78));
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.schedule(0, 'RV Tournament', 'View today\'s target feedback image',
      scheduledNotificationDateTime, platformChannelSpecifics);
}

scheduleNotification(RVTApp.connectionData.timeWhenNextFeedbackAvailable);

The crash log I am seeing in the Google Play Console is as follows:

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3533)
  at android.app.ActivityThread.access$1400 (ActivityThread.java:207)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1769)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6898)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:537)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: 
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupNotificationChannel (FlutterLocalNotificationsPlugin.java:46)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification (FlutterLocalNotificationsPlugin.java)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification (FlutterLocalNotificationsPlugin.java)
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver.onReceive (ScheduledNotificationReceiver.java:77)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3517)
  at android.app.ActivityThread.access$1400 (ActivityThread.java:207)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1769)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6898)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:537)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
MaikuB commented 4 years ago

Thanks for the info. Based on what you said, does that mean after you added the new rules, you were seeing the errors in the Google Play console for an updated release that's was not on the production track? If so, was this testing done on a new installation of the app or an update?

MichaelFerrier commented 4 years ago

The errors reports I've seen on Google Play Console have all been on the production track. The good news is that the new version of my app that included the full set of proguard rules has only produced one crash of this type, six days ago, shortly after the update. This is consistent with your idea that notifications created by the previous version (with incomplete rules) may have been serialized wrong and could produce a crash with the new version.

MaikuB commented 4 years ago

In the absence of steps to reproduce this I'll have to close this. From the conversations above, this is likely from not having completely done all of the release build configuration the first time around. If so, this isn't something that can't be retroactively fixed