2017-08-04 118 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()不叫。 –

相关问题