01.public class C2dm_BroadcastReceiver extends BroadcastReceiver {
02.
03.NotificationManager mNotiManager;
04.public static String registration_id = null;
05.static String c2dm_name = "";
06.static String c2dm_msg = "";
07.
08.
09.public void onReceive(Context context, Intent intent) {
10.
11.mNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
12.
13.if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
14.
15.handleRegistration(context, intent);
16.
17.
18.} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
19.
20.c2dm_name = intent.getExtras().getString("name");
21.c2dm_msg = intent.getExtras().getString("msg");
22.
23.System.out.println("c2dm_msg======>"+c2dm_msg);
24.
25.
26.Toast toast = Toast.makeText(context, c2dm_name+"님 으로부터의 메시지\n"+c2dm_msg, Toast.LENGTH_LONG );
27.toast.setGravity( Gravity.TOP | Gravity.CENTER, 0, 150 );
28.toast.show();
29.
30.Notification noti = new Notification(R.drawable.btn_notread, "쪽지가 왔어요~!", System.currentTimeMillis());
31.
32.noti.defaults |= Notification.DEFAULT_SOUND;
33.noti.defaults |= Notification.DEFAULT_VIBRATE;
34.
35.noti.flags |= Notification.FLAG_AUTO_CANCEL;
36.
37.mNotiManager.notify(1, noti);
38.}
39.}
40.
41.private void handleRegistration(Context context, Intent intent) {
42.
43.registration_id = intent.getStringExtra("registration_id");
44.
45.System.out.println("registration_id====>"+registration_id);
46.
47.if (intent.getStringExtra("error") != null) {
48.Log.v("C2DM_REGISTRATION",">>>>>" + "Registration failed, should try again later." + "<<<<<");
49.} else if (intent.getStringExtra("unregistered") != null) {
50.Log.v("C2DM_REGISTRATION",">>>>>" + "unregistration done, new messages from the authorized sender will be rejected" + "<<<<<");
51.} else if (registration_id != null) {
52.
53.SharedPreferences pref = context.getSharedPreferences("reg_id", 0);
54.SharedPreferences.Editor editor = pref.edit();
55.
56.editor.putString("reg_id", registration_id);
57.
58.editor.commit();
59.
60.System.out.println("registration_id complete!!");
61.}
62.}
63.
64.}
01.if(intent.getAction().equals(02."com.google.android.c2dm.intent.RECEIVE")) {03.c2dm_msg = intent.getExtras().getString("msg");04.Toast toast = Toast.makeText(context,"메시지 도착!\n"+ c2dm_msg,05.Toast.LENGTH_SHORT);06.toast.setGravity(Gravity.TOP | Gravity.CENTER,0,150);07.toast.show();08.09.nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);10.PendingIntent pendingIntent = PendingIntent.getActivity(11.context,0,newIntent(context, C2dmTest.class),0);12.String ticker = c2dm_msg;13.String title ="test";14.String text = c2dm_msg;15.16.//화면 켜짐 유무 체크17.KeyguardManager km = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);18.// Create Notification Object19.if(GetClassName(context).equals("c2.dm.C2dmTest") && !km.inKeyguardRestrictedInputMode()){}20.elseif(km.inKeyguardRestrictedInputMode()){21.22.PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);23.PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK ,"test");24.wl.acquire();25.Notification notification =newNotification(R.drawable.icon1,ticker, System.currentTimeMillis());26.notification.defaults |= (Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);27.notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT ;28.29.notification.setLatestEventInfo(context,title, text, pendingIntent);30.nm.notify(1234, notification);31.nm.wait(5000);32.wl.release();33.34.35.}형식으로 전 OnReceive 내에서 사용했습니다.
Context만 끌고오면 문제없이 사용할 수 있으실겁니다.