android中的大图片通知

时间:2016-08-29 04:56:17

标签: android notifications

我已经使用FCM进行了android推送通知。但是当我尝试通过大画面通知它没有在通知栏中显示完整图像时。图像的左侧和右侧正在被裁剪。请让我知道如何解决这个问题。我试着给出512 * 256图像和600 * 300尺寸的图像。

3 个答案:

答案 0 :(得分:1)

我使用Glide库从URL加载图片...然后我使用Big Picture Notification加载了这个图像。

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);

答案 1 :(得分:1)

以下是来自FCM通知的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;
    }
}

答案 2 :(得分:0)

更改您的可绘制名称并将其大小调整为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());