如何在Android中使用NotificationCompat.Builder创建通知?


在深入了解NotificationCompact.Builder之前,我们应该了解Android中的通知是什么。通知就像在操作栏上显示系统消息一样,就像下面显示的未接来电通知。

Notification Center

此示例演示如何集成Android通知。

步骤1 - 在Android Studio中创建一个新项目,转到文件⇒新建项目,并填写所有必需的详细信息以创建新项目。

步骤2 - 将以下代码添加到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android =    "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Click"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout>

步骤3 - 将以下代码添加到src/MainActivity.java

package com.example.andy.myapplication;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button=findViewById(R.id.button);
      button.setOnClickListener(this);
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.button:
         notificationDialog();
         break;
      }
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   private void notificationDialog() {
      NotificationManager notificationManager = (NotificationManager)       getSystemService(Context.NOTIFICATION_SERVICE);
      String NOTIFICATION_CHANNEL_ID = "tutorialspoint_01";
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
         // Configure the notification channel.
         notificationChannel.setDescription("Sample Channel description");
         notificationChannel.enableLights(true);
         notificationChannel.setLightColor(Color.RED);
         notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
         notificationChannel.enableVibration(true);
         notificationManager.createNotificationChannel(notificationChannel);
      }
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
      notificationBuilder.setAutoCancel(true)
      .setDefaults(Notification.DEFAULT_ALL)
      .setWhen(System.currentTimeMillis())
      .setSmallIcon(R.mipmap.ic_launcher)
      .setTicker("Tutorialspoint")
      //.setPriority(Notification.PRIORITY_MAX)
      .setContentTitle("sample notification")
      .setContentText("This is sample notification")
      .setContentInfo("Information");
      notificationManager.notify(1, notificationBuilder.build());
   }
}

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您的项目中的一个活动文件,然后单击运行播放图标 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕

Click Icon

现在点击上面的按钮,您将得到如下所示的输出

Sample Notification

更新于: 2020年6月26日

3K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告