2017-08-04 95 views
0
  • 我可以成功地接收通知和數據消息與圖像時,應用程序在前臺火力地堡通知犯規顯示圖像。
  • 當後臺程序/殺死,然後onMessageReceived(消息)不會調用,所以我使用getIntent(),我得到的數據,但是,當應用程序在後臺,然後Android操作系統自動顯示使用系統托盤通知,但圖像不能顯示。
  • 所以我的問題是顯示通知文字與圖像?

我使用Django的API來發送通知和發送的數據如下當應用程序處於後臺

fields = { 
     'registration_ids': registrationIds, 
     "notification" : { 
      "body" : desc, 
      "title" : name, 
      "icon" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg" 
     }, 
     "data" : { 
      "id" : "1", 
      "body" : desc, 
      "title" : name, 
      "image" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg" 
     } 
    } 

下面是我onMessageReceived()方法

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("id")); 
     id = String.valueOf(remoteMessage.getData().get("id")); 
    } 
    if (remoteMessage.getNotification() != null) { 
     String title = remoteMessage.getNotification().getTitle(); 
     String message = remoteMessage.getNotification().getBody(); 
     String icon = remoteMessage.getNotification().getIcon(); 
     Intent intent = new Intent(this, AnotherActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra("id",id); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle(title); 
     notificationBuilder.setContentText(message); 
     notificationBuilder.setContentIntent(pendingIntent); 
     notificationBuilder.setSound(defaultSoundUri); 
     notificationBuilder.setSmallIcon(R.drawable.jv); 
     notificationBuilder.setAutoCancel(true); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

     ImageRequest imageRequest = new ImageRequest(icon, new Response.Listener<Bitmap>() { 
      @Override 
      public void onResponse(Bitmap response) { 
       notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response)); 
       NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       notificationManager.notify(0, notificationBuilder.build()); 
      } 
     }, 0, 0, null, Bitmap.Config.RGB_565,new Response.ErrorListener(){ 
      @Override 
      public void onErrorResponse(VolleyError error) { 
      } 
     }); 
     MySingleton.getmInstance(this).addToRequestQue(imageRequest); 
    } 
} 
+0

當在後臺程序/殺然後onMessageReceived()不被調用。 – Dexture

回答

0

在FCM消息添加這些行

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_face) 
      .setContentTitle("title") 
      .setContentText("message").setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)) 
       .setLargeIcon(getBitmapfromUrl(messageBody.getData().get("**YOUR_IMAGE_URL**"))); 

    public Bitmap getBitmapfromUrl(String imageUrl) { 
    try { 
     URL url = new URL(imageUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap bitmap = BitmapFactory.decodeStream(input); 
     return bitmap; 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 

    } 
} 
+0

當應用程序在後臺/殺死,然後onMessageReceived()不叫。 –

相關問題