Android O通知未显示

问题描述:

我正在尝试为Android O版本实现通知。 我已阅读关于通知管理员和渠道。所以Android O仍然不想重现通知。在PostCreate方法的主要Activity中,我写了这个。Android O通知未显示

NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    String CHANNEL_ID = "my_channel_01"; 
    String CHANNEL_NAME = "my Channel Name"; 
    int NOTIFICATION_ID = 1; 

    Intent notificationIntent = new Intent(this, MainActivity.class); 

    PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, 
      notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


    NotificationChannel notificationChannel = null; 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 

     notificationChannel = new NotificationChannel(CHANNEL_ID, 
       CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW); 
     notificationChannel.enableLights(true); 
     notificationChannel.setLightColor(Color.RED); 
     notificationChannel.setShowBadge(true); 
     notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); 

     mNotifyManager.createNotificationChannel(notificationChannel); 
    } 
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Notification myNotification = new NotificationCompat.Builder(MainActivity.this) 
      .setContentTitle("You have been notify") 
      .setContentText("This is your Notifiaction Text") 
      .setSmallIcon(R.drawable.ic_donut_large_black_24dp) 
      .setChannel(CHANNEL_ID) 
      .setPriority(NotificationCompat.PRIORITY_HIGH) 
      .setDefaults(NotificationCompat.DEFAULT_ALL) 
      .setContentIntent(notificationPendingIntent) 
      .setAutoCancel(true) 
      .setSound(alarmSound) 
      .build(); 

    mNotifyManager.notify(NOTIFICATION_ID, myNotification); 
    Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show(); 

建立第26 API不创建通知,吐司消息火灾和之后的日志说我:

W /通知:流类型提倡使用比音量控制等操作 W /通知:见setSound()的什么,而不是使用与android.media.AudioAttributes出线您的播放使用情况

如何处理这种情况下错误的文件?

Upd。经过一番调查后,我发现26 api在通知构建器中使用了一些变化。现在它也接受chanelid。因此,26个使用两个参数的构建器。

+0

的[通知不Android中ö显示出来尽管创建信道]可能的复制(https://*.com/questions/46255675/notification-not-showing-up -in-android-o-though-creating-a-channel) – Da2da2

在创建通知/ notificationcompact对象时传递通道ID。

Notification.Builder(Context context, String channelId) 

NotificationCompat.Builder(Context context, String channelId) 
+0

我这样做,但Toast消息触发,并且日志继续说我:“W /通知:对于音量控制以外的操作,不推荐使用流类型W/Notification:请参阅setSound()用什么来代替android.media.AudioAttributes来限定你的播放用例“ – eldes

您的alarmSound应该可能是别的东西,而不是URI?

+0

问题在于新的Android O通知。即使没有setSound也不会出现,并且日志中的警告保持不变。 – Oleksiy

+0

所以你低估了我的答案,因为这个? – Frank

+0

这不是我的投票 – Oleksiy

您不应在通知构建器上使用setSound方法,而应使用您创建的通知通道来设置这些设置。

notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);