简体   繁体   中英

Android Why Notification is not working on below Oreo version?

I am refering this notficastion from this url of android documentation . trying build a simple notification but it seem this code is for oreo and above. But how should i handle notification for below version ?

Below is code i am running on a button click:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Much longer text that cannot fit one line...")
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Much longer text that cannot fit one line..."))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.feroz_channel_name);
            String description = getString(R.string.feroz_channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
            notificationManager1.createNotificationChannel(channel);
        }
// notificationId is a unique int for each notification that you must define
        notificationManager.notify(123, mBuilder.build());

its working fine on android oreo and pie version but how can i include for below versions ?

below is exception i am getting if i am running above mentioned code in below oreo versions :

Process: sample.androido.com.myapplication, PID: 14596
    android.app.RemoteServiceException: Bad notification posted from package sample.androido.com.myapplication: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=sample.androido.com.myapplication id=0x7f0f0000) visible user=0 )
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

place the small notif icon into drawable folders.

To provide support for all Android versions, developers should: Place status bar icons for Android 3.0 and later in the drawable...

check documentation

use this type of notification....

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        Notification builder = new NotificationCompat.Builder(ctx, 
  notification.CHANNEL_ID_ONE)
                .setContentTitle(title)
                .setContentText(Artist)
                .setLargeIcon(bitmap1)
                .setSmallIcon(R.drawable.back)
                .addAction(R.drawable.play,"back",pi)
                .addAction(R.drawable.play,"play",pendingIntent)
                .addAction(R.drawable.play,"next",PI)
                .build();
        NotificationManager notificationManager = (NotificationManager) 
 ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(111, builder);
    }else{
        Notification notification = new NotificationCompat.Builder(ctx)
                .setContentTitle(title)
                .setContentText(Artist)
                .setLargeIcon(bitmap1)
                .setSmallIcon(R.drawable.back)
                .addAction(R.drawable.play,"back",pi)
                .addAction(R.drawable.play,"play",pendingIntent)
                .addAction(R.drawable.play,"next",PI)
                .build();
        NotificationManager notificationManager = (NotificationManager) 
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(111, notification);
    }

You know how to set notification in Oreo so the coding of else block is for you it will definitely help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM