2017-10-28 84 views
7

我得到吐司上的Android 8.1 API 27:爲包 「my_package_name」
未能張貼通知在Android 8.1 API 27通知不顯示

開發者警告...

Logcat包括下一個字符串:

通知:流類型的使用已棄用於其他操作,其他 比volume con TROL

W /通知:否通道發現PKG = my_package_name:什麼用 不是與android.media.AudioAttributes出線您的播放 使用情況

E/NotificationService見setSound()的文檔

Toast和Logcat中的完整信息可以幫助本地化這個問題。

+0

郵政代碼請 –

+0

的可能的複製[通知失敗中的Android奧利奧顯示(API 26)](HTTPS:/ /stackoverflow.com/questions/45395669/notifications-fail-to-display-in-android-oreo-api-26) –

回答

12

如果你得到這個錯誤應該注意的2項,並命令他們:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(this, id);

而且NotificationManager notifManager和NotificationChannel mChannel創建只有一次。和setChannelId(ID)所需的在API級別> = 26構造.Builder棄用(本)。

如果在API含量使用棄用> = 26構造.Builder(本)然後builder.setChannelId(ID)是必需的Android 8奧利奧API 26和更高版本。

有所需的通知設置器在Android 8奧利奧API 26和更高版本:

  • builder.setContentTitle()//需要
  • .setSmallIcon()//需要
  • .setContentText() //需要
  • .setChannelId(ID)//用於棄用API水平所需> = 26構造.Builder(本)

例如:然而

 private NotificationManager notifManager; 

     public void createNotification(String aMessage) { 
      final int NOTIFY_ID = 1002; 

      // There are hardcoding only for show it's just strings 
      String name = "my_package_channel"; 
      String id = "my_package_channel_1"; // The user-visible name of the channel. 
      String description = "my_package_first_channel"; // The user-visible description of the channel. 

      Intent intent; 
      PendingIntent pendingIntent; 
      NotificationCompat.Builder builder; 

      if (notifManager == null) { 
       notifManager = 
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      } 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
       int importance = NotificationManager.IMPORTANCE_HIGH; 
       NotificationChannel mChannel = notifManager.getNotificationChannel(id); 
       if (mChannel == null) { 
        mChannel = new NotificationChannel(id, name, importance); 
        mChannel.setDescription(description); 
        mChannel.enableVibration(true); 
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
        notifManager.createNotificationChannel(mChannel); 
       } 
       builder = new NotificationCompat.Builder(this, id); 

       intent = new Intent(this, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

       builder.setContentTitle(aMessage) // required 
         .setSmallIcon(android.R.drawable.ic_popup_reminder) // required 
         .setContentText(this.getString(R.string.app_name)) // required   
         .setDefaults(Notification.DEFAULT_ALL) 
         .setAutoCancel(true) 
         .setContentIntent(pendingIntent) 
         .setTicker(aMessage) 
         .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
      } else { 

       builder = new NotificationCompat.Builder(this); 

       intent = new Intent(this, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

       builder.setContentTitle(aMessage)       // required 
         .setSmallIcon(android.R.drawable.ic_popup_reminder) // required 
         .setContentText(this.getString(R.string.app_name)) // required 
         .setDefaults(Notification.DEFAULT_ALL) 
         .setAutoCancel(true) 
         .setContentIntent(pendingIntent) 
         .setTicker(aMessage) 
         .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) 
         .setPriority(Notification.PRIORITY_HIGH); 
      } // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 

      Notification notification = builder.build(); 
      notifManager.notify(NOTIFY_ID, notification); 
} 
+0

*我已設置頻道ID,但通知仍未顯示; *最後我發現我的問題是沒有調用「setContentText()」方法 *這真的幫助我,你提到「必需的setters」! – Allen

1

安迪的回答是工作,我想,以避免過時生成器,並按照FireBase Official Document。我剛添加的代碼之前,從經理通知。

String channelId = "default_channel_id"; 
String channelDescription = "Default Channel"; 
//Check if notification channel exists and if not create one 
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
    NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId); 
    if (notificationChannel == null) { 
     int importance = NotificationManager.IMPORTANCE_HIGH; 
     notificationChannel = new NotificationChannel(channelId, channelDescription, importance); 
     notificationChannel.setLightColor(Color.GREEN); 
     notificationChannel.enableVibration(true); 
     notificationManager.createNotificationChannel(notificationChannel); 
    } 
} 

//notificationManager.notify as usual 
0

我已設置通道ID,但通知仍未顯示。

終於讓我找到我的問題是有沒有援引「setContentText()」方法。

@Andy Sander提到「必需安裝程序」真的對我有幫助!

這裏所必需的通知設置器在Android 8奧利奧API 26和更高版本:

builder.setContentTitle() // required 
.setSmallIcon() // required 
.setContentText() // required 
.setChannelId(id) // required for deprecated in API level >= 26 constructor .Builder(this) 
+0

文件有時不夠具體。 –

+0

是的,它花費我很多時間。 @filthy_wizard – Allen