首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android AlarmManager 通知無聲音解決:設置 Notification.defaults

     編輯:程式設計綜合問答

問題

自定義 BroadcastReceiver 發送通知,彈出提示但**無鈴聲、無震動**。

原因

未設置 Notification.defaults,系統不知道使用默認鈴聲/震動/燈光模式。

修正代碼

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("ALERT!!!!")
        .setContentText("Time Over")
        .setContentIntent(contentIntent)
        .setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE) // 關鍵
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .build();

可選常量

常量效果
DEFAULT_SOUND系統默認通知鈴聲
DEFAULT_VIBRATE系統默認震動模式
DEFAULT_LIGHTS系統默認 LED 燈光(需硬件支持)
DEFAULT_ALL以上三者合一

Android 8.0+ (Oreo) 必須創建 NotificationChannel

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Alarm notifications");
    NotificationManager manager = context.getSystemService(NotificationManager.class);
    manager.createNotificationChannel(channel);
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved