[英]Enable all notification settings by default in my android app
Android 8 或更高版本您需要使用NotificationChannel來啟用聲音、振動、聲音等。
Uri notification_sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(notification_sound, attributes);//for enable sound
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
但在Redmi note 5 pro (MIUI 10.2.1.0) 中,通知聲音仍被禁用。 我認為 MIUI 中存在錯誤。 在mi A1(Android one mobile)中運行相同的代碼,一切正常。 有用。
請參閱此鏈接以了解有關通知渠道的更多信息
嘗試在 AndroidManifest 文件中使用以下權限
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
並為低於和高於 Oreo 版本的 Oreo 和更高版本設置通知優先級IMPORTANCE_HIGH
對於 Oreo 和更高版本,以及PRIORITY_HIGH or PRIORITY_MAX
對於低於 Oreo 版本
奧利奧以下版本優先
mBuilder.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(soundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH) // prirority here for version below Oreo
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.app_logo))
.setContentText(message)
.build();
奧利奧及以上優先
WhatsApp 、 Telegram 、 Snapchat等。 這些都是列入白名單的應用程序,這意味着軟件包名稱在操作系統級別進行了硬編碼,以允許默認啟用某些權限。 我們的應用程序並非如此。 因此,我們需要用戶手動啟用這些權限。
你可以自己檢查一下。 創建一個新的 android 應用程序,給出Telegram應用程序的包名( org.telegram.messenger )並運行它。 根本不做任何代碼,也不需要打開應用程序。 只需轉到新創建的應用程序的通知設置,您就可以在其中找到默認啟用的所有權限。
希望你得到了答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.