Android - 通知渠道API> = 26无法正常运行

时间:2018-03-22 08:03:00

标签: android notifications android-8.0-oreo

我一直在努力使用API​​ 26及更高版本中引入的新NotificationChannels

我正在开发一个应用程序,可以选择是否在四种情况下收到通知:

  1. 声音和振动。
  2. 只有声音。
  3. 仅振动。
  4. 没有声音或震动,只是一个弹出窗口。
  5. 在所有情况下,我的应用程序会通过声音通知并振动我选择的任何内容。

    我的代码是:

    NotificationCompat.Builder builder;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(context, CHANNEL_ID);
            int importance;
            NotificationChannel channel;
    
            //Boolean for choosing Sound
            if(sound) {
                importance = NotificationManager.IMPORTANCE_DEFAULT;
            } else {
                importance = NotificationManager.IMPORTANCE_LOW;
            }
    
            channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
            channel.setDescription(CHANNEL_DESC);
    
            //Boolean for choosing Vibrate
            if(vibrate) {
                channel.enableVibration(true);
            } else {
                channel.enableVibration(false);
            }
    
            notificationManager.createNotificationChannel(channel);
        } else {
            builder = new NotificationCompat.Builder(context);
        }
    
        if(sound && vibrate) {
            //Sound and Vibrate
            builder.setDefaults(Notification.DEFAULT_ALL);
        } else if(sound && !vibrate) {
            //Sound
            builder.setDefaults(Notification.DEFAULT_SOUND);
        } else if(!sound && vibrate) {
            //Vibrate
            builder.setDefaults(Notification.DEFAULT_VIBRATE);
        } else if(!sound && !vibrate) {
            //None
            //Do nothing! just notification with no sound or vibration
        }
    
        builder.setSmallIcon(R.drawable.ic_logo)
                .setContentTitle(title)
                .setContentText(text)
                .setAutoCancel(true)
                .setOnlyAlertOnce(false)
                .setPriority(Notification.PRIORITY_MAX);
    

    此外,我每次运行应用时都会更改CHANNEL_ID,因此每次测试时都会获得一个新的频道ID,直到我找到解决方案。

    当然,它在API小于26时工作正常。

    谢谢你们,伙计们!

3 个答案:

答案 0 :(得分:6)

谢谢大家,

我设法通过为每个案例创建NotificationChannelBuilder来解决问题,并在满足条件时通知每个NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationCompat.Builder builder_all, builder_sound, builder_vibrate, builder_none; NotificationChannel channel_all = new NotificationChannel(CHANNEL_ID_ALL, CHANNEL_NAME_ALL, NotificationManager.IMPORTANCE_HIGH); channel_all.enableVibration(true); notificationManager.createNotificationChannel(channel_all); NotificationChannel channel_sound = new NotificationChannel(CHANNEL_ID_SOUND, CHANNEL_NAME_SOUND, NotificationManager.IMPORTANCE_HIGH); channel_sound.enableVibration(false); notificationManager.createNotificationChannel(channel_sound); NotificationChannel channel_vibrate = new NotificationChannel(CHANNEL_ID_VIBRATE, CHANNEL_NAME_VIBRATE, NotificationManager.IMPORTANCE_HIGH); channel_vibrate.setSound(null, null); channel_vibrate.enableVibration(true); notificationManager.createNotificationChannel(channel_vibrate); NotificationChannel channel_none = new NotificationChannel(CHANNEL_ID_NONE, CHANNEL_NAME_NONE, NotificationManager.IMPORTANCE_HIGH); channel_none.setSound(null, null); channel_none.enableVibration(false); notificationManager.createNotificationChannel(channel_none); //Boolean for Sound or Vibrate are chosen if(sound && vibrate) { builder_all = new NotificationCompat.Builder(context, CHANNEL_ID_ALL); builder_all.setSmallIcon(R.drawable.ic_logo) .setContentTitle(title) .setContentText(text) .setAutoCancel(true) .setOnlyAlertOnce(false); switch (transition) { case Geofence.GEOFENCE_TRANSITION_ENTER: builder_all.setSmallIcon(R.drawable.ic_entered_white); break; case Geofence.GEOFENCE_TRANSITION_EXIT: builder_all.setSmallIcon(R.drawable.ic_left_white); break; } notificationManager.notify(notificationID, builder_all.build()); } else if(sound && !vibrate) { builder_sound = new NotificationCompat.Builder(context, CHANNEL_ID_SOUND); builder_sound.setSmallIcon(R.drawable.ic_logo) .setContentTitle(title) .setContentText(text) .setAutoCancel(true) .setOnlyAlertOnce(false); switch (transition) { case Geofence.GEOFENCE_TRANSITION_ENTER: builder_sound.setSmallIcon(R.drawable.ic_entered_white); break; case Geofence.GEOFENCE_TRANSITION_EXIT: builder_sound.setSmallIcon(R.drawable.ic_left_white); break; } notificationManager.notify(notificationID, builder_sound.build()); } else if(!sound && vibrate) { builder_vibrate = new NotificationCompat.Builder(context, CHANNEL_ID_VIBRATE); builder_vibrate.setSmallIcon(R.drawable.ic_logo) .setContentTitle(title) .setContentText(text) .setAutoCancel(true) .setOnlyAlertOnce(false); switch (transition) { case Geofence.GEOFENCE_TRANSITION_ENTER: builder_vibrate.setSmallIcon(R.drawable.ic_entered_white); break; case Geofence.GEOFENCE_TRANSITION_EXIT: builder_vibrate.setSmallIcon(R.drawable.ic_left_white); break; } notificationManager.notify(notificationID, builder_vibrate.build()); } else if(!sound && !vibrate) { builder_none = new NotificationCompat.Builder(context, CHANNEL_ID_NONE); builder_none.setSmallIcon(R.drawable.ic_logo) .setContentTitle(title) .setContentText(text) .setAutoCancel(true) .setOnlyAlertOnce(false); switch (transition) { case Geofence.GEOFENCE_TRANSITION_ENTER: builder_none.setSmallIcon(R.drawable.ic_entered_white); break; case Geofence.GEOFENCE_TRANSITION_EXIT: builder_none.setSmallIcon(R.drawable.ic_left_white); break; } notificationManager.notify(notificationID, builder_none.build()); } } else { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); if(sound && vibrate) { //Sound and Vibrate builder.setDefaults(Notification.DEFAULT_ALL); } else if(sound && !vibrate) { //Sound builder.setDefaults(Notification.DEFAULT_SOUND); } else if(!sound && vibrate) { //Vibrate builder.setDefaults(Notification.DEFAULT_VIBRATE); } else if(!sound && !vibrate) { //None //Do nothing! just notification with no sound or vibration } builder.setSmallIcon(R.drawable.ic_logo) .setContentTitle(title) .setContentText(text) .setAutoCancel(true) .setOnlyAlertOnce(false) .setPriority(Notification.PRIORITY_MAX); switch (transition) { case Geofence.GEOFENCE_TRANSITION_ENTER: builder.setSmallIcon(R.drawable.ic_entered_white); break; case Geofence.GEOFENCE_TRANSITION_EXIT: builder.setSmallIcon(R.drawable.ic_left_white); break; } notificationManager.notify(notificationID, builder.build()); }

我不知道这是否是最佳做法,但如果有人对此感到满意,我会尝试稍后优化代码。但它现在工作得很好。

这是我的代码:

insert into errorLogs (packageName, TaskName, [DateTime], ErrorCode, ErrorMessage)
values ('?', '?', GetDate(), '?', '?')

答案 1 :(得分:4)

我在文档中找到了这个。可能它会帮助你:

  

在Android 8.0(API级别26)及更高版本上,通知的重要性取决于通知发布到的频道的重要性。用户可以在系统设置中更改通知通道的重要性(图12)。在Android 7.1(API级别25)及以下,每个通知的重要性由通知的优先级决定。

还有:

  

Android O引入了通知渠道,以提供统一的系统来帮助用户管理通知。当您定位Android O时,您必须实施一个或多个通知渠道以向您的用户显示通知。如果您不定位Android O,则在Android O设备上运行时,您的应用与Android 7.0上的应用相同。

最后:

  
      
  • 现在必须将个别通知放入特定频道。
  •   
  • 用户现在可以关闭每个频道的通知,而不是关闭来自应用的所有通知。
  •   
  • 有效通知的应用在主页/启动器屏幕上的应用图标上显示通知“徽章”。
  •   
  • 用户现在可以暂停来自抽屉的通知。您可以为通知设置自动超时。
  •   
  • 有关通知行为的API已从Notification转移到NotificationChannel。例如,对于Android 8.0及更高版本,请使用NotificationChannel.setImportance()而不是NotificationCompat.Builder.setPriority()。
  •   

答案 2 :(得分:0)

如果您的声音和振动来自应用程序设置,请注意,这样做的目的是您应从应用程序中删除这些设置,并将用户转到频道设置:

”创建通知频道后,您无法以编程方式更改通知频道的视觉和听觉行为,只有用户才能从系统设置中更改频道行为。要使您的用户轻松访问这些通知设置,应添加应用程序的设置用户界面中的一个项,可以打开这些系统设置。”

https://developer.android.com/training/notify-user/channels#UpdateChannel