Я не могу понять, что может помешать работе уведомлений?

#java #android #android-studio #notifications #android-notifications

Вопрос:

Я не могу понять, в чем проблема, я сделал уведомления по видео, у него все работает, у меня нет. Я пытался делать уведомления с других видосов, код примерно такой же, но все равно не соответствует стандартам. Минимальная версия SDK-21. Во время отладки ошибок нет. Похоже, я что-то изменил в некоторых других файлах, поэтому я не вижу уведомлений, но я не знаю, что может помешать 🙂

 
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import static androidx.core.app.NotificationCompat.PRIORITY_HIGH;


public class SettingsActivity extends AppCompatActivity {
    NotificationManager notificationManager;
    private static final int NOTIFY_ID = 1;
    private static final String CHANNEL_ID = "NOTIFICATION_ID";
    Button notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        notification = findViewById(R.id.on_notification);
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                RemoteViews normal = new RemoteViews(getPackageName(), R.layout.notification_normal);
                RemoteViews expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                        .setAutoCancel(false)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent)
                        .setCustomContentView(normal)
                        .setCustomBigContentView(expanded)
                        .setPriority(PRIORITY_HIGH);
                createChannelIfNeeded(notificationManager);
                notificationManager.notify(NOTIFY_ID, notificationBuilder.build());

            }
        });
    }

    public static void createChannelIfNeeded(NotificationManager manager){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(notificationChannel);
        }
    }

}
 

Что может помешать работе уведомлений?