Anda di halaman 1dari 4

How to show Notification with Notification Builder &

NotificationManager

Dalam aplikasi android, saat kita menggunakannya pasti sudah menemukan sebuah
pop-up yang muncul dari bilah atas ponsel kita. Biasanya memberikan informasi status, pesan
masuk dan lain sebagainya. Ada beberapa jenis notifikasi. Ada yang berupa pop-up dan status
bar. Notification memiliki design guidelines tersendiri. Untuk informasi lebih lanjutnya
mengenai android notifications bisa mengunjungi langsung ke Material.io dan Notification
Overview
Untuk menentukan sebuah informasi pada user interface dan action dari notifikasi
yang muncul bisa menggunakan object Notification.Builder. Nah, untuk membuat notifikasi
itu sendiri, anda bisa menggunakan Notification.Builder.build() yang mana akan
mengembalikan sebuah object notifikasi. Untuk memberikan object notifikasi, anda bisa
menggunakan NotificationManager.notifiy().
Untuk Android O ke atas, anda perlu mebuat channel agar notifikasi bisa berjalan.
Channel ini difungsikan untuk mengelompokkan notifikasi dalam beberapa group seperti
kanal (Channel). Dengan menggunakan channel anda bisa mengatur notifikasi dalam satu
channel dalam satu waktu, seperti mengatur prioritas suara, maupun geteran. Sehingga akan
mempermudah dalam mem-filter notifikasi yang masuk, dan juga akan meningkatkan
kenyamanan pengguna.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,


CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);

if (notificationmanager != null) {
notificationmanager.createNotificationChannel(channel);
}
}

Berikut ini adalah contoh penerapan sederhana dari Notification :


1. Buatlah sebuah project baru di AndroidStudio. Untuk template pilihlah Phone and
Tablet lalu pilih yang EmptyActivity
2. Selanjutnya buatlah nama project sesuai dengan gambar dibawah ini

3. Selanjutnya bukalah activity_main.xml lalu tambahkanlah kode berikut


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_send_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kirim_notifikasi"
android:background="@color/teal_200"
android:padding="10dp"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

4. Selanjutnya tambahkanlah beberapa string pada strings.xml


<resources>
<string name="app_name">MyNotification</string>
<string name="kirim_notifikasi">KIRIM NOTIFIKASI</string>
<string name="content_title">Welcome to Urang Coding</string>
<string name="content_text">There is nothing that is impossible to do
as long as you are willing to try</string>
<string name="subtext">New Update</string>
</resources>

5. Kita akan mengatur tampilan dari notifikasi. Gunakan NotificationCompat.Builder.


tambahkanlah pada MainActivity
package com.urangcoding.mynotification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.IntentFilter;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

private static final int NOTIFICATION_ID = 1;


private static final String CHANNEL_ID = "channel_introduction";
private static final CharSequence CHANNEL_NAME = "urangcoding channel";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton btn_send_notification =
findViewById(R.id.btn_send_notification);

btn_send_notification.setOnClickListener(view -> {
kirimNotifikasi();
});

private void kirimNotifikasi() {


NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

Uri alarmSound =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder builder = new


NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.logo_urangcoding)
.setContentTitle(getResources().getString(R.string.content_
title))
.setContentText(getResources().getString(R.string.content_t
ext))
.setSubText(getResources().getString(R.string.subtext))
.setAutoCancel(true)
.setSound(alarmSound);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


NotificationChannel channel = new
NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(CHANNEL_NAME.toString());

builder.setChannelId(CHANNEL_ID);

if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}

Notification notification = builder.build();

if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
}

6. Selanjutnya silahkan jalankan aplikasi. Bisa menggunakan emulator atau bisa


langsung di ponsel menggunakan mode pengembang.

Anda mungkin juga menyukai