TUAN DC

Sử dụng Notification trong Android



Chắc hẳn ai cũng dùng Facebook trên điện thoại đúng không nào và khi có tin nhắn hay các thông báo mới bạn sẽ thường thấy một thông báo được hiển thị lên màn hình từ Facebok mà chúng ta không hề bật app trên điện thoại và đó chính là notification.

Notification là gì.

Chắc hẳn ai cũng dùng Facebook trên điện thoại đúng không nào và khi có tin nhắn hay các thông báo mới bạn sẽ thường thấy một thông báo được hiển thị lên màn hình từ Facebok mà chúng ta không hề bật app trên điện thoại và đó chính là notification.

Notification là một dạng thông báo hiển thị thông tin lên màn hình người sử dụng ở bên ngoài ứng dụng và nếu bạn muốn xem chi tiết thông báo này bạn có thể kéo thanh Notification drawer xuống để xem thông báo đó.

Cách sử dụng notification.

Các thông báo trong Android là các đối tượng thuộc lớp Notification vì thế để sử dụng được nó ta phải sử dụng NotificationManager, và nó được tạo ra từ context thông qua hàm getSystemService().

Sau đây là một ví dụ đơn giản về notification.

Ví dụ: ứng dụng này sẽ không có giao diện xinh lung linh hay chân dài hot girl nên mình sẽ viết ví dụ vô phần chính luôn đó là file MainActivity.java.

package com.blog.akinosora.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
        Context.NOTIFICATION_SERVICE);// Tạo đối tượng Notification
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent
        .getActivity(this, (int) System.currentTimeMillis(), intent, 0);// Tạo một pendding Intent
    /*
    * Một PendingIntent là một token cho phép chuyển đến một ứng
    * dụng khác ( ví dụ notification manager,
    * alarm manager hoặc các ứng dụng bên thứ 3 ) , nó cho phép
    * ứng dụng khác sử dụng quyền trong ứng dụng của bạn để thực hiện một chức
    * năng nào đó.*/

    /*Sử dụng Builder của lớp notification để tạo một notification*/
    Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject")
        .setSmallIcon(R.drawable.email)
        .setContentIntent(pIntent)
        .setAutoCancel(true)
        .addAction(R.drawable.like, "Call", pIntent)
        .addAction(R.drawable.eyeglasses, "More", pIntent).build();
    notificationManager.notify(0, noti);
    /* Hiển thị thông báo notification bàng hàm notification
    Và bạn chú ý nhé hàm có hai đối số và là notify(ID, noyifcation)
    ID chính là ID mà bạn muốn đặt cho notification.
    và nếu muốn hủy một notification bạn chỉ cần gọi hàm notificationManager.cancel(ID) hoặc cancelAll() để xóa tất các notification
    */

  }
}
package com.blog.akinosora.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
        Context.NOTIFICATION_SERVICE);// Tạo đối tượng Notification
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent
        .getActivity(this, (int) System.currentTimeMillis(), intent, 0);// Tạo một pendding Intent
    /*
    * Một PendingIntent là một token cho phép chuyển đến một ứng
    * dụng khác ( ví dụ notification manager,
    * alarm manager hoặc các ứng dụng bên thứ 3 ) , nó cho phép
    * ứng dụng khác sử dụng quyền trong ứng dụng của bạn để thực hiện một chức
    * năng nào đó.*/

    /*Sử dụng Builder của lớp notification để tạo một notification*/
    Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject")
        .setSmallIcon(R.drawable.email)
        .setContentIntent(pIntent)
        .setAutoCancel(true)
        .addAction(R.drawable.like, "Call", pIntent)
        .addAction(R.drawable.eyeglasses, "More", pIntent).build();
    notificationManager.notify(0, noti);
    /* Hiển thị thông báo notification bàng hàm notification
    Và bạn chú ý nhé hàm có hai đối số và là notify(ID, noyifcation)
    ID chính là ID mà bạn muốn đặt cho notification.
    và nếu muốn hủy một notification bạn chỉ cần gọi hàm notificationManager.cancel(ID) hoặc cancelAll() để xóa tất các notification
    */

  }
}

Như vậy là tôi đã hướng dẫn các bạn sử dụng Notification rồi đúng không nào nó thật đơn giản cảm ơn cấc ban đã theo dõi và xin hẹn gặp lại ở các bài hướng dẫn tiếp theo.


Tags: android notification thông báo


Nội dung liên quan