更新通知震动/铃声

7
我正在使用`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);

前往查看https://dev59.com/Qmgu5IYBdhLWcg3wOUkk。 - Jeff Bootsholz
2个回答

7

我刚刚亲测过,即使在更新时手机仍然会发出震动/铃声。

更新: 如果你正在使用NotificationCompat.BuilderNotification.Builder,你可以设置setOnlyAlertOnce仅在第一次响铃/震动时发出声音。


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);

NotificationCompat.Builder 通知构建器 = new NotificationCompat.Builder(MainActivity.this)

long[] 震动模式 = {500,1000}; 通知构建器.setVibrate(震动模式);

Uri 音频路径 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 通知构建器.setSound(音频路径);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接