0

I have been trying to display a notification but it either doesn't show or causes a fatal error inside the notify method. This notification is supposed to effectively be a toast that will stay in the notification drawer until tapped.

I've tried several different intents including none. I have also, at points, copied an entire example in which still didn't work.

I have no clue what's causing the error and I've tried to attach a logcat into the app but I wasn't able to get anything out.

I'm using a Pixel 2 running stock 8.1; my programming is on the phone itself so I can't use adb / root options

  public int noti(String title, String body, String ico){
//Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
showToast(title+">"+body+">"+ico);
try{
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/"));
  PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
  Notification.Builder mBuilder =
    new Notification.Builder(getApplicationContext(), "83")
      .setSmallIcon(Icon.createWithContentUri(ico))
      .setContentTitle(title)
      .setContentText(body)
      .setOngoing(true)
      .setContentIntent(pendingIntent)
      .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
  Notification noti = mBuilder.build();
  if(noti==null){
    logBack("noti.builder == null.");
    return -2;
  }
  int notificationId = notiIDz.getAndIncrement();
  // notificationId is a unique int for each notification that you must define
  notificationManager.notify(notificationId, noti);
  return notificationId;
} catch(Exception e){
  logBack(e.toString());
}
return -1;

}

I know that the parameters are valid from the toast, I also know that none of the logBack()s are fired.

Notification channel creation, called during onCreate:

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  NotificationChannel channel = new NotificationChannel("83", "vzyNotiChan", NotificationManager.IMPORTANCE_DEFAULT);
  channel.setDescription("The notification channel for vzy apps.");
  // Register the channel with the system; you can't change the importance
  // or other notification behaviors after this
  NotificationManager notificationManager = getSystemService(NotificationManager.class);
  notificationManager.createNotificationChannel(channel);
  }

}

Edit, finally got to the logcat:07-24 10:48:10.879 27133 27133 E AndroidRuntime: android.app.RemoteServiceException: Bad notification posted from package vzy.html.tester: Couldn't create icon: StatusBarIcon(icon=Icon(typ=URI uri=content://com.android.externalstorage.documents/document/primary%3Ayp_sodapop.png) visible user=0 )

vzybilly
  • 323
  • 3
  • 14

1 Answers1

1

My guess is that you didn't create a notification channel for your notification. On All Android devices running 8.0+, each notification needs to be associated to a notification channel. You're passing in the channel ID "83" to the notification builder, but maybe you haven't actually created the channel beforehand.

For more information on how to create channels, check this out:

https://developer.android.com/training/notify-user/channels

Dominik G.
  • 610
  • 3
  • 7