【问题标题】:Big Picture Notification in androidandroid中的大图片通知
【发布时间】:2016-08-29 04:56:17
【问题描述】:

我已经使用 FCM 完成了 android 推送通知。但是当我尝试大图片通知时,它没有在通知栏中显示完整图像。图像的左侧和右侧被裁剪。请告诉我如何修复问题。我尝试提供 512*256 图像和 600*300 大小的图像。

【问题讨论】:

  • 你为什么不张贴你得到的东西和你想要的样子的屏幕截图。

标签: android kotlin notifications


【解决方案1】:

我使用 Glide 库从 URL 加载图像...然后我使用大图片通知加载了此图像。

public class ImageNotification extends AppCompatActivity {
    public Bitmap image ,bmp;
    public NotificationCompat.Builder nb;
    final String url = "https://www.google.es/images/srpr/logo11w.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_notification);


    }

    public void createNotification(View view) {
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(this, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        nb = new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.icon);
        nb.setContentTitle("Image Notification");
        nb.setContentText("Set Content text");
        nb.setTicker("Set Ticker text");


        Glide.
                with(this).
                load(url).
                asBitmap()
                .centerCrop()
                .into(new SimpleTarget<Bitmap>(100,100) {
                          @Override
                          public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                              NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(resource);
                              bps.setSummaryText("Summary text appears on expanding the notification");
                              nb.setStyle(bps);

                          }
                      });






        TaskStackBuilder TSB = TaskStackBuilder.create(this);
        TSB.addParentStack(ImageNotification.class);
        // Adds the Intent that starts the Activity to the top of the stack
        TSB.addNextIntent(intent);
        PendingIntent resultPendingIntent =
                TSB.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(true);
        nb.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nb.setSound(alarmSound, AudioManager.STREAM_MUSIC);
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(11221, nb.build());
    }

}

如果您从 mipmap 或 drawable 加载图像,您可以使用以下代码替换为 glide 库编写的代码

Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.picture);
      NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
       s.setSummaryText("Summary text appears on expanding the notification");
        nb.setStyle(s);

【讨论】:

    【解决方案2】:

    以下是 FCM Notification 中 BigPicture 的完整代码:

    Bitmap bitmap = getBitmapFromURL(imageUrl);
    
      private void showBigNotification(Bitmap bitmap,NotificationCompat.Builder mBuilder, int icon, String title, String  message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
    
        mBuilder.setSmallIcon(R.drawable.ic_message_alertnoti).setTicker(title).setWhen(0);
        Intent intent = new Intent().setClassName("com.packageName", "com.packageName.activity.YourActivity"); // give any activity name which you want to open
        try {
            intent.putExtra("regId", userInfo.selectOneField(UserInfo.DEVICE_ID));
        } catch (DAOException e) {
            e.printStackTrace();
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
        mBuilder.setContentTitle(title);
        mBuilder.setColor(ContextCompat.getColor(mContext, R.color.colorOrange));
        mBuilder.setContentText(message);
        mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));/*Notification with Image*/
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        if (Build.VERSION.SDK_INT >= 21) mBuilder.setVibrate(new long[0]);
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
        notificationManager.notify(Config.NOTIFICATION_ID, mBuilder.build());
    }
    
    /**
     * Downloading push notification image before displaying it in
     * the notification tray
     */
    public Bitmap getBitmapFromURL(String strURL) {
        try {
            URL url = new URL(strURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    【讨论】:

      【解决方案3】:

      更改您的drawable名称并将其调整为96 * 96并将其放入drawable-xhdpi文件夹中。并在通知中使用重命名的drawable。

      这是 BigPicture 通知的代码:

           Bitmap icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.k);
      
                  NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
                  bigPicture.bigPicture(icon1);
      
      
                   NotificationCompat.Builder mBuilder =
                              new NotificationCompat.Builder(getApplicationContext())
                               .setSmallIcon(R.drawable.ic_launcher)
                               .setContentTitle("Big picture")
      
                               .setStyle(bigPicture);
      
                   NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                   mNotificationManager.notify(4, mBuilder.build());
      

      【讨论】:

      • 我试过 96*96 现在长度没问题,但高度问题。有什么办法从代码中处理它。
      • 尝试将图像放入 xxhdpi 和 xxxhdpi 文件夹。然后它也不起作用然后将此图像放入 48*48 并放入 xhdpi。
      • 如果图像来自后端,它将如何工作?
      • 是的。我正在从 url 设置图像
      • 使用 imageview 作为通知图标?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多