Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过点击按钮创建android通知_Android_Android Notifications_Mobile Application_Android Developer Api - Fatal编程技术网

通过点击按钮创建android通知

通过点击按钮创建android通知,android,android-notifications,mobile-application,android-developer-api,Android,Android Notifications,Mobile Application,Android Developer Api,我尝试创建android通知,当我点击一个按钮但不起作用时。 这是我在按钮中编写的代码 NotificationManager Nm; public void Notify(View view) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setContentTitle("LOGIN")

我尝试创建android通知,当我点击一个按钮但不起作用时。 这是我在按钮中编写的代码

NotificationManager Nm;
    public void Notify(View view) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setContentTitle("LOGIN")
                .setContentText("You are login successfully")
                .setSmallIcon(R.drawable.done);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, mBuilder.build());
    }

正如Ikazuchi所说,对于android 8之后的android版本,您需要添加一个通知通道。可以这样做,如这里的文档所示:,如下所示:

createNotificationChannel();
 Notification notification = new NotificationCompat.Builder(this, "channelID")
              .setSmallIcon(R.drawable.notification_icon)
              .setContentTitle("My notification")
              .setContentText("Much longer text that cannot fit one line...")
              .build();


 private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel serviceChannel = new NotificationChannel(
                "channelID",
                "Channel Name",
                importance
        );

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }
}

您必须为您的通知添加频道,因为在android>=Oreo的手机中显示通知非常重要。看看这个。