android 8.0 notifacation

private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationManager notificationManager = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);

            //分組(可選)
            //groupId要惟一
            String groupId = "group_001";
            NotificationChannelGroup group = new NotificationChannelGroup(groupId, "廣告");

            //建立group
            notificationManager.createNotificationChannelGroup(group);

            //channelId要惟一
            String channelId = "channel_001";

            NotificationChannel adChannel = new NotificationChannel(channelId,
                    "推廣信息", NotificationManager.IMPORTANCE_DEFAULT);
            //補充channel的含義(可選)
            adChannel.setDescription("推廣信息");
            //將渠道添加進組(先建立組才能添加)
            adChannel.setGroup(groupId);
            //建立channel
            notificationManager.createNotificationChannel(adChannel);

			//建立通知時,標記你的渠道id
            Notification notification = new Notification.Builder(MainActivity.this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setContentTitle("一條新通知")
                    .setContentText("這是一條測試消息")
                    .setAutoCancel(true)
                    .build();
            notificationManager.notify(1, notification);

        }
    }

複製代碼

相關文章
相關標籤/搜索