2017-09-01 147 views
2

我正在嘗試爲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://stackoverflow.com/questions/46255675/notification-not-showing-up -in-android-o-though-creating-a-channel) – Da2da2

回答

4

在創建通知/ notificationcompact對象時傳遞通道ID。

Notification.Builder(Context context, String channelId) 

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

我這樣做,但Toast消息觸發,並且日誌繼續說我:「W /通知:對於音量控制以外的操作,不推薦使用流類型W/Notification:請參閱setSound()用什麼來代替android.media.AudioAttributes來限定你的播放用例「 – eldes

-2

您的alarmSound應該可能是別的東西,而不是URI?

+0

問題在於新的Android O通知。即使沒有setSound也不會出現,並且日誌中的警告保持不變。 – Oleksiy

+0

所以你低估了我的答案,因爲這個? – Frank

+0

這不是我的投票 – Oleksiy

2

您不應在通知構建器上使用setSound方法,而應使用您創建的通知通道來設置這些設置。

notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);