I have a button which, when pressed 5 times will create a user notification. I am able to get the app to run however when pressing the button, I can see that the "counter" is incrementing but when >=5 there is not any notification being displayed. I have entered into AndroidManifest.xml these lines:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Below is my MainActivity.java file:
package com.enetapplications.localnotifications;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.Manifest;
public class MainActivity extends AppCompatActivity {
Button button;
public final String CHANNEL_ID = "1_" + System.currentTimeMillis();
int counter = 0;
private static final int PERMISSION_REQUEST_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission();
counter++;
button.setText("" + counter);
if (counter >= 5) {
startNotification();
}
}
});
}
public void startNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_add_alert_24)
.setContentTitle("Title")
.setContentText("Notification Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat compat = NotificationManagerCompat.from(MainActivity.this);
if (compat.areNotificationsEnabled()) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, PERMISSION_REQUEST_CODE);
} else {
compat.notify(1, builder.build());
}
}
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.VIBRATE}, PERMISSION_REQUEST_CODE);
} else {
startNotification();
}
} else {
startNotification();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startNotification();
} else {
// Handle permission denied
}
}
}
}
>Solution :
If notifications are not being displayed in Android Studio, there are a few possible reasons and troubleshooting steps you can try:
Ensure that notifications are enabled in Android Studio. Go to File -> Settings (or Preferences on macOS) -> Appearance & Behavior -> Notifications. Make sure that the desired notification types are set to "Log Errors" or a higher level.
Confirm that your system’s notification settings allow Android Studio to display notifications. On your Android device, go to Settings -> Apps -> Android Studio -> Notifications. Ensure that notifications are allowed, and the desired notification channels are enabled.
Sometimes, a simple restart can resolve notification issues. Close Android Studio completely and reopen it to see if the notifications start appearing.
Android Studio’s Event Log can provide information about any errors or issues that may be affecting notifications. Go to View -> Tool Windows -> Event Log. Look for any relevant error messages or notifications related to notifications.
Make sure you are using the latest version of Android Studio. Check for updates and install any available updates to ensure you have the most up-to-date version of the IDE.
If you have enabled "Power Save" mode in Android Studio, it may suppress certain notifications. Disable this mode by going to File -> Power Save Mode and unchecking the option.
If you are expecting specific notifications in your code, double-check that you have implemented them correctly. Review your code for any issues that might be preventing the notifications from being triggered.
In some cases, antivirus software or firewall settings can interfere with Android Studio’s notifications. Temporarily disable these tools and check if the notifications start appearing.