首页 > 解决方案 > 在android中不显示通知

问题描述

我正在尝试显示通知。但不显示通知。下面是我的代码,

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createNotificationButton = findViewById(R.id.button_create_notification);

        // Waits for you to click the button
        createNotificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starts the function below
                addNotification();
            }
        });
    }
    // Creates and displays a notification
    private void addNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel("1" , "Notify", NotificationManager.IMPORTANCE_HIGH);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);


            NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                    .setContentTitle("Test Title")
                    .setContentText("Test Message")
                    .setSmallIcon(R.mipmap.ic_launcher);

            notificationManager.notify(1, notification.build());



        }

    }

    }



我想在顶部栏中显示一个通知,并想在我的应用程序中清除或打开它......任何人都可以帮我解决这个问题。提前致谢。

标签: androidnotificationsandroid-studio-3.0

解决方案


您应该只在版本检查中包含通道创建逻辑。否则,如果设备操作系统版本低于 Android O,它将不会显示通知。

并且您在创建频道时使用频道 ID 作为“1”,在创建通知时使用“频道 ID”。

您应该使用相同channelId的字符串来创建NotificationChannelNotification

试试这个代码:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createNotificationButton = findViewById(R.id.button_create_notification);

        // Waits for you to click the button
        createNotificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starts the function below
                addNotification();
            }
        });
    }
    // Creates and displays a notification
    private void addNotification() {
        String channelId = "myNotificationChannel"; // Store channel ID as String or String resource
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel(channelId , "Notify", NotificationManager.IMPORTANCE_HIGH);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
           }

            NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId) // Use  the same channelId String while creating notification
                    .setContentTitle("Test Title")
                    .setContentText("Test Message")
                    .setSmallIcon(R.mipmap.ic_launcher);

            notificationManager.notify(1, notification.build());


    }

}

希望能帮助到你


推荐阅读