2016-08-11 72 views
2

我正在使用Firebase雲消息傳遞API創建應用程序...我能夠將通知和數據從服務器發送到我的客戶端應用程序。 但問題是當應用程序打開時,數據出現時通知不會觸發(我的意思是我登錄了它)這不是問題。但是,當應用程序關閉時,會收到通知,而我單擊通知時,活動已打開,而我無法查看日誌數據。我需要將數據更新到一個TextView ..Firebase通知和數據

我MyFirebaseMessagingService:

public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     Log.e("FROM", "From: " + remoteMessage.getFrom()); 
     String data = remoteMessage.getData().get("message"); 
     Log.e("KOIII", "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     // showNotificationS(remoteMessage.getNotification().getBody(),2); 
     if(data.equals("true")){ 
      Log.e("DATA", "SUCCESS"); 
      // Fragment1.getInstace().updateTheTextView(data, data, data); 
     } 
    } 

我的清單:

<activity 
      android:name=".SplashScreen" 
      android:label="@string/app_name" 
      android:launchMode="singleInstance"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".myHome" 
      android:label="@string/app_name"> 
     </activity> 
<service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <service android:name=".FirebaseIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service> 

回答

2

FCM將不會顯示通知,如果你的應用程序是在前臺,所以你必須手動顯示如下: -

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO(developer): Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "From: " + remoteMessage.getData().get("message")); 
//  Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     sendNotification(remoteMessage.getData().get("message")); 
    } 
    // [END receive_message] 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MarketActivity.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.ic_launcher) 
       .setContentTitle("Fred Notification") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
       .setContentIntent(pendingIntent); 

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

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

是啊,我能做到這一點..但我需要的是..如果應用程序是在後臺和通知被觸發..但沒有日誌在我的logcat中看到,因爲我打印的數據..我希望你得到的情況.. –

+1

@KarthikCP請參閱這裏http://stackoverflow.com/questions/38867199/how-get-message-body -fhen-fcm-notification-message-is-tap/ –

+0

是的..我知道了..數據和通知,如果兩者都使用這個問題存在,我希望..如果只使用數據,那麼消息將被接收所有時間和可以創建通知.. http://stackoverflow.com/ a/37876727/3910281 –

5

當您發送帶有da的通知消息有效載荷(通知和數據),並且應用程序在後臺,您可以從用戶點擊通知後啓動的意向附加項中檢索數據。

從它啓動時通報挖掘在MainActivity的FCM sample

if (getIntent().getExtras() != null) { 
    for (String key : getIntent().getExtras().keySet()) { 
     String value = getIntent().getExtras().getString(key); 
     Log.d(TAG, "Key: " + key + " Value: " + value); 
    } 
} 
+1

這是否獲取數據值?或通知價值活動? –

+0

附加項僅爲來自數據有效載荷的鍵/值對。 –

+0

@亞瑟湯普森我嘗試並將其添加到我的活動..但沒有看到日誌..我應該把它放在片段?因爲我正在使用片段.. –