Android Studio Notification (status bar notification) does not display notification solution

16042258:

Introduction: In the process of learning, I found that no matter how I modified it, or even directly copied the code of the boss, my program could not pop up the status bar notification correctly. After a night of entanglement, I finally found the reason

Notifications not showing can be caused by several reasons:

  1. Notification Permissions: Make sure your app has the correct notification permissions. On Android devices, the user needs to grant the app notification permission to display notifications. You can check the status of the notification permission in the app's settings.

  2. Notification channels: Starting from Android 8.0 (API level 26), the concept of notification channels is introduced. You need to create and assign notifications to the corresponding channels to display notifications. Make sure you have properly created notification channels in your code and assigned notifications to the corresponding channels.

  3. Notification Priority: Notifications may be filtered or hidden based on their priority. Make sure you set an appropriate priority for notifications to make sure they show up on your device.

  4. Notification bar settings: Some devices or users may customize the notification bar, which may cause some notifications to be hidden or filtered. Make sure your device's notification settings aren't filtering or hiding your app.

  5. Other issues: There may be other factors involved in notifications not showing up, such as errors in code logic, device or operating system issues, and more. It is recommended to check your code logic to ensure that the notification is triggered and created correctly.

What affects me is the problem of notification permissions. Here I bring a solution:

method one:

Request notification permission. In your code, you can use NotificationManagerCompatthe class to check and request permission for notifications

// 检查通知权限是否已经授予
boolean notificationPermissionGranted = NotificationManagerCompat.from(this).areNotificationsEnabled();

if (!notificationPermissionGranted) {
    // 请求通知权限
    Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
        .putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
    startActivity(intent);
}

 Method Two:

Guide users to enable notification permissions in the app settings. If the notification permission is not granted, you can display a prompt dialog or guide the user to enter the application settings page to enable the notification permission

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("您需要开启通知权限才能接收通知,请点击确定前往设置页面开启权限。");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            .setData(Uri.fromParts("package", getPackageName(), null));
        startActivity(intent);
    }
});
builder.setNegativeButton("取消", null);
builder.show();

 

Finally, turn on the notification permission to receive notifications

Guess you like

Origin blog.csdn.net/qq_69574549/article/details/130622883