2017-09-24 151 views
0

所以點擊,我試圖得到通知,當應用程序在後臺或前臺,並試圖從火力控制檯Android應用活動發送數據有效載荷時,還沒有收到有效載荷數據,但我得到這個沒有收到通知,當應用程序在前臺的通知

  1. 我收到通知時,應用程序在後臺
  2. 沒有收到通知,當應用程序在前臺
  3. 此外,datapayload這我從火力控制檯獲取,使用鍵「海峽」,未在活動中顯示

請告訴我問題在哪裏以及如何解決。

private static final String TAG = "FirebaseMessagingServic"; 

public FirebaseMessagingService() { 
} 

String value = "0"; 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     value = remoteMessage.getData().get("str"); //key text in quotes 

    } 
    String title = remoteMessage.getNotification().getTitle(); //get title 
    String message = remoteMessage.getNotification().getBody(); //get message 

    sendNotification(title, message, value); 
    // Check if message contains a notification payload. 
    /* if (remoteMessage.getNotification() != null) { 



     Log.d(TAG, "Message Notification Title: " + title); 
     Log.d(TAG, "Message Notification Body: " + message); 


    }*/ 
} 

@Override 
public void onDeletedMessages() { 

} 

private void sendNotification(String title,String messageBody, String value) { 
    Intent intent = new Intent(this, CS101noti.class); 
    SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE); 
    prefs.edit().putString("body", value).apply(); 

    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.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

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

回答

0

嗯,我已經解決了吧:)

你只需要從REST API或郵差在Java代碼中發送數據的有效載荷,並獲得數據值, 它會像神韻:)

相關問題