我正在使用惠普推送通知。 當用戶點擊通知時,我想要在應用程序未啓動的情況下啓動應用程序,或者在啓動應用程序時將其啓動。 謝謝如何啓動應用程序或當用戶點擊android通知時點擊前面
回答
只需設置通知的意圖,這在official API guide詳細介紹。
通過創建PendingIntent
來實現這一點。
例如,這將在單擊通知時啓動MainActivity。
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
實施pending intent
。
代碼
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
清單
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
我不知道你在談論谷歌雲端通訊與否。但是,如果這是一個正常的通知,那麼在創建通知時,您必須提供Pending Intent。只需將您想要的課程放入待定意圖中,以便當用戶單擊該通知時,您將被驅動到您所謂的應用程序。片段:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
之後,在創建通知時使用此意圖。它的方法是setContentIntent(「上面的意圖」)並通過NotificationManager激發你的通知!
需要添加以下標誌intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER);給啓動器意圖以啓動應用程序,並 – user1796624
感謝提醒! :) – thegiga
這是complite代碼,我在這裏找到了答案Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
重要的事情是對意圖的標誌,這將打開的應用程序,或者打開把它前面,或者什麼也不做您在瀏覽應用程序時點擊通知
- 1. 當用戶點擊Urban Airship通知時重新啓動android應用程序
- 2. 點擊通知,啓動應用程序在android
- 3. 當用戶點擊Android中的通知時顯示應用
- 4. 如何在Android中點擊圖標時啓動應用程序?
- 5. Android通知不斷啓動應用程序(即使我沒有點擊通知)?
- 6. 當點擊通知時重用當前應用實例
- 7. 當用戶點擊其圖標時,Android應用程序重新啓動
- 8. IBM Mobilefirst:當我點擊通知時,應用程序重新啓動
- 9. 如何驗證當前用戶,當他們點擊「啓動應用程序」從谷歌應用程序市場
- 10. Android GCM「背景」通知在點擊時不會啓動我的應用程序
- 11. 如何關閉當前打開的應用程序,當點擊推送通知
- 12. Android - 用戶點擊酒吧中的通知時如何不啓動活動
- 13. 檢查應用程序是否通過Toast通知面板啓動點擊
- 14. 當點擊應用程序時發出通知
- 15. 如何通過點擊推送通知來啓動應用程序
- 16. 確定何時點擊應用程序圖標啓動應用程序android
- 17. 在用戶點擊通知後完成當前活動
- 18. 如何在android中點擊url啓動應用程序
- 19. 如何在用戶點擊推送通知時禁用打開應用程序
- 20. 當用戶點擊應用程序中的發送時,IME如何知道?
- 21. Android Firebase推送通知在點擊後重新啓動應用程序
- 22. 在Android中,當用戶點擊應用程序A的通知時,如何啓動應用程序B的活動?
- 23. 當點擊通知時更新活動
- 24. 當點擊通知時恢復活動
- 25. 當用戶點擊來自鎖定屏幕的通知時啓動活動
- 26. 的Android程序啓動程序並點擊時無法啓動
- 27. Android:當我點擊推送通知時如何調用功能
- 28. 如果用戶沒有點擊通知,swift通知更新應用程序
- 29. 如何從外部應用程序如WhatsApp或Facebook點擊鏈接時啓動我的Android應用程序?
- 30. Android通知點擊
抱歉,這不起作用,它會從活動中創建新實例 – user1796624
然後您可以將PendingIntent.getActivity()更改爲PendingIntent.getBroadcast(),並使您的IntentReceiver接受這個意圖,並做你想做的任何事情。 – Mine