Установите уведомление в цикле, пока пользователь не нажмет - PullRequest
0 голосов
/ 27 сентября 2018

вот мой код уведомления:

   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());
}

Это мой код уведомления.

Я использовал .setDefaults (Notification.FLAG_INSISTENT) для зацикливания, но звук уведомления приходит только один раз.

1 Ответ

0 голосов
/ 27 сентября 2018

Вы не можете установить флаг, используя setDefaults()

Вы можете использовать следующий код:

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;

, также проверьте этот вопрос переполнения стека

...