我正在使用NotificationManager
构建器在我的应用中显示提醒。我知道notify
方法的第一个参数是一个id,如果通知已经可见,框架将更新通知,但如果我将警报设置为播放铃声或振动,铃声/振动是否也会触发如果更新警报?
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setContentTitle("title");
nb.setContentText("message");
nb.setSmallIcon(getResources().getIdentifier("drawable/alert", null, packageName));
nb.setWhen(System.currentTimeMillis());
nb.setAutoCancel(true);
nb.setTicker("message");
final Uri ringtone = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).getString("ringtone", getString(R.string.settings_default_ringtone)));
nb.setDefaults(Notification.DEFAULT_VIBRATE);
nb.setSound(ringtone);
nb.setDefaults(Notification.DEFAULT_LIGHTS);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Intent notificationIntent = new Intent(this, Main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
nb.setContentIntent(contentIntent);
Notification notification = nb.getNotification();
nm.notify(0, notification);
答案 0 :(得分:6)
我自己进行了测试,即使在更新时振动/铃声也会启动。
更新:
只需更新,如果您使用NotificationCompat.Builder
或Notification.Builder
,则可以将setOnlyAlertOnce
设置为仅响铃铃声/振动一次。
答案 1 :(得分:0)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
long [] v = {500,1000}; notificationBuilder.setVibrate(V);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationBuilder.setSound(URI);