2015-10-13 63 views
0

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

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

第二次执行。我可以看到大图+小图标。 但是,当我在通知上展开大图时,我才看到大图标。 如何在通知中显示带有大图的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()); 
} 

回答

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