kim J
kim J

Reputation: 51

Android Studio 'Notification.FLAG_INSISTENT' not work

I want to make device vibrate continually when I received a notification and if I press some button, vibration stop.

So I add these two line.

Notification notification = notificationBuilder.build();

notification.flags |= Notification.FLAG_INSISTENT;

But when I received notification, device vibrates only one time.

What do I have to do for continually vibrate?

private void showNotification(String title, String message) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

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

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher_plant)
                .setLights(Color.GREEN,1,1)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .addAction(R.drawable.ic_menu_camera,"Go CCTV",getNotificationPendingIntent());


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_INSISTENT;
        notificationManager.notify(0, notificationBuilder.build());
    }

Upvotes: 0

Views: 2906

Answers (1)

etan
etan

Reputation: 593

You're calling notificationBuilder.build() twice :)

Instead of notificationManager.notify(0, notificationBuilder.build());

do: notificationManager.notify(0, notification);

Upvotes: 3

Related Questions