此代码将在屏幕顶部的android系统栏中生成通知。此代码将创建一个新的意图,该意图将在单击顶部栏中的通知后将用户定向到“
Home.class”。如果您希望它根据当前活动执行特定的操作,则可以将广播请求从GCMIntentService发送到您的其他活动。
Intent notificationIntent=new Intent(context, Home.class);generateNotification(context, message, notificationIntent);private static void generateNotification(Context context, String message, Intent notificationIntent) { int icon = R.drawable.icon; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification);}请注意,此示例使用R.drawable和R.String中的资源,这些资源必须存在才能正常工作,但是应该可以给您带来启发。有关状态通知的更多信息,请参见http://developer.android.com/guide/topics/ui/notifiers/index.html,以及有关广播接收的信息。http://developer.android.com/reference/android/content/BroadcastReceiver.html



