Harish Rawal
Harish Rawal

Reputation: 236

Set notification in loop until user click

here is my code of notification:

   private void APP_FOREGROUND_showNotificationMessage(Context context, String title, String message, Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    String channelId = "CHANNEL_ID";
    String channelName = "CHANNEL NAME";

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel(channelId,
                channelName,
                NotificationManager.IMPORTANCE_HIGH);

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();



        mChannel.setDescription(title);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic), attributes); // Here you set the sound


        if (manager != null)
            manager.createNotificationChannel(mChannel);
     }
     NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.vb_grey)
            .setContentTitle(title)
            .setColor(Color.RED)
            .setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
            .setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);

    Notification notification = builder.build();
    notification.flags = Notification.FLAG_INSISTENT;

    manager.notify(0, builder.build());
}

This is my notification code.

I have used .setDefaults(Notification.FLAG_INSISTENT) for looping but notification sound coming one time only.

Upvotes: -1

Views: 1064

Answers (1)

Niki van Stein
Niki van Stein

Reputation: 10724

You can not set the flag by using setDefaults()

You can use the following code:

NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.vb_grey)
            .setContentTitle(title)
            .setColor(Color.RED)
            .setOngoing(true)  //<-- you also need this one
            .setSound(Uri.parse("android.resource://"+context+context.getPackageName()+"/"+R.raw.offic))
            .setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);

Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;

also check this stackoverflow question

Upvotes: 0

Related Questions