2017-10-28 83 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