2017-02-01 2 views
0

firebase에서 Android 애플리케이션으로 알림을 전송하는 데 필요한 몇 가지 자습서를 따라 왔으며 알림을받을 수 있습니다.어떤 활동에서 firebase 알림을 청취하는 방법

나는이 일을 이해하려고 노력하고 있어요 :

  1. 어떻게 통지의 수신을 처리하나요 사용자가 알림을 처리하지 않는 활동을보고있는 동안?

  2. 응용 프로그램이 실행 중이지만 포 그라운드에 있지 않고 알림을받는 경우 시스템 트레이에서 클릭하면 응용 프로그램이 브로드 캐스터가 정의한 활동을 엽니 다. - 어떤 활동이 열려 있는지 제어하려면 어떻게해야합니까? ? 여기

는 알림
public class DisplayNotificationActivity extends AppCompatActivity { 
    private BroadcastReceiver mRegistrationBroadcastReceiver; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_notification); 

     mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // checking for type intent filter 
      if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
       // gcm successfully registered 
       // now subscribe to `global` topic to receive app wide notifications 
       FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 
      } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
       // new push notification is received 
       String message = intent.getStringExtra("message"); 
       Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show(); 
       txtMessage.setText(message); 
      } 
     } 
    }; 

당신이 코드의 다른 부분이 알려 참조해야하는 경우

을 처리하는 활동에서 코드입니다.

답변

0

사용자가 알림을 처리하지 않는 활동을보고있는 동안 알림 수신을 어떻게 처리합니까?

가능한 한 짧게하려고합니다. 앱이 포 그라운드에있을 때 구현의 onMessageReceived() 메소드로 알림을받습니다. 이제 어떤 활동에 이벤트를 브로드 캐스트해야합니까? 기본 활동 수업을 만들고 모든 활동으로 확장하십시오. BaseActivity에서 당신은 브로드 캐스트 방식을 사용하거나이 같은처럼 일부 Eventbus 라이브러리를 사용하여 시도 할 수 있습니다 : 기본 활동을 확장

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) 
public void onNotificationReceived(NotificationReceieveBean bean){ 

if(bean != null){ 

     ViewUtils.showJobOfferSnackBar(this, bean.map); 

     EventBus.getDefault().removeStickyEvent(bean); 
    } 



} 

모든 활동 알림 이벤트를받을 수 있습니다. 그에 따라 처리 할 수 ​​있습니다. 내가 활동 인 제어하는 ​​방법 -

앱을 실행하지만, 전경에없는 내가 알림을 받게됩니다

, 나는 트레이 시스템에서를 클릭하면 응용 프로그램이 정의 방송국을 가지고 활동을 열어 열렸어?

일반적으로 시스템 생성 알림을 클릭하면 첫 번째 활동이 열립니다. 당신은에서 onCreate() 예를 들어, 당신은 서버에서 보내는 것과 같은 키를 가진 Bundle 인스턴스에서 데이터를 확인해야 :

Bundle bundle = getIntent().getExtras(); 

    if(bundle != null){ 
     // if the below condition is true then it means you have received notification 
     if(bundle.getString("notification_key_sent_from_server") != null) { 
      ViewUtils.handleIncomingFCMNotification(bundle); 

      finish(); // finishing the first activity since I want to go to some other activity 
      return; 

     } 

    } 

** 편집 **

public abstract class BaseNotificationHandlerActivity extends AppCompatActivity { 

protected int activityFlag = 0; // set a unique value from each extending Activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
} 

@Override 
public void onStart(){ 
    super.onStart(); 
    EventBus.getDefault().register(this); 
} 

    @Override 
    public void onStop() { 
    super.onStop(); 
    EventBus.getDefault().unregister(this); 
} 

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(NotificationItem notif) { 
    if(activityFlag == 1){ 
    // this means Starting Activity, so handle for it particularly 
     NotificationHelper notifHelper = new NotificationHelper(this); 
     notifHelper.AddNotificationItem(notif); 
    } 
    else if(activityFlag ==2){ 
    // for some other Activity 
    } 

    } 
} 


public class StartingActivity extends BaseNotificationHandlerActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_starting); 
    activityFlag = 1; // add unique flag for Activity here 
} 

@Override 
public void onResume() { 
super.onResume(); 
} 
+0

오케이 - 질문 1과 관련하여 특정 활동에 대해 다른 방법으로 알림 수신을 처리 할 수 ​​있습니까? – andrewb

+0

@andrewb, 그냥 통지를 처리하는 BaseActivity에서 메소드를 작성하십시오. 구현하는 액티비티가 다르게 통지를 처리하려는 경우 해당 메소드를 대체해야합니다. –

+0

BaseActivit, y, 제 사례는이 DisplayNotificationActivity입니까? – andrewb

관련 문제