2016-11-20 5 views
0

Мне нужна ваша помощь, я отправился в Google и много раз искал, у меня серьезная проблема, когда мое приложение находится в фоновом режиме, и я отправляю уведомление от подложки, затем уведомление идет, но он не загружает данные, когда я делаю свое приложение из фона на площадку, если я касаюсь значка уведомления, тогда я получаю данные.Firebase onMessageReceived не вызывается, когда приложение в фоновом режиме, что альтернатива его

Я пошел по этой ссылке также How to handle notification when app in background in Firebase

Там ребята говорят вы должны нажать на значок уведомления, чтобы загрузить данные, то, что является альтернативой ТСМ.

Я пробовал:

 @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.v(TAG, "From: " + remoteMessage.getFrom()); 
     // Check if message contains a data payload. 
//  if (remoteMessage.getData().size() > 0) { 
//   Log.v(TAG, "Message data payload: " + remoteMessage.getData().get("order")); 
// 
//  } 
//  if (remoteMessage.getNotification() != null) { 
//   Log.v(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
// 
//  } 
     sendNotification(remoteMessage.getData().get("order")); 
    } 
    private void sendNotification(String messageBody) { 

     Log.e("inside sendNotification","sendNotification"); 
     JSONObject jobj_order= null; 
     try { 
      dbHelper=new DBHelper(this); 
      jobj_order = new JSONObject(messageBody); 
      dbHelper.insertOrder(jobj_order.getJSONObject("supplier").toString(),jobj_order.getJSONObject("request").toString(),jobj_order.getString("expiry"),jobj_order.getString("id"),jobj_order.getString("quantity")); 
      Log.e("inside try block","inside try block"); 

      Intent broadcastIntent = new Intent(); 
      broadcastIntent.setAction(MainActivity.NOTIFY_ACTIVITY_ACTION); 
      broadcastIntent.putExtra("reload", true); 
      sendBroadcast(broadcastIntent); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 




     MainActivity.isNotification=true; 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.splash_logo) 
       .setContentTitle("Logizo") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

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

ответ

1

Это проще в использовании «сообщение данных» Я думаю, что вы используете «уведомление». Сообщение данных всегда загружает класс onMessageReceived.

Ниже мой рабочий код:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body")); 
} 

private void sendNotification(String messageTitle,String messageBody) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

    long[] pattern = {500,500,500,500,500}; 

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

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_stat_name) 
      .setContentTitle(messageTitle) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setVibrate(pattern) 
      .setLights(Color.BLUE,1,1) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

Если это не работает, пожалуйста, напишите Ваше onReciveMessage реализацию кода.

+0

Измените ваше sendNotification (remoteMessage.getNotification(). GetBody()); send_notification (remoteMessage.getData()) и любые данные, которые вы получаете, анализируете данные в методе sendNotification(). – Cool7

+0

Ваш прием Барнали .. да, пожалуйста, дайте мне знать .. – Cool7

+0

Просто попробуйте распечатать remoteMessage.getData(). get («порядок»), это не должно быть пустым, если это значение равно null, ваше уведомление не будет создано. также проверьте NotificationManager notificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); notificationManager.notify (0/* ID уведомления * /, notificationBuilder.build()); этот код вызывается или нет. – Cool7

Смежные вопросы