java - 来自通知点击的用户
问题描述
如果用户来自通知点击,我想触发 Toast。
但我不知道如何知道用户是否来自通知点击。知道我这样设置通知:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel =
new NotificationChannel(notification_channel, notification_channel, importance);
String description = "A channel which shows notifications about the app";
channel.setDescription(description);
channel.setLightColor(Color.MAGENTA);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().
getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
String notificationTitle = "Title";
String notificationText = "Notification Text";
Intent intent_not = new Intent(getApplicationContext(), Home_Base.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent_not, FLAG_UPDATE_CURRENT);
//build the notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext(), notification_channel)
.setSmallIcon(R.drawable.fiamma)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
//trigger the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
notificationManager.notify(123, notificationBuilder.build());
解决方案
尝试使用 intent_not.setAction() 设置操作(例如 On_NOTIFICATION_CLICK)
在 Home_Base 中使用 getIntent().getAction(),尝试从接收到的意图中获取操作。
如果 Home_Base 中收到的操作是 On_NOTIFICATION_CLICK,则它是通知单击操作。
推荐阅读
- python - 从 python 调用 Matlab 的 ODE45
- javascript - 将不可变数组从字符串转换为数字
- three.js - 将纹理更改为 glb 文件
- dialogflow-es - 在 Dialogflow 中添加一段时间后数据丢失 是存储问题还是服务器问题?
- terraform - Terraform 远程执行配置程序失败并显示“bash:权限被拒绝”
- java - Java:解决未经检查的调用
- jquery - 从弹出表单返回字符串值
- matplotlib - 带总和的绘图函数
- python - 聚合管道在直接使用时有效,但在通过 Python 传递时无效
- python - 在 Windows 10 中创建 conda 环境时出错