我正在尝试显示通知。但是不会显示通知。下面是我的代码,
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());
        }
    }
    }
 
我想在顶部栏中显示一个通知,并希望在我的应用程序中清除或打开它。任何人都可以帮助我。提前致谢。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
您应该只在版本检查中包装通道创建逻辑。否则,如果设备操作系统版本低于Android O,则不会显示通知。
并且您在创建频道时将频道ID设置为“ 1”,在创建通知时将频道ID设置为“ channel_id”。
您应该使用相同的channelId字符串来创建NotificationChannel和Notification。
试试这个代码:
    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());
    }
}
 
希望能帮助到你