7

兩個日誌示出無法發佈關於信道通知「空」目標API是26

1:流類型的使用是不推薦用於其它操作比音量控制

2:見setSound的文檔()對於而不是用什麼與android.media.AudioAttributes出線您的播放使用情況

Showing this

+0

你仍然有這個問題的問題? –

+0

此處的文檔https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels –

+0

@Mohit Singh:如果有正確的答案,您應該將其標記爲這樣。 –

回答

0

當您定位Android 8.0(API級別26)時,您必須實現一個或多個通知通道以向用戶顯示通知。

int NOTIFICATION_ID = 234; 

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 


     String CHANNEL_ID = "my_channel_01"; 
     CharSequence name = "my_channel"; 
     String Description = "This is my channel"; 
     int importance = NotificationManager.IMPORTANCE_HIGH; 
     NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); 
     mChannel.setDescription(Description); 
     mChannel.enableLights(true); 
     mChannel.setLightColor(Color.RED); 
     mChannel.enableVibration(true); 
     mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
     mChannel.setShowBadge(false); 
     notificationManager.createNotificationChannel(mChannel); 
    } 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(message); 

    Intent resultIntent = new Intent(ctx, MainActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx); 
    stackBuilder.addParentStack(MainActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(resultPendingIntent); 

    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
0

您可以通過兩種方式解決這個問題,但對他們倆的,你需要創建一個帶有specifi通知通道c頻道ID。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
String id = "my_channel_01"; 
int importance = NotificationManager.IMPORTANCE_LOW; 
NotificationChannel mChannel = new NotificationChannel(id, name,importance); 
mChannel.enableLights(true); 
mNotificationManager.createNotificationChannel(mChannel); 

第一種方法是在構造函數通知中設置通道:

Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title"); 
mNotificationManager.notify("your_notification_id", notification); 

第二種方法是Notificiation.Builder.setChannelId()

Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title"). 
setChannelId(id); 
mNotificationManager.notify("your_notification_id", notification); 

希望這有助於設置通道

1

首先創建通知通道:

public static final String NOTIFICATION_CHANNEL_ID = "4655"; 
//Notification Channel 
     CharSequence channelName = NOTIFICATION_CHANNEL_NAME; 
     int importance = NotificationManager.IMPORTANCE_LOW; 
     NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance); 
     notificationChannel.enableLights(true); 
     notificationChannel.setLightColor(Color.RED); 
     notificationChannel.enableVibration(true); 
     notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 


NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.createNotificationChannel(notificationChannel); 

然後使用信道ID在構造函數中:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setSmallIcon(R.drawable.ic_timers) 
       .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) 
       .setSound(null) 
       .setContent(contentView) 
       .setPriority(NotificationCompat.PRIORITY_DEFAULT) 
       .setLargeIcon(picture) 
       .setTicker(sTimer) 
       .setContentIntent(timerListIntent) 
       .setAutoCancel(false); 
3

古勒扎爾銖的答案是完美的工作,如果您的最低API是奧利奧。但是,如果您的最小值較低,則必須在平臺級別檢查中包裝NotificationChannel代碼。之後,你仍然可以使用的ID,將被忽略的預奧利奧:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
    int importance = NotificationManager.IMPORTANCE_LOW; 
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance); 
    notificationChannel.enableLights(true); 
    notificationChannel.setLightColor(Color.RED); 
    notificationChannel.enableVibration(true); 
    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
} 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); 

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build()); 
+1

好點。但是你忘記了NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel);'? –

+0

當android.os.Build.VERSION.SDK_INT

+0

@DonHatch這個頻道已經在上面創建了,我補充說明了其餘部分,但是和以前一樣。使用NotificationCompat的重點在於,您可以在所有API級別使用相同的格式,所以它不會崩潰。它會爲您製作正確的通知格式,包含或不包含頻道。 – Herrbert74

0

如果你得到這個錯誤應該注意的2項,並將其順序爲:

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

另外NotificationManager notifManager和NotificationChannel mChannel只創建一次。在API級別> = 26的構造函數.Builder(this)中,不贊成使用setChannelId(id)。

如果您在API級別> = 26構造函數中使用了.Builder(this),那麼builder.setChannelId(id)對於Android 8 Oreo API 26及更高版本是必需的。

有需要在Android 8奧利奧API 26和隨後通知制定者:

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

參見On Android 8 Oreo API 26 and later notification does not display的例子。