无法发布通知频道空日志?

时间:2019-06-22 04:56:58

标签: java android notifications android-8.0-oreo

朋友,我是android的初学者,我在项目中使用了一些GitHub代码,我的问题是我正在构建警报管理器应用程序,其通知在8.0 oreo设备以下完美运行,但问题在oreo上,通知给了我此异常< / p>

无法在null频道上发布通知

我知道我需要再创建一个频道,但是不知道如何为该频道编写代码,这是我的代码?

response not found

2 个答案:

答案 0 :(得分:1)

  public static final String NOTIFICATION_CHANNEL_ID = "10001";

        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
        {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

答案 1 :(得分:0)

您必须先创建通知通道(API 26+),然后将通道ID传递到NotificationCompat.Builder(context, channelId)(对于26以下的端口为null):

// Create the notification channel
String channelId = null;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    channelId = "YOUR_DESIRED_CHANNEL_ID";
    NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
    String channelName = "A user-visible name for the channel";
    if (channel == null) {    // if not created yet
        channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        // Setup your channel config as you wish
        channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE);
        // Create the channel
        notificationManager.createNotificationChannel(channel);
    }
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// Setup your notification and notify