2015-08-13 21 views
13

Từ hai ngày tôi đang cố gắng để có được như thế này. Nhưng tôi đã thất bại. Xin vui lòng bất kỳ một đề nghị tôi làm thế nào để có được hình ảnh trong thông báo đẩy. Cảm ơn bạn trước.Làm thế nào để hiển thị hình ảnh trong thông báo đẩy (Gcm) android?

enter image description here

Mã những gì tôi đã cố gắng:

@SuppressWarnings("deprecation") 
private void handleMessage(Context mContext, Intent intent) { 
    Bitmap remote_picture = null; 
    long when = System.currentTimeMillis(); 
    int icon = R.drawable.reload_logo; 
    try { 
     Bundle gcmData = intent.getExtras(); 
     if(intent.getExtras().getString("message")!=null) 
      Log.v("TAG_IMAGE", "" + intent.getExtras().getString("message")); 
     Log.v("TAG_IMAGE", "" + intent.getExtras().getString("imageurl")); 


     { 
      NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle(); 
      notiStyle.setSummaryText(intent.getExtras().getString("message")); 

      try { 
       remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("imageurl")).getContent()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      notiStyle.bigPicture(remote_picture); 
      notificationManager = (NotificationManager) mContext 
        .getSystemService(Context.NOTIFICATION_SERVICE); 
      PendingIntent contentIntent = null; 

      Intent gotoIntent = new Intent(); 
      gotoIntent.setClassName(mContext, "com.reloadapp.reload.fragments.MainActivity");//Start activity when user taps on notification. 
      contentIntent = PendingIntent.getActivity(mContext, 
        (int) (Math.random() * 100), gotoIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        mContext); 
      Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0) 
        .setAutoCancel(true) 
        .setContentTitle("Reload.in") 
        .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) 
        .setContentIntent(contentIntent) 
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
        .setLargeIcon(remote_picture) 
        .setContentText(intent.getExtras().getString("message")) 
        .setStyle(notiStyle).build(); 


      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      count++; 
      notificationManager.notify(count, notification);//This will generate seperate notification each time server sends. 
     } 
    }catch (Throwable e) { 
     e.printStackTrace(); 
    } 
} 

Tôi nhận được biểu tượng nhỏ và biểu tượng lớn là với cùng một hình ảnh. Nhưng tôi đã khác nhau.

kết quả:

+0

vui lòng bất cứ ai giúp tôi. –

+0

Hãy giải thích những điều bạn đã thử? – Emil

+1

Bạn đã thử chuyển một chuỗi 'json' chứa url hình ảnh làm thông báo ** và phân tích cú pháp từ nó? – Emil

Trả lời

16

cảm ơn bạn @Ravi.

@SuppressWarnings("deprecation") 
    private void handleMessage(Context mContext, Intent intent) { 
     Bitmap remote_picture = null; 
     long when = System.currentTimeMillis(); 
     int icon = R.drawable.reload_logo; 
     Bundle gcmData = intent.getExtras(); 
     //if message and image url 
     if(intent.getExtras().getString("message")!=null && intent.getExtras().getString("imageurl")!=null) { 
      try { 


       Log.v("TAG_IMAGE", "" + intent.getExtras().getString("message")); 
       Log.v("TAG_IMAGE", "" + intent.getExtras().getString("imageurl")); 


       NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle(); 
       notiStyle.setSummaryText(intent.getExtras().getString("message")); 

       try { 
        remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("imageurl")).getContent()); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       notiStyle.bigPicture(remote_picture); 
       notificationManager = (NotificationManager) mContext 
         .getSystemService(Context.NOTIFICATION_SERVICE); 
       PendingIntent contentIntent = null; 

       Intent gotoIntent = new Intent(); 
       gotoIntent.setClassName(mContext, "com.reloadapp.reload.fragments.MainActivity");//Start activity when user taps on notification. 
       contentIntent = PendingIntent.getActivity(mContext, 
         (int) (Math.random() * 100), gotoIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT); 
       NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
         mContext); 
       Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0) 
         .setAutoCancel(true) 
         .setContentTitle("Reload.in") 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) 
         .setContentIntent(contentIntent) 
         .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

         .setContentText(intent.getExtras().getString("message")) 
         .setStyle(notiStyle).build(); 


       notification.flags = Notification.FLAG_AUTO_CANCEL; 
       count++; 
       notificationManager.notify(count, notification);//This will generate seperate notification each time server sends. 

      } catch (Throwable e) { 
       e.printStackTrace(); 
      } 
     } 
} 

cho đa dòng:

.setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) // use this 
+2

Kích thước của hình ảnh này là gì để nó có thể cung cấp giao diện tương tự trên tất cả các thiết bị Android với cắt xén? –

+0

kích thước của hình ảnh trong thông báo có được cố định hay không? –

+0

Bạn gọi .setStyle() hai lần tại đây; một lần cho bigText và một lần cho bigPicture. Điều này có chủ ý không? – Project

Các vấn đề liên quan