Reputation: 1978
I'm using this code to make 'scheduled notification' so notification would appear even if the app is not workign.
But I have some crashes at my Nexus with Android-M
that appear randomly when I'm using my phone (during phone call, when using YT app and so on).
Is it a good or a bad way to make scheduled notifications?
private void scheduleNotification(Notification notification, int delay) {
Intent notificationIntent = new Intent(mCtx, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, (int) System.currentTimeMillis());
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mCtx, (int) System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);
long futureInMillis = System.currentTimeMillis() + delay;
AlarmManager alarmManager = (AlarmManager) mCtx.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
}
EDIT: I actually caught exception but still don't know how to fix it.
Process: com.ko.makemyhaircut, PID: 2184
java.lang.RuntimeException: Unable to start receiver com.ko.makemyhaircut.model.NotificationPublisher: java.lang.NullPointerException: Attempt to read from field 'android.net.Uri android.app.Notification.sound' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to read from field 'android.net.Uri android.app.Notification.sound' on a null object reference
at android.app.NotificationManager.notify(NotificationManager.java:213)
at android.app.NotificationManager.notify(NotificationManager.java:194)
at com.ko.makemyhaircut.model.NotificationPublisher.onReceive(NotificationPublisher.java:17)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Upvotes: 1
Views: 890
Reputation: 462
I had a similar issue and it turned out I was using a global variable for the context which turned out to be null. I can see that you are doing the same. Your variable
mCtx
contains the context. If that is null, then you will get that error. It is never a good idea to save the context in a global variable.
Upvotes: 1