使用BigPicture,大小图标通知

时间:2015-10-13 09:39:24

标签: android push-notification

我有两种方式的通知实现: 1.LargeIcon +小图标(标题+消息) 2. LargeIcon + SmallIcon + Big Pictuire。(标题+消息+ Mesage2(PicURL))

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

第二次实施。我可以看到大图+小图标。 但是只有在我通知扩展大图片时才能看到大图标。 如何通知大图显示LargeIcon +小图标?

以下是代码:

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) {
            //http://stackoverflow.com/questions/22119374/how-to-show-compact-notification-if-it-is-available-on-the-user-device
            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());
}

1 个答案:

答案 0 :(得分:2)

在您的第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);
    }