我正在处理推送通知。 当用户单击通知时,我想启动应用程序(以防应用程序未启动),或者将其置于前面(以防应用程序启动)。 谢谢
最佳答案
这是我在这里找到答案的完整代码 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);
重要的是 Intent 上的标志,这将打开应用程序或将其置于最前面(如果打开),或者如果您在浏览应用程序时单击通知,则不执行任何操作
关于android - 当用户点击通知时如何启动应用程序或将其置于前台点击android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17548658/