首页 > 解决方案 > 点击通知导航到不同的活动

问题描述

当用户单击通知时,我有这种情况,如果应用程序已打开/前台,我想将用户重定向到HomeActivity ,否则将用户重定向到SplashActivity,因为我在那里执行一些身份验证任务。那么,实现这一目标的最佳和正确方法是什么?

我知道有很多相关的问题,但我没有找到任何特定于我的用例的东西

标签: androidpush-notificationnotifications

解决方案


要根据您的逻辑将用户重定向到特定的活动,您可以使用PendingIntent

要检查工作示例,请单击此处

或者

在按钮单击时尝试下面的代码。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.stackoverflow.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("Notifications Title");
    builder.setContentText("Your notification content here.");
    builder.setSubText("Tap to view the website.");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());

要检查您的应用程序是否在前台,请查看此帖子


推荐阅读