简体   繁体   中英

How to show Android notification with picture in notification tray in Android Oreo and above?

I want to show notification like this(on Android 7.0 API24):

在此处输入图像描述

But I got result with just tray icon not full content.(Android 8.1.0 API27): 在此处输入图像描述

CODE:

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        String channelID="channelID";
        String channelName = "channelName";

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder;

        Intent notifyIntent = new Intent(this, MainActivity.class);

        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Android 8.1.0 API27
            NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, notificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);

            notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelID);

            notificationBuilder
                    .setSmallIcon(R.drawable.ic_add_post)
                    .setContentTitle("some title")
                    .setContentText("some message")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setVibrate(new long[]{1000, 1000})
                    .setLights(Color.BLUE, 1,1)
                    .setShowWhen(true)
                    .setContentIntent(notifyPendingIntent);
            notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
        } else {

            notificationBuilder = new NotificationCompat.Builder(getApplicationContext());

            notificationBuilder
                    .setSmallIcon(R.drawable.ic_add_post)
                    .setContentTitle("some title")
                    .setContentText("some message")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setVibrate(new long[]{1000, 1000})
                    .setLights(Color.BLUE, 1,1)
                    .setPriority(NotificationManager.IMPORTANCE_HIGH)
                    .setContentIntent(notifyPendingIntent);
            notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
        }

I don't know why it is working at lower api, but at higher api it is not working!

It seems like there is problem with NotificationChannel, but cannot get reason.

How to show full content of notification like above picture at over OREO api?

I don't see anything suspicious in your code, but there is a chance, that Android removed this feature (sneak peek of important notification) - don't know anything about that - or custom OEM producer redesigned it's UI considering this sneak peek as undesirable

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