Android开发——通知提示框效果

通知提示框效果


    通知提示可以说是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);
    }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jerry说前后端

请作者喝杯冰阔落~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值