我有GCM通知實施。我知道客戶端應用程序收到的通知是處於前景,背景還是死亡狀態。我想知道的是,當應用程序處於死亡狀態時,如何啓動收到通知的應用程序?當收到GCM通知時啓動應用程序
回答
在郵件接收器,我請執行下列操作:
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
這裏與入門活性更換YourActivity.class。這對我有效。
您可以使用NotificationManager啓動您的活動。
嘗試下面的代碼裏面使用您的的onMessage()方法這是重載方法在擴展GCMBaseIntentService類GCM的類。
int icon = R.drawable.your_app_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);
Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// notificationIntent.putExtra("PostName", vPostText);
// Log.i(TAG, "Sent Postid " + postid);
// Util util = (Util) context.getApplicationContext();
// util.setPostID(postid);
// util.setNotify(true);
// util.setUserNAME(vPortCode);
// util.setPostNAME(vPostText);
// util.setmEDIA(vMedia);
// util.setmEDIATHHUMB(vMediaThumb);
// util.setmEDIATYPE(vMediaType);
// util.setAirportName(vAirportName);
notificationIntent.putExtra("Set_image", true);
notificationIntent.putExtra("Notify", true);
// set intent so it does not start a new activity
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context,
(int) System.nanoTime(), notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify((int) System.nanoTime(), notification);
謝謝。我知道如何從應用程序開始一個活動。但是,我想要做的是,收到通知後,如果我的應用程序在後臺,我會將其帶到前臺, 如果它在前臺,那麼我將繼續處理,並且如果我的應用程序處於停止/死亡狀態,然後我想「重新啓動應用程序」而不是活動。 那麼你能告訴我,當應用程序處於收到通知的停止/死亡狀態時,是否有任何方式啓動應用程序? – jasdmystery
@jasdmystery:你找到了嗎?我面臨同樣的問題。 – user836026
請參閱上面的答案。 – jasdmystery
- 1. 設備上收到的GCM通知未啓動應用程序
- 2. 當它接收到遠程通知時自動啓動iPhone應用程序
- 3. 在應用程序未運行時收到GCM Android通知
- 4. Pushover通知收到時啓動應用程序
- 5. 收到通知時重新啓動應用程序
- 6. 失敗嘗試接收GCM通知時,應用程序空轉
- 7. 如何在收到gcm通知時啓動對話框?
- 8. GCM + AppEngine - 應用程序不會收到推送通知
- 9. 在前臺應用程序未收到GCM通知
- 10. 無法收到我的Android應用程序後GCM通知
- 11. 遠程通知(GCM)到Xamarin.Android(MonoDroid的),當應用程序退出
- 12. 當應用程序遇到無限循環時收到通知?
- 13. IOS收到推送通知,從谷歌GCM當應用程序不迅速
- 14. 當應用程序ios關機時,GCM不發送通知
- 15. iOS應用程序僅通過主題接收GCM通知
- 16. 如何在收到通知[FCM]時自動啓動android應用程序?
- 17. 當應用程序關閉時收到通知
- 18. 當收到多個推送通知時,應用程序凍結
- 19. 當應用程序死亡時收到的iOS無聲通知
- 20. 當應用程序在前臺時未收到推送通知
- 21. 當我離開應用程序時沒有收到GCM消息
- 22. 當通知托盤按下推送通知時,Android GCM崩潰應用程序
- 23. 未接收到GCM通知
- 24. 在設備上收到GCM推送通知時彈出Android應用程序
- 25. Android GCM「背景」通知在點擊時不會啓動我的應用程序
- 26. 在Web應用程序上收到SMS時收到通知
- 27. GCM - 從通知打開應用程序?
- 28. 如何停止在android中每次啓動應用程序時收到通知?
- 29. 第一次啓動第三方應用程序時會收到通知
- 30. 當收到推送通知時自動打開供應商應用程序(自動'按'通知)
如果應用程序已經運行,該怎麼辦? – Ahmed
如果應用程序從任務列表中刪除,該怎麼辦?在這種情況下,「消息接收者」將不會被調用。它將按系統處理,並由「click_action」標籤點擊。但是我的擔心是在通知到達後打開我的目標活動。 –
如何在fcm消息中獲取上下文? –