开发者社区> 问答> 正文

在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());



        }

    }

    }

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

展开
收起
Puppet 2020-01-07 21:50:05 497 0
1 条回答
写回答
取消 提交回答
  • 您应该只在版本检查中包装通道创建逻辑。否则,如果设备操作系统版本低于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());
    
    
        }
    
    }
    

    希望能帮助到你

    2020-01-07 21:50:30
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载