1

I am trying to show notification in the app. App show notification when background or foreground But the App crash when app not clear from app background stack. I don't know why it happens. below is my app notification code Any Help will be Appreciated.

if (VERSION.SDK_INT >= 26) {
    notif = (new android.app.Notification.Builder(this, this.getString(string.notification_channel_id))).setSmallIcon(notificationResId).setContentTitle(title).setStyle((new BigTextStyle()).bigText(this.mText == null ? "" : this.mText)).setContentText(this.mText == null ? "" : this.mText).setVisibility(0).setChannelId(this.getString(string.notification_channel_id)).setAutoCancel(true);
} else {
    mBuilder = (new Builder(this)).setContentTitle(title).setSmallIcon(notificationResId).setStyle((new android.support.v4.app.NotificationCompat.BigTextStyle()).bigText(this.mText == null ? "" : this.mText)).setContentText(this.mText == null ? "" : this.mText).setAutoCancel(true).setDefaults(-1);
}

if (VERSION.SDK_INT >= 26) {
    notif.setContentIntent(contentIntent);
    this.mNotificationManager.notify(id, notif.build());
} else {
    mBuilder.setContentIntent(contentIntent);
    this.mNotificationManager.notify(id, mBuilder.build());
}

Error:

java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=null vibrate=default sound=default defaults=0xffffffff flags=0x11 color=0x00000000 vis=PRIVATE semFlags=0x0 semPriority=0)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
tech_android
  • 719
  • 1
  • 7
  • 18

1 Answers1

0

Add this meta in your manifest in 'application'

<meta-data
         android:name="com.google.firebase.messaging.default_notification_icon"
         android:resource="@mipmap/ic_hello" />
Daniel Carreto
  • 211
  • 1
  • 9
  • I did this, but I still have this problem. In my case the icon is at the drawable folder instead of the mipmap, but I'm not sure that's the problem because it works on some devices and does not on others. – Leonardo Sibela Jul 02 '20 at 15:52
  • 1
    hi!, if you have notification control you could put the icon with notificationcompatbuilder like question but if firebase have the control of show notifications when the app is in background firebase will show this icon (metadata), but is your icon a svg format whithout background? or are you have all pack of images for all resolutions? – Daniel Carreto Jul 03 '20 at 16:29
  • 1
    Hello Daniel. I indeed solved my problem using the setSmallIcon method from the notificationcompatbuilder, once the meta-data tag was not working everytime. I do not have a svg without background because my icon is too complex, but I do have an image (white png with blank background) for all resolutions (hdpi, mdpi, xhdpi, xxhdpi and xxxhdpi). I'm still not sure whats going on. – Leonardo Sibela Jul 06 '20 at 19:08
  • Great! I would like to share with you this link https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase (check the answer have 159 votes), hoping it will help you a little bit with this – Daniel Carreto Jul 07 '20 at 21:01