2014-12-11 2 views
2

MainActivity는 모든 조각을 호스팅합니다.알림에서 onNewIntent() 호출

사용자가 Google Cloud Messaging에서받은 알림을 클릭 할 때 특정 부분을 표시해야합니다.

onNewIntent()에서 처리 할 수 ​​있다는 인상을 받았지만 알림을 누르면이 메서드는 실행되지 않습니다.

private void sendNotification(String message, String eventId) { 

    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.putExtra(Constants.eventIdBundleKey, eventId); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_test) 
        .setContentTitle(getString(R.string.app_name)) 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(message)) 
        .setContentText(message); 
     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

} 

여기에 내가받을려고 MainActivity, 및 과정 :

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 


    if(intent.hasExtra(Constants.eventIdBundleKey)) { 

     Fragment fragment = GiftsPriceFragment.newInstance(Common.retrieveAppUser(this), null, null); 
     mFm.beginTransaction().replace(R.id.content_frame, fragment) 
       .addToBackStack(GiftsPriceFragment.FRAGMENT_TAG).commit(); 

    } 

} 

어떤 생각

은 여기 내 GcmIntentService에 클래스 내 전송 알림 방법입니다 onNewIntent()가 호출되지 않는 이유는 무엇입니까?

답변

7

launchMode of your activity as "singleTop"을 매니페스트에 설정해보십시오.

공식 문서에서.

이것은 패키지에서 launchMode를 "singleTop"으로 설정하거나 클라이언트가 startActivity (Intent)를 호출 할 때 FLAG_ACTIVITY_SINGLE_TOP 플래그를 사용하는 활동에 대해 호출됩니다. 두 경우 모두 시작되는 활동의 새 인스턴스 대신 활동 스택의 맨 위에있는 동안 활동이 다시 시작되면 on-newIntent()가 다시 시작하는 데 사용 된 인 텐트가있는 기존 인스턴스에서 호출됩니다 그것.

새로운 의도를 받기 전에 활동이 일시 중지되므로이 메소드 후에 호출되는 onResume()을 기대할 수 있습니다.

getIntent()는 여전히 원래 의도를 반환합니다. setIntent (Intent)를 사용하여이 새로운 Intent로 업데이트 할 수 있습니다.

+0

아, 실제로 내 매니페스트에서 singleTop이 필요하고 setIntent()를 호출 한 것처럼 보입니다. 이제 내 유일한 문제는 내가 보낸 문자열을 검색 할 수없는 것입니다, 그것은 onNewIntent() null이 나타납니다? – jwBurnside

+0

해당 문자열을 전혀 검색하지 않습니다. 아마도 hasExtra()가 false를 반환한다고 말할 수 있을까요? – greenapps

관련 문제