2011-08-08 4 views
0

푸시 알림 메시지를 표시하는 응용 프로그램을 개발 중입니다. 토스트 메시지를 사용하여 메시지를 표시하려고 할 때 모든 상황에서 올바르게 작동합니다. 하지만 이러한 푸시 알림에 StatusBarNotifications을 사용하고 싶습니다. 앱이 실행 중일 때 제대로 작동합니다. 종료 한 후 장치를 다시 시작하면 상태 표시 줄 알림이 표시되지 않습니다. 앱이 강제 종료 될 때도 마찬가지입니다.응용 프로그램이 실행되고 있지 않을 때 onReceive 함수의 컨텍스트가 제대로 작동하지 않습니다.

이 문제를 어떻게 해결할 수 있습니까? 다음

코드입니다 :

public void onReceive(Context context, Intent intent) 
{ 
    if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) 
    { 
    handleMessage(context, intent); 
    } 
} 
private void handleMessage(Context context, Intent intent) 
{ 
    String message= intent.getStringExtra("msg"); 
    Toast.makeText(context.getApplicationContext(),"\n message : "+message,1).show(); 
    NotificationManager objNotfManager=(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
    int icon = R.drawable.logo; 
    CharSequence tickerMessage = message; 
    long when= System.currentTimeMillis(); 
    Notification objNotf = new Notification(icon,tickerMessage,when); 
    CharSequence title = "New message from "+message; 
    CharSequence mesage = "You have "+number+" unread messages"; 
    Intent NotifIntent = new Intent(context.getApplicationContext(),TabContainer.class); 
    NotifIntent.putExtra("message",message); 
    PendingIntent contentIntent = PendingIntent.getActivity( context.getApplicationContext(), 0, NotifIntent, 0); 
    objNotf.setLatestEventInfo( context.getApplicationContext(), title, mesage, contentIntent); 
      objNotfManager.notify(1,objNotf); 
} 

은 이전에 내가 컨텍스트를 사용하지만, 다른 위젯, 다른 토스트 작동하지되었다. 그래서 context.getApplicationContext()을 사용할 계획이었습니다.

+0

참조 http://stackoverflow.com/questions/6921464/android-context-has-some-problem-when-displaying-push-notification-using-c2dm/8974369#8974369 –

답변

1

TL; DR : 대신 getApplicationContext()

(세미) 자세한 답변 getBaseContext()를 사용 : 나는 오늘 같은 문제를 겪고로

나는 당신의 문제에 대한 답을 알아 냈어. 문제는 응용 프로그램을 강제 종료/다시 시작한 후 작업 관리자에 있지 않을 때 getApplicationContext()이 제대로 초기화되지 않았기 때문입니다. 알림을 생성하기 위해 알림 관리자를 검색 할 때 잘못된 참조를 사용하게됩니다.

handleMessage 메서드에 전달 된 ContextgetApplicationContext()에 의해 제공 될 가능성이 큽니다.

응용 프로그램이 무엇 내가 제안하는 푸시 알림 의도를 얻는 곳으로 방법을 통해 다시 최대한 추적하는 것입니다 getBaseContext()

으로 검색 할 수있는 살아 있지 않을 때 당신에게 사용할 수있는 컨텍스트 handleMessage 메소드에 전달 된 을 변경하고이를 Context context = getBaseContext();으로 대체 한 다음 메소드 내에서 context 매개 변수를 사용할 수 있습니다.

관련 문제