android - 点击通知导航到不同的活动
问题描述
当用户单击通知时,我有这种情况,如果应用程序已打开/前台,我想将用户重定向到HomeActivity ,否则将用户重定向到SplashActivity,因为我在那里执行一些身份验证任务。那么,实现这一目标的最佳和正确方法是什么?
我知道有很多相关的问题,但我没有找到任何特定于我的用例的东西
解决方案
要根据您的逻辑将用户重定向到特定的活动,您可以使用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());
要检查您的应用程序是否在前台,请查看此帖子。
推荐阅读
- python-3.x - 如何使用单个索引和一系列索引从 numpy 数组中获取行
- python - Python DEAP 多处理示例
- javascript - 尽管使用 async/await 时使用 {new: true} 选项,Mongoose findOneAndUpdate 不返回更新的对象
- python-3.x - 如何通过获取另一个数据框的滚动 COLUMN 总计/总和来创建新的数据框?
- python - 如何从python中的文件中绘制大的Y值?
- python - os.system 和 mysql 恢复在 python unittest 中不起作用
- mysql - 如何根据一个id从多个mysql表中选择多个字段?
- python - 星号参数的类型提示
- java - 在 Spark 中运行现有的生产 Java 应用程序
- spring-boot - 从拥有超过 3 篇文章的用户中选择一个唯一的名称。春季数据,H2