带有 BigPicture、大图标和小图标的通知

Notification with BigPicture, Large and Small Icon

我有两种方式实现通知: 1.LargeIcon+小图标(标题+留言) 2. LargeIcon + SmallIcon +Big Pictuire.(Title+ Message+ Mesage2(PicURL) )

第一个运行正常,右下角显示大图标和小图标。

第二次实施。我可以看到大图片 + 小图标。 但是只有当我在通知中展开大图片时,我才会看到大图标。 如何在通知中显示带有大图片的 LargeIcon + Small Icon?

代码如下:

private void sendNotification(String title, String msg, String msg1) {
    Log.i("msg1", "" + msg1);
    NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();

    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Context context = getApplicationContext();
    Intent myIntent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            context,
            0,
            myIntent,
            PendingIntent.FLAG_ONE_SHOT); //Intent.FLAG_ACTIVITY_NEW_TASK);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setContentText(msg)
            .setDefaults(
                    Notification.DEFAULT_SOUND
                            | Notification.DEFAULT_VIBRATE);
    if(msg1 == null)
    {
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);
        mBuilder.setLargeIcon(largeIcon);
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
    }
    else
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            //
            try {
                Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);

                s.bigLargeIcon(largeIcon);
                s.bigPicture(Picasso.with(context).load(msg1).get());
            } catch (IOException e) {
                e.printStackTrace();
            }
            mBuilder.setStyle(s);

        }
    }


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder
                .setSmallIcon(R.mipmap.ic_notification)
                .setColor(ContextCompat.getColor(context, R.color.bgColor));
    } else {
        mBuilder.setSmallIcon(R.drawable.ic_notification);
    }


    mBuilder.setContentIntent(pendingIntent);
    int notifID =(int)System.currentTimeMillis();
    mNotificationManager.notify(notifID, mBuilder.build());
}

在您的第一个 else 条件中,您没有应用 mBuilder.setLargeIcon(largeIcon);,而您正在将 s.bigLargeIcon(largeIcon); 应用到您的 NotificationBuilder style 而不是通知生成器。

所以只需在下面的代码中添加 mBuilder.setLargeIcon(largeIcon); 即可:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);

        mBuilder
                .setSmallIcon(R.mipmap.ic_notification)
                .setLargeIcon(largeIcon);
                .setColor(ContextCompat.getColor(context, R.color.bgColor));
    } else {
        mBuilder.setSmallIcon(R.drawable.ic_notification);
    }