Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 128 章 Notification 通知中心

目录

128.1. 文本通知
128.2. 添加点击操作

128.1. 文本通知

		
    private int createNotification(String title, String text) {
        String channelId = "channelId";
        String channelName = "channelName";
        String description = "description";
        String group = "group";
        int notificationId = new Random().nextInt(101);
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(description);
        notificationManagerCompat.createNotificationChannel(channel);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId)
                .setContentTitle(title).setContentText(text)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true).setGroup(group);

        notificationManagerCompat.notify(notificationId, notification.build());
        return notificationId;
    }
		
		

		
        CharSequence name = "test";
        String description = "test";
        NotificationChannel channel = new NotificationChannel("test", name, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(description);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
        Notification.Builder builder = new Notification.Builder(this, "test")
                .setContentTitle("XXX 门票打折")          //标题
                .setContentText("参与 XXX 领取 XXX")      //内容
                .setSubText("打折信息")                   //内容下面的一小段文字
                .setWhen(System.currentTimeMillis())     //设置通知时间
                .setSmallIcon(R.mipmap.ic_launcher)      //设置小图标
                .setAutoCancel(false);                   //设置点击后取消Notification

        notificationManager.notify(1, builder.build());