そのため、アプリをOreoに対応させ、通知に関する問題に直面しています。
ドキュメントに従って通知チャネルを追加しましたが、すべての投稿で通知が鳴り続ける以外はすべてスムーズに動作し、デフォルトも0に設定してみました。
私はエミュレータでアプリをテストしていますが、どんな助けも大歓迎です。
このコードを使用してチャネルを作成しました
NotificationCompat.Builder builder = new NotificationCompat.Builder(PlayerService.this, "channel_01")
.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setContent(viewsSmall)
.setCustomBigContentView(viewsExpanded)
.setDeleteIntent(pSwipeToDismiss);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Lollipop) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
builder.setPriority(Notification.PRIORITY_MAX);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
NotificationChannel channel = new NotificationChannel("channel_01",
"Playback Notification",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("channel_01");
}
final Notification notification = builder.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
通知チャンネルの設定を確認します(通知をスワイプし、その下にある設定アイコンを押してから、チャンネルを選択します)。これらの設定は、最初にチャネルを作成するときに設定され、デバイスで手動で行わない限り変更されません(少なくとも、アプリのアンインストールと再インストールで、デフォルトでどの設定が表示されるかを確認できます)。
基本的に、channel.setSound(null, null)
は、新規インストールでチャネルを作成する場合にのみ有効です。それは、彼らが 公式ガイド で説明しようとしていることかもしれません:
元の値で既存の通知チャネルを作成しようとしても、操作は実行されません
そのガイドに従って、_NotificationManager.IMPORTANCE_HIGH
_を設定しようとしてchannel.setSound(null, null)
を使用しなかった場合、チャンネルはデフォルトのサウンドで重要度レベル_Urgent Make sound and pop on screen
_を取得します。
^ベンジャミンの答えは機能しますが、彼はいくつかの重要な詳細を見逃しています!コードを調整するたびにチャンネルIDを変更する必要があります。変更しないと、Oreoは変更を行いません。理由はわかりません。
以下の私のコードと、これで変更を行う必要がある場所を見ることができます<-------こちら
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelID = "My Channel I"; <-------here
String appName = mContext.getResources().getString(R.string.app_name);
NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(mContext );
notificationCompatBuilder
.setOngoing(true)
.setContentTitle(mContext.getResources().getString(R.string.app_name))
.setContentText(mContext.getString(R.string.clocked_in))
.setSmallIcon(R.drawable.ic_action_name)
.setChannelId(channelID)
.setSound(null);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(channelID, appName, NotificationManager.IMPORTANCE_LOW);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(ONGOINGNOTIFICATION_ID, notificationCompatBuilder.build());
}
コードをこれに置き換えます
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
NotificationChannel channel = new NotificationChannel("channel_01",
"Playback Notification",
NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("channel_01");
}
私のシーンは初めて音があり、更新通知に音は必要ありません。
これを使用します setOnlyAlertOnce() メソッド
参照: https://developer.Android.com/training/notify-user/build-notification#Updating
テストパスバージョン26