Update notification vibrate/ringtone
Asked Answered
M

2

7

I am using the NotificationManager builder to show an alert in my app. I know that the first parameter for the notify method is an id and the framework will update the notification if it is already visible, but if I set the alert to play a ringtone or vibrate, does the ringtone/vibration also fire if an alert is updated?

    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);
Manteau answered 4/5, 2012 at 16:26 Comment(1)
Go to see <#11487177>Polytechnic
M
7

Just tested this myself and the vibration/ringtones do fire off, even on an update.

UPDATE: Just an update, if you are using NotificationCompat.Builder or Notification.Builder you can set setOnlyAlertOnce to only sound the ringtone/vibrate once.

Manteau answered 4/5, 2012 at 17:18 Comment(0)
D
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);

Debi answered 30/9, 2015 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.