我有一个在线 JSON 文件,显示最新的国家地震,我的应用程序中的 listView 适配器从该 JSON 文件接收数据,并且通过用户滑动手动刷新,但现在我添加自动及时的基础刷新模式到我的列表,问题是: 检测到新地震后如何创建系统通知?
最佳答案
编写以下代码,以便在从网络请求加载数据时在您的应用中发送系统通知。
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("App Name / Title Message")
.setContentText("Description Message hat data is updated")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(getResources().getColor(R.color.icon_color))
.setPriority(Notification.PRIORITY_HIGH) // will show notification even you are in app
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS;
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
//you can use unique identification int number for notificationID instead of 1
放置此代码块将显示通知。
关于android - listview 从在线 JSON 文件收到新数据后如何创建通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423908/