我正在嘗試爲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個使用兩個參數的構建器。
的[通知不Android中ö顯示出來儘管創建信道]可能的複製(https://stackoverflow.com/questions/46255675/notification-not-showing-up -in-android-o-though-creating-a-channel) – Da2da2