11

사용자가 알림을 클릭하면 앱이 배경에있을 때 추가 매개 변수와 함께 특정 활동을 열려고합니다. click_action을 사용하고 있으며 정상적으로 작동하고 있습니다. 앱에서 원하는 활동을 엽니 다.FCM (Firebase Cloud Messaging) - 사용자가 엑스트라를 사용하여 알림을 클릭하면 활동 시작

이제 추가 매개 변수 인 id을이 액티비티에 전달해야 서버에서 알림과 관련된 원하는 세부 정보를 제공 할 수 있습니다. 전자 메일 응용 프로그램과 마찬가지로 알림을 클릭하면 해당 특정 전자 메일의 세부 정보가 열립니다.

어떻게하면됩니까?

+0

몇 가지 코드를 공유 할 수 있습니까? 내 앱에서 비슷한 점이 있지만 GCM을 사용하고 있습니다. 통지를받는 서비스에서, 나는 Intent에 여분의 문자열을 추가하고 이것을 사용하여 어떤 프래그먼트를 열어야 하는지를 알 수있다. – Vucko

+0

나는 어떤 도움도 필요 없어, 내 애플 리케이션이 작동 :) 당신이 그것을 고쳐 준 당신을 위해 좋은 – Vucko

+0

그것을 해결 했습니까? 나는 같은 문제를 가지고있다. 나는 targetActivity를 시작했지만 의도로부터 추가 정보를 얻지 못했습니다. 어떤 해결책이 있습니까? –

답변

32

좋아, 나는 해결책을 발견했다.

앱이 폐쇄 또는 배경에있을 때 나는 AndroidManifest.xml을

<activity 
    android:name=".ActivityXPTO" 
    android:screenOrientation="sensor" 
    android:windowSoftInputMode="stateHidden"> 
    <intent-filter> 
     <action android:name="ACTIVITY_XPTO" />   
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

에서 응용 프로그램

{ 
    "registration_ids": [ 
    "XXX", 
    ... 
    ], 
    "data": { 
    "id_offer": "41" 
    }, 
    "notification": { 
    "title": "This is the Title", 
    "text": "Hello I'm a notification", 
    "icon": "ic_push", 
    "click_action": "ACTIVITY_XPTO" 
    } 
} 

서버로부터 전송있어 JSON과 사용자가 클릭입니다 알림에서 내 ActivityXPTO를 열어 오직해야 할 id_offer를 검색합니다.

public class ActivityXPTO extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ... 

     String idOffer = ""; 

     Intent startingIntent = getIntent(); 
     if (startingIntent != null) { 
      idOffer = startingIntent.getStringExtra("id_offer"); // Retrieve the id 
     } 

     getOfferDetails(id_offer); 
    } 

    ... 
} 

그게 전부예요 ...

+2

http://stackoverflow.com/questions/37554274/open-app-on-firebase-notification-received-fcm/37626817#37626817 –

+0

답안을 보내 주셔서 감사합니다. 당신의 방법을 사용하여 나는 targetActivity를 시작했으나 의도로부터 엑스트라를 얻지 못했습니다. 어떤 해결책이 있습니까? –

+0

안녕하세요 @ Md.SajedulKarim, 서버에서 보내는 json의 예가 있습니까? 추가 정보는 json의 "데이터"개체 안에 추가해야합니다. – m4n3k4s

0

활동을 시작하고 메소드 onCreate의 활동에 사용하는 추가 정보를 사용하여 getIntent(). getExtras()를 사용하여 추가 정보를 사용하십시오. 예를 들어 :

활동 시작 : 활동에서

Intent intent = new Intent(context, TargetActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putString("extraName", "extraValue"); 
intent.putExtras(bundle); 
startActivity(intent); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle bundle = getIntent().getExtras(); 
    String value = bundle.getString("extraName"); 
    .... 
} 
+1

누가 활동을 시작했는지 나인 것이 아닙니다. 이것은 시스템에 의해 처리됩니다. 앱이 백그라운드에 있거나 닫혔습니다. – m4n3k4s