How to make a notification with support for older versions of android (api 16, 17 ..). In my test service, there is a method that builds a notification on later versions of android, everything works (android 5,6,7), and on android 4.4 and below some kind of error appears. Do not scold me strictly new

Method to create a notification

private void showNotify() { Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(MyConstants.ACTION.MAIN_ACTION); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Intent closeIntent = new Intent(this, MyService.class); closeIntent.setAction(MyConstants.ACTION.STOP_ACTION); PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0); notification = new NotificationCompat.Builder(this) .setContentTitle("ВСстовый Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ") .setContentText("ОписаниС") .setSmallIcon(R.mipmap.ic_launcher) .addAction(R.drawable.ic_close, "Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ", pCloseIntent) .setContentIntent(pendingIntent).build(); startForeground(MyConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification); } 

Mistake

 FATAL EXCEPTION: main android.app.RemoteServiceException: Bad notification posted from package androidapp.testapp: Couldn't expand RemoteViews for: StatusBarNotification(pkg=androidapp.testapp id=101 tag=null score=0 notn=Notification(pri=0 contentView=androidapp.testapp/0x109008f vibrate=null sound=null defaults=0x0 flags=0x62 kind=[null] 1 action) user=UserHandle{0}) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 

    1 answer 1

    You fall because of the Action button ( addAction(R.drawable.ic_close, "Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ", pCloseIntent ), which does not work on older versions, you need to branch in code like this:

     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { //Π½ΠΎΡ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ с экшн ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ } else { //Π½ΠΎΡ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ Π±Π΅Π· экшн ΠΊΠ½ΠΎΠΏΠΊΠΈ } 
    • I'll try now. Thank you - OVERFLOW
    • Removed the button all the rest left and set the condition as you said. Anyway crashes on Android 4.4 - OVERFLOW
    • This is in any way connected with ACTION - remove all setAction() from the code - Barmaley
    • I have indicated that if the version is more than KITKAT to launch the notification with the button, everything will work. Only now, as for the old versions, make sure that if you swipe the notification, the service stops - OVERFLOW
    • one
      You need setDeleteIntent() it works when brushing a notification - Barmaley