android BroadcastReceiver에서 notification 사용하기


 상태바에 노티하는것을 Broadcast Receiver에서 직접 처리하려고 했더니, extends Activity가 아니라 extends Broadcast receiver라서 그런지 


notification manager를 실행할 수가 없다는 에러가 나서 좀 고생했는데..


찾아보니.. 아래처럼 수정하면 된다.


mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 


 ======>


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



public class C2DMReceiver extends BroadcastReceiver
{
	Context mContext;
	String sMsg = "";
	private NotificationManager mNotificationManager;
	private static int ABOUT_NOTIFICATIONS = R.layout.notice_popup;
	
	
	@Override   
    public void onReceive(Context context, Intent intent)
    {
		mContext = context;
		if(C.D)Log.e(C.LOG_TAG, "onReceive");
        String action = intent.getAction();
 
        //서버에 정상적으로 등록이 완료되었을 때
        if (action.equals("com.google.android.c2dm.intent.REGISTRATION"))
        {
            handleRegistration(context, intent);
        }
        //서버에서 메시지가 도착했을 때
        else if (action.equals("com.google.android.c2dm.intent.RECEIVE"))
        {
            handleMessage(context, intent);
        }
    }
 
    private void handleRegistration(Context context, Intent intent)
    {
    	mContext = context;
    	if(C.D)Log.e(C.LOG_TAG, "handleRegistration");
        //서버에서 넘어오는 메시지의 내용에 key이름 "registration_id"에는 이 기기에만 사용하는 인증키값이 담겨서 넘어온다.
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null)
        {
            if(C.D)Log.e(C.LOG_TAG, "error");
        }
        else if (intent.getStringExtra("unregistered") != null)
        {
            if(C.D)Log.e(C.LOG_TAG, "unregistered");
        }
        else if (registration != null)
        {
            if(C.D)Log.e(C.LOG_TAG, "registration_id: "+registration);
            String sMsg = "C2DM 서버에 등록 성공, registration_id:"+registration;
            Toast.makeText(context, sMsg, Toast.LENGTH_LONG).show();
        }
    }
 
    private void handleMessage(Context context, Intent intent)
    {
    	mContext = context;

    	
    	sMsg = intent.getStringExtra("msg").toString();
        if(C.D)Log.e(C.LOG_TAG, "handleMessage 수신된 C2DM 메시지:"+sMsg);
        if(C.D)Toast.makeText(context, "수신된 C2DM 메시지:"+ sMsg, Toast.LENGTH_LONG).show();
        
        // Get the notification manager serivce.
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);   
        setDefault(Notification.DEFAULT_LIGHTS);  // 상태창에 출력하기		

    }
    
    private void setDefault(int defaults) {
        
        // This method sets the defaults on the notification before posting it.
        
        // This is who should be launched if the user selects our notification.
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,  new Intent(mContext, NotiList.class), 0);

        // In this sample, we'll use the same text for the ticker and the expanded notification
        //CharSequence text = getText(R.string.status_bar_notifications_message);

        final Notification notification = new Notification(
                R.drawable.noti_icon_new,       // the icon for the status bar
                sMsg,                        // the text to display in the ticker
                System.currentTimeMillis()); // the timestamp for the notification

        String sTitle = "메시지 제목";
        notification.setLatestEventInfo(
        		mContext,                        // the context to use
                sTitle,
                                             // the title for the notification
                sMsg,                        // the details to display in the notification
                contentIntent);              // the contentIntent (see above)

        notification.defaults = defaults;
        
        mNotificationManager.notify(
        		ABOUT_NOTIFICATIONS, // we use a string id because it is a unique
                                    // number.  we use it later to cancel the notification
                notification);
    } 			    

}

만약 노티 상태바에서 변수를 넘겨주고 싶다면..

    	int nReqCode = 999;	
	Intent i = new Intent(mContext, NotiList.class);
	Bundle extra = new Bundle();
	extra.putString("FROM", "RECEIVER");
	i.putExtras(extra);
		
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, nReqCode,  i, 0);
 
 


받는 NotiList.java 파일에서는..


         Intent intent = getIntent();
        String sMsg = intent.getStringExtra("FROM");  // 메시지
        if(sMsg != null){
        	if(sMsg.equals("RECEIVER")){	// 상태창에서 온경우  
        		bFromReceiver = true;
        	}else{
        		bFromReceiver = false;
        	}
        }else{
        	bFromReceiver = false;
        }
 
 


+ Recent posts