Rajat Saxena
Rajat Saxena

Reputation: 3915

Android notification keeps on repeating notification sound

Following is my notification generationg code, but the sound keeps on repeating until pull down the status bar to check it.

NotificationManager mNotificationManager;
        android.support.v7.app.NotificationCompat.Builder mBuilder;
        Notification mNotification;
        final int NOTIFICATION_ID = 1;

        mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, HomeScreenTabbed.class), 0);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        mBuilder = new android.support.v7.app.NotificationCompat.Builder(ctx);
        mBuilder.setContentTitle(notificationTitle);
        mBuilder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(notificationBody));
        mBuilder.setContentText(notificationBody);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(alarmSound);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher));
        mBuilder.setSmallIcon(R.drawable.ic_pollen_notification);
        mBuilder.setAutoCancel(true);
        mNotification = mBuilder.build();
        // set dismiss on click flags
        mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);

Upvotes: 3

Views: 6008

Answers (3)

Nenad Štrbić
Nenad Štrbić

Reputation: 397

Just this:

.setOnlyAlertOnce(true)

Upvotes: 0

Rajat Saxena
Rajat Saxena

Reputation: 3915

I solved the problem by using mNotification.defaults in place of mNotification.flags

Upvotes: 7

Shane Duffy
Shane Duffy

Reputation: 1147

Try adding the flag FLAG_ONLY_ALERT_ONCE to your mNotification.flags definition.

http://developer.android.com/reference/android/app/Notification.html#FLAG_ONLY_ALERT_ONCE

This should prevent it from sending the sound more than once.

Upvotes: 1

Related Questions