2015-05-20 128 views
2

我想在我的通知中使用BigTextStyle和BigPictureStyle。但setStyle只接受一種樣式。如何在setStyle通知中使用BigTextStyle和BigPictureStyle?

我的代碼:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
mBuilder.setVisibility(1); 
mBuilder.setSmallIcon(R.drawable.app_icon1); 
mBuilder.setContentTitle(title.toString()); 
bigTextStyle.bigText(description.toString()); 
//mBuilder.setSubText(bigText.toString()); 
if (bigImage != null && !bigImage.toString().equals("")) { 
    mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(ImageUtil.getBitmapFromUrl(bigImage.toString()))); 
} 
mBuilder.setStyle(bigTextStyle); 
mBuilder.setPriority(Notification.PRIORITY_MAX); 
mBuilder.setContentIntent(contentIntent); 

我如何可以同時使用?我想與圖像一起顯示文本(帶換行符)!

+0

關於此主題的任何更新? 使用BigPictureStyle BigTextStyle ... –

+1

是的,它很容易。創建自己的通知佈局! 你可以設計你的XML並將其添加到通知。 欲瞭解更多詳情: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification –

回答

2

對不起,對於遲到的答覆..其實我也面臨同樣的問題,並得到了解決方案,所以我認爲它可以幫助其他用戶。

因爲我們不能使用NotificationCompat.Builder的兩個BigTextStyleBigPictureStyle方法比,我們可以創建CustomView

我們可以使用setCustomBigContentView(RemoteViews)方法NotificationCompat.Builder並創建我們自己的視圖來顯示大圖像與大文本。

請檢查下面的代碼吧: -

PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle("YOUR_APP_NAME"); 
     notificationBuilder.setContentText(body); 
     notificationBuilder.setTicker("YOUR_APP_NAME"); 
     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setSound(defaultSoundUri); 
     notificationBuilder.setCustomBigContentView(remoteView("YOUR_MESSAGE_TO_SHOW"));///IT IS THE MAIN METHOD WHICH WE USE TO INFLATE OR CREATE THE CUSTOM VIEW 
     notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder)); 
     notificationBuilder.setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build()); 

下面是我們從setCustomBigContentView()方法稱爲

private RemoteViews remoteView(String message) 
    { 
     RemoteViews views; 
     views = new RemoteViews(getPackageName(), R.layout.YOUR_LAYOUT_HERE); 
     views.setImageViewBitmap(R.id.YOUR_BIG_IMAGE_ID_FROM_LAYOUT, bitmap); 
     views.setImageViewBitmap(R.id.YOUR_APP_ID_FROM_LAYOUT, BitmapFactory.decodeResource(getResources(), R.drawable.APP_ICON_OF_YOUR_APP)); 
     views.setTextViewText(R.id.YOUR_BIG_TEXTVIEW_ID_FROM_LAYOUT, message); 
     return views; 
    } 

我創建了自定義通知喜歡它Custom Notification example

的RemoteViews
相關問題