[英]Notification sound played twice when Notification is shown
This is myService
class that extends BroadcastReceiver
.这是扩展
BroadcastReceiver
myService
类。 Notification sound is played only once if I don't call showNotif .如果我不调用showNotif ,通知声音只会播放一次。 When I do, the sound is played twice.
当我这样做时,声音会播放两次。 Can anyone help me and tell what's wrong with this code?
任何人都可以帮助我并告诉我这段代码有什么问题吗? Thanks.
谢谢。
public class myService extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
showNotif(context, id, name);
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
private void showNotif(Context context, Integer id, String name) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id.toString());
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.alert)
.setTicker("bla")
.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
.setContentTitle(name)
.setContentText("blabla")
.setContentInfo("blablabla");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(context, newClass.class);
mIntent .putExtra("ID", id);
mIntent .putExtra("NAME", name);
PendingIntent mPending = PendingIntent.getActivity(context, id, mIntent , PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(mPending);
notificationManager.notify(id, notificationBuilder.build());
}
}
The sound on the notification can be turned off like this:通知上的声音可以像这样关闭:
.setDefaults(0)
Or do not set DEFAULT_ALL或者不设置 DEFAULT_ALL
Other default values that can be used:可以使用的其他默认值:
More info here: Android Disable Notification Sounds更多信息: Android 禁用通知声音
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.