java - 我的通知代码突然停止工作
问题描述
我昨天有一个工作正常的通知,通知没有出现,我不记得我触摸过代码。当我获得所需的值时必须出现该通知,并且必须打开一个我们用户触摸它的弹出对话框。
有人可以帮忙吗?
通知代码-在服务中-:
Intent notifyIntent = new Intent(context.getApplicationContext(), PopUp.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//how I tried to pass data
notifyIntent.putExtra("text1", text1);
notifyIntent.putExtra("text2", text2);
PendingIntent pIntent = PendingIntent.getActivity(context, 1, notifyIntent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new NotificationCompat.Builder(context,CHANNEL_ID2)
.setContentTitle("check this")
.setSmallIcon(R.drawable.ic_delete)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
我在同一个服务类中使用 android studio emulator API 30 我有一个通知,它似乎表明该服务正在后台工作,除了我的通知,但它上面的代码突然没有工作
不知道是什么问题,请帮忙
解决方案
使用 notify 发出通知时,notificationId 必须是每个通知的唯一 int。更新您的代码以包含随机唯一的 notificationId
见下文
//you can use the timestamp
int notificationId = Integer.parseInt(String.valueOf(System.currentTimeMillis()));
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, n);
推荐阅读
- c++ - C++:setw() 只在第一行工作,在循环中
- jquery - Jquery:基于行跨长度的新类添加问题
- java - 将 onTouch 事件中文本的背景颜色更改为相应的标签
- ios - UICollectionViewCell 在 UICollectionViewReusableView 宽度之后开始
- nginx - nginx 两个位置相同的后端但不同的端口
- python - 调整图像大小使其变形无法识别
- ios - iOS Per-App VPN 未连接到 Internet
- airflow - 气流:ValueError:无法配置处理程序“处理器”-wasb 记录器
- java - 当键已经存在于地图中时,将值推送到地图
- python - 通过将列表索引视为python中的键将dict中的所有列表转换为dict