在折叠模式下带有图像的 Android 通知

标签 android android-notifications

我最近看到一条 YouTube 通知,通知的折叠 View 中显示了图片。

现在我知道并且已经使用通知的 BigPictureStyle 在通知展开时在通知中显示图像。但是当使用这种模式时,折叠的通知只显示摘要文本而不是图像的小缩略图。

YouTube 通知在折叠模式下显示图像的缩略图,在展开时显示通常的大图片样式大图像。这也不是大图标,因为当通知展开时,大图标是 YouTube channel 缩略图,图像显示在大图片部分。

我将如何实现这样的目标?这是扩展为大图片 View 的自定义远程 View 吗?

我什至不知道该怎么做。我当前的代码是:

            try{

               NotificationCompat.Builder builder = new NotificationCompat.Builder(this,UserRewardChannel);

                builder.setAutoCancel(true);

                builder.setContentTitle(remoteMessage.getData().get("messagetitle"));

                builder.setContentText(remoteMessage.getData().get("messagebody"));

                builder.setSmallIcon(R.drawable.ic_foodini_notif);
                builder.setColor(ContextCompat.getColor(this,R.color.colorAccent));

                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher2));

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

                builder.setSound(defaultSoundUri);

                builder.setVibrate(new long[]{0,100,100,100});

                if(Boolean.parseBoolean(remoteMessage.getData().get("hasimage"))){

                    Bitmap image=null;

                    Response response = null;

                    try {

                        OkHttpClient client = new OkHttpClient.Builder()
                                .connectTimeout(10000, TimeUnit.MILLISECONDS)
                                .readTimeout(10000,TimeUnit.MILLISECONDS)
                                .retryOnConnectionFailure(false)
                                .build();

                        Request request = new Request.Builder()
                                .url(remoteMessage.getData().get("imageurl"))
                                .build();

                        response = client.newCall(request).execute();

                        InputStream imageStream = response.body().byteStream();

                        image = BitmapFactory.decodeStream(imageStream);

                    } catch (Exception e) {
                        //Log.d("FragmentCreate","Some error occured while creating the notification in debug mode",e);
                    }finally {
                        try {
                            response.close();
                        }catch (Exception e){
                            //Do nothing
                        }
                    }

                    if(image!=null){
                        builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(remoteMessage.getData().get("messagebody")));

                    }else{
                        builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(remoteMessage.getData().get("messagebody")).bigText(remoteMessage.getData().get("messagebody")));
                    }

                }else{
                    builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(remoteMessage.getData().get("messagebody")).bigText(remoteMessage.getData().get("messagebody")));
                }

                int notif_id=(int)(System.currentTimeMillis()%10000);

                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());

                notificationManagerCompat.notify(notif_id,builder.build());

            } catch (Exception e) {
                //Log it
            }

preview of the youtube notification

最佳答案

您将需要创建自定义通知布局。有关如何创建这些的指南位于:

https://developer.android.com/training/notify-user/custom-notification

此链接中的代码描述了如何设置通知的小型和扩展布局

关于在折叠模式下带有图像的 Android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50065916/

相关文章:

android - 如何重用 FragmentManager 中的现有 fragment 而不是重新创建它们?

具有前台服务的 Android worker 不显示通知

Android 通知图标在某些设备上为空白

安卓通知 : How to show sticky/updating alarm

android - 在运行 API <11 的设备上使用 ActionBarSherlock 的全屏模式

java - 测试 getJSONArray 是否为 null

android - Android ART 运行时是否具有与 Dalvik 相同的方法限制限制?

android - FCM 推送通知在某些设备上不起作用

java - 想要访问android中的通知存储

android - 在Android上播放声音