通知提示框效果
通知提示可以说是Android开发的一大特色,能够让软件给用户传递的消息更加显眼。使用代码比较固定,于是直接给出代码:
//为了增强定制化效果,可以通过增加参数实现,其实就和微信小程序的Toast的提示功能差不多
public void noticeInfo(String topic, String msg) {
//由于我这里使用的JSON信息,故而多了一些关于String转成JSON数据的处理。
//一般msg主要是string类型
JsonObject newsInfo = new JsonParser().parse(msg).getAsJsonObject();
String notice_title = newsInfo.get("title").toString().replace("\"", "");
String news_id = newsInfo.get("news_id").toString();
String department_name = newsInfo.get("department_name").toString().replace("\"", "");
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//处理版本兼容问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel manager = new NotificationChannel("campus_notice", "测试通知", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(manager);
}
//点击跳转到对应的app。点击通知,页面的预调转处理
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
int notifyId = (int) System.currentTimeMillis();
//利用bundle实现页面的参数传递,这是Android页面信息传递的主要方式之一
//类似于小程序的options页面传参方式。
Bundle bundle = new Bundle();
bundle.putString("news_id", news_id);
bundle.putBoolean("need_read", true);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//实现通知栏的定制化
Notification notification = new NotificationCompat.Builder(MainActivity.this, "campus_notice")
.setContentText(department_name + "~")
.setContentText(notice_title)
.setSmallIcon(R.drawable.campus_notice)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
notificationManager.notify(notifyId, notification);
}