2

Below is the code I am using to send a notification to the user:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("Secure Mail")
                    .setContentText("New Secure Mail Received")
                    .setAutoCancel(true)
                    .setOnlyAlertOnce(true);
                    /*
                    .addAction(R.drawable.ic_reply_white_24dp, "Reply", contentIntent)
                    .addAction(R.drawable.ic_reply_all_white_24dp, "Reply All", contentIntent)
                    .addAction(R.drawable.ic_forward_white_24dp, "Forward", contentIntent);
                    */

            builder.setContentIntent(contentIntent);
            builder.setAutoCancel(true);
            builder.setOnlyAlertOnce(true);

            if (ringtone.equals("")) {
                Log.d(TAG, "Default Sound");
                Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                builder.setSound(soundUri);
            } else if (!ringtone.equals("Silent")) {
                Log.d(TAG, "Selected sound: " + Uri.parse(ringtone));
                //Uri currentRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
                Uri currentRintoneUri = Uri.parse(ringtone);
                builder.setSound(currentRintoneUri);
            }
            //builder.setLights(Color.BLUE, 500, 500);

            if (vibrate.equals("true")) {
                Log.d(TAG, "Vibrate is enabled");
                //long[] pattern = {500, 500, 500, 500, 500, 500, 500, 500, 500};
                //builder.setVibrate(pattern);

                builder.setDefaults(Notification.DEFAULT_VIBRATE);
            }

            builder.setStyle(new NotificationCompat.InboxStyle());

            /*
            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
            */

            builder.setDefaults(Notification.FLAG_AUTO_CANCEL);
            builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);

            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            manager.notify(1, builder.build());

I need to be able to have multiple defaults (vibrate, auto cancel and alert once) but it appears that only the last (alert once) will work whilst the others won't.

I tried using a Notification object and then setting flags (this appeared to work with vibrate, auto cancel and alert once on my device but then other users were reporting that the notifications now weren't coming through at all).

Therefore, how can I use my current code but also set the three defaults of vibrate, auto cancel and alert once?

Toby Clench
  • 403
  • 1
  • 5
  • 22

1 Answers1

2

You can use the | operator to combine the defaults.

builder.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE);
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33