我在我的Android應用程序中執行由GCM觸發的推送通知。我想要顯示通知,如果應用程序正在警報框中運行,如果應用程序未運行意味着不在前臺只顯示狀態欄通知,您如何確定應用程序是否正在運行並且位於前臺?這是可能顯示這樣的通知。現在我正在使用此代碼顯示狀態欄推送通知當應用程序在前臺運行時在警告框中顯示推送通知
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, BottomActivity.class);
// 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, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
,如果這是因爲它是過時和谷歌說:「C2DM將不接受新用戶,你應該使用GCM,而不是C2DM一個新的項目,它將不授予新配額「 – NickT 2013-03-22 15:34:48
好的,謝謝,我將使用GCM並編輯我的問題 – 2013-03-22 15:38:40
@Jithu我也需要在我的一個應用程序中做同樣的事情,您是否找到了適當的解決方案和思路在此發佈相同,這將是一個很好的幫助謝謝。 – 2015-09-03 04:14:00