8

如何在小米设备上以编程方式启用提醒通知?某些应用程序(例如 Telegram)默认启用此权限。

设置截图:https ://imgur.com/a/D48G8Mz

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel( ) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    channel.enableLights(true);
    channel.setLightColor(Color.RED);
    channel.enableVibration(true);
    channel.setSound(createNotificationSound(),
            new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build());
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);

    return CHANNEL_ID;
}

通知

    String channelId = "";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelId = createNotificationChannel();
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
            .setLargeIcon(icon)
            .setSmallIcon(R.mipmap.ic_mipmap_launcher)
            .setContentTitle("Title")
            .setTicker(getResources().getString(R.string.app_name))
            .setContentText("Text")
            .setSound(createNotificationSound(), AudioManager.STREAM_NOTIFICATION)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setChannelId(channelId)
            .setPriority(Notification.PRIORITY_HIGH);

    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    notificationBuilder.setStyle(inboxStyle);
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = notificationBuilder.build();
    notificationManager.notify(order.getId(), notification);`
4

0 回答 0