2017-05-14 1 views
0

알림 및 데이터 페이로드가있는 Firebase 메시지 수신과 관련하여 질문이 있습니다. documentation은 데이터가 "의도 외의 용도로"도착할 것이라고 말합니다.Android 앱에서 Firebase Cloud Messaging의 데이터 페이로드 (incn 알림) 처리/처리 방법은 무엇입니까?

내 질문은 인 텐트 (또는 활동)? 앱을 백그라운드로 전환 할 때 사용자가 중단 한 화면이 있습니다. 그렇다면 앱의 모든 Intents/Activities에 대한 Extra를 검색해야합니까?

앱이 포어 그라운드에 오면 어디서 어떻게 데이터 페이로드를 검색하도록 실제로 코딩합니까?

감사합니다.

ADDED는 :

내 말은, 내가 10 개 + 활동이 alraedy, 앱이 끝나면 더있을 것입니다. 그렇다면 모든 액티비티에 대해 엑스트라를 가져 와서 앱이 푸시 데이터 페이로드로 다시 열렸는 지 확인해야합니까?

+0

기본 동작을 재정의하는 방법에 대한 설명이 포함되도록 답변을 업데이트했습니다. –

답변

2

, 그것은 상태 :

모두 통지 및 데이터 페이로드 메시지, 모두 배경 및 전경. 이 경우, 통지는 장치의 시스템 트레이에 전달되고, 데이터 페이로드는 런처 활동이 매니페스트 사용 범주에 지정하여 실행 활동

의 의도의 엑스트라 에 전달 발사통. 예 :

<activity 
     android:name="com.example.MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

다른 동작을 지정하기 위해 기본 동작을 무시할 수 있습니다. message notification data에서 click_action 속성에 작업 문자열 값을 추가하십시오. 그런 다음 활동을 작성하고 해당 활동과 일치하는 목록 필터에 활동 필터를 지정하십시오. 예를 들어, 메시지 :

{ 
    "to": "dhVgCGVkTSR:APA91b...mWsm3t3tl814l", 
    "notification": { 
    "title": "New FCM Message", 
    "body": "Hello World!", 
    "click_action": "com.example.FCM_NOTIFICATION" 
    }, 
    "data": { 
    "score": "123" 
    } 
} 

이렇게 의도 필터를 정의

<activity android:name=".MyFcmNotificationActivity"> 
     <intent-filter> 
      <action android:name="com.example.FCM_NOTIFICATION" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

및 문서를 조금 명확히하기 위해, 데이터 페이로드 메시지를 수신 활동에 전달되지 않는다; 사용자가 알림을 클릭하면 전달됩니다.

1

FirebaseMessagingService 클래스를 확장해야합니다.

onMessageReceived 메서드를 재정의하십시오.

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
// ... 

// TODO(developer): Handle FCM messages here. 

Log.d(TAG, "From: " + remoteMessage.getFrom()); 

// Check if message contains a data payload. 
if (remoteMessage.getData().size() > 0) { 
    Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 

    if (/* Check if data needs to be processed by long running job */ true) { 
     // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher. 
     scheduleJob(); 
    } else { 
     // Handle message within 10 seconds 
     handleNow(); 
    } 

} 

// Check if message contains a notification payload. 
if (remoteMessage.getNotification() != null) { 
    Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
} 

// 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. 
} 

서비스를 매니페스트에 등록했는지 확인하십시오. 당신이 당신의 질문에 링크 된 문서에서

+0

나는 이미 이것을했다. 그러나 페이로드에 데이터와 알림이 모두 포함되어 있으면 onMessageReceived에 도착하지 않지만 "의도적 인 것"으로 표시됩니다. – ikevin8me

관련 문제