android - 当用户点击通知时如何启动应用程序或将其置于前台点击android

标签 android android-notifications android-broadcast

我正在处理推送通知。 当用户单击通知时,我想启动应用程序(以防应用程序未启动),或者将其置于前面(以防应用程序启动)。 谢谢

最佳答案

这是我在这里找到答案的完整代码 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/

相关文章:

android - 按联系人分组查询最新短信

android - 为什么我们使用 TaskStackBuilder?

java - Android 广播接收器中的 EditText

android - 将 SMS 从 BroadcastReceiver 传递到 Activity

android - Android View 标记何时需要结束标记?

android - 如何在设备处于横向时拍摄人像照片

android - 位置更新后执行某些操作的最佳做​​法是什么?

android - NotificationCompat 无法解析为类型

android LocalBroadcastManager 和序列化

android - 收听新的日历事件