Android 通知8.0 全适配


/**
     * 创建通知
     * @param context
     * @param message
     */
    private static final String CHANNEL_ID = "hou2020";
    private static final String MyGroupId = "houGroupId2020";
    public static void createNotification(Context context, NotificationMessage message) {
        if (message == null) return;
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (nm == null) return;
        int notificationId = message.notificationId;
        String title = message.notificationTitle;
        String content = message.notificationContent;
        String largeIcon = message.notificationLargeIcon;
        String smallIcon = message.notificationSmallIcon;

//        Intent intent = new Intent(context, GuideActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(context, 12, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(title)
//                .setSubText(context.getResources().getString(R.string.app_name))
                .setContentText(content)
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
            NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup(MyGroupId, "自定义通知组");
            nm.createNotificationChannelGroup(notificationChannelGroup);
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "自定义通知", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setGroup(MyGroupId);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
//                notificationChannel.setSound("android.resource://包名/raw/铃声文件", null);    //设置自定义铃声
            nm.createNotificationChannel(notificationChannel);
        }
        DLog.d(TAG, "createNotification: " + notificationId);
        nm.notify(notificationId, builder.build());
    }

猜你喜欢

转载自blog.csdn.net/houdada_/article/details/108005730