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

How to create a notification with NotificationCompat.Builder in Android?

在进入NotificationCompact.Builder之前,我们应该知道什么是android中的通知。通知就像在操作栏上显示系统消息一样。就像未接来电通知,如下所示

Notification Center

此示例演示了如何集成Android Notification。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 Mobile设备与计算机连接。要从android studio运行该应用,请打开您项目的活动文件之一,然后从工具栏中单击运行Play Icon icon。选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备

Click Icon

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

Sample Notification