2011-04-14 5 views
2

내 장치에 푸시 알림을 보내는 작동하는 서버/클라이언트 솔루션이 있습니다. 내 프로젝트의 다음 단계는 C2DMReceiver 클래스에서 onReceive 이벤트가 호출 될 때 활동 창에 대화 상자를 표시하는 것입니다.C2DM 푸시 알림을 수신 할 때 활동 동작 변경

내가 안드로이드를 처음 접했을 때, 나는 이것을 어떻게하는지 모른다. 누군가 나에게 이것을 설명 할 수 있다면 정말 행복 할 것이다.

기본적으로 저는 c2dm에 대한 chrometophone Application의 클래스를 재사용했습니다. logcat에 대한 로그 항목을 만들 때 onReceive 이벤트가 호출됩니다. C2DMReceiver은 서비스이므로 새 메시지가 있으면 내 활동에 대한 정보를 얻는 방법은 무엇입니까?

나는 많이 봤지만 작동하는 해결책을 찾지 못했습니다 ... registerReceiver()을 사용하려고했지만 꽤 잘못했다고 확신합니다. 누구든지 예제가 있습니까? ,

활동

BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
     Log.w(Consts.LOG_TAG_SERVICE, "Test"); 
     } 
    }; 

    // Called when the activity is first created. 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Log.w(Consts.LOG_TAG_SERVICE, "started"); 

    } 

    // Called when the activity is restarted 
    protected void onResume() { 
     super.onResume(); 

     IntentFilter filter = new IntentFilter(); 
     filter.addCategory("com.mydomain.myapp.CONTEXT"); 

     registerReceiver(mReceiver, filter); 
    } 

    // Called when the activity is closed 
    protected void onPause() { 
     super.onPause(); 

     unregisterReceiver(mReceiver); 
    } 

C2DMReceiver을 C2DMReceiver이 C2DMBaseReceiver {

public C2DMReceiver() { 
    super("[email protected]"); 

    // TODO Load dynamic Gmail address 
} 

@Override 
public void onRegistrered(Context context, String registrationId) { 
    Log.i(Consts.LOG_TAG_SERVICE, registrationId); 

    // Store the registration id in the preferences 
    SharedPreferences settings = Prefs.get(context); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putString("deviceRegistrationID", registrationId); 
    editor.commit(); 

    // TODO: Send ID to server 
} 

@Override 
public void onUnregistered(Context context) { 
    Log.w(Consts.LOG_TAG_SERVICE, "got here!"); 
} 

@Override 
public void onError(Context context, String errorId) { 
    Log.w(Consts.LOG_TAG_SERVICE, errorId); 
} 

@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload")); 

    Intent broadcastIntent = new Intent(); 
    broadcastIntent.setAction("com.mydomain.myapp.NEWMESSAGE"); 
    broadcastIntent.putExtra("reading", intent.getStringExtra("payload")); 
    broadcastIntent.addCategory("com.mydomain.myapp.CONTEXT"); 
    context.sendBroadcast(broadcastIntent); 

} 

} 

확장 공용 클래스 이것은 내가 가진 전부입니다 :

좋아, 그래서 여기 내가 지금까지 무엇을 가지고 있습니다 그러나 나는 내 자신의 방송을 결코받지 못한다.

+0

설명을 위해'Log.w (Consts.LOG_TAG_SERVICE, "C2DMReceiver :"+ intent.getStringExtra ("payload"));'행에 도달 했습니까? – uvesten

+0

예! 내가 logcat을 사용하여 장치에 푸시 한 것을 볼 수 있습니다 – nino

+0

내 대답이 도움이 되었습니까? – uvesten

답변

3

이렇게해야합니다.

@Override 
protected void onMessage(Context context, Intent intent) { 

    Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload")); 

    Intent i = new Intent(context, YourMainActivity.class); 
    i.putExtra("reading", intent.getStringExtra("payload")); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 

} 

그런 다음 주 활동에서 onStart의 의도를 처리합니다. 활동이 이미 실행 중이면 기존 인스턴스가 처리하고, 그렇지 않으면 새 인스턴스가 시작됩니다.

+0

또는이 가이드에 따라 상태 표시 줄 알림을 만들어 사용자가 앱을 시작할 것인지 여부를 선택할 수 있습니다. 더 깨끗한 방법이 될 수 있습니다. http://developer.android.com/guide/topics/ui/notifiers/notifications.html – uvesten

관련 문제