更新通知振动/铃声

Kri*_*s B 7 notifications alert android notificationmanager

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

Kri*_*s B 6

我自己测试了这个,振动/铃声即使在更新时也会启动.

更新:只是一个更新,如果你正在使用NotificationCompat.BuilderNotification.Builder你可以设置setOnlyAlertOnce只发出一次铃声/振动.