Computer >> Máy Tính >  >> Lập trình >> Android

Cách tạo thông báo với NotificationCompat.Builder trong Android?

Trước khi tham gia NotificationCompact.Builder, chúng ta nên biết thông báo trong Android là gì. Thông báo giống như một thông báo hiển thị hệ thống trên thanh tác vụ. giống như thông báo cuộc gọi nhỡ như hình dưới đây

Cách tạo thông báo với NotificationCompat.Builder trong Android?

Ví dụ này trình bày cách tích hợp Android Notification.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android =    "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://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>

Bước 3 - Thêm mã sau vào 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());
   }
}

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn

Cách tạo thông báo với NotificationCompat.Builder trong Android?

Bây giờ bấm vào nút trên, bạn sẽ nhận được đầu ra như hình dưới đây

Cách tạo thông báo với NotificationCompat.Builder trong Android?