更新通知振动/铃声

时间:2012-05-04 16:26:30

标签: android notifications alert 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);

2 个答案:

答案 0 :(得分:6)

我自己进行了测试,即使在更新时振动/铃声也会启动。

更新: 只需更新,如果您使用NotificationCompat.BuilderNotification.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);