Contoh Notifikasi Android

Pemberitahuan Android memberikan informasi singkat dan tepat waktu tentang tindakan yang terjadi di aplikasi, bahkan tidak berjalan. Pemberitahuan menampilkan ikon, judul, dan beberapa jumlah teks konten.


Mengatur Properti Notifikasi Android

Properti pemberitahuan Android diatur menggunakan objek NotificationCompat.Builder. Beberapa properti pemberitahuan disebutkan di bawah ini :

  • setSmallIcon() : Ini menetapkan ikon pemberitahuan.
  • setContentTitle() : ini digunakan untuk mengatur judul pemberitahuan.
  • setContentText() : ini digunakan untuk mengatur pesan teks.
  • setAutoCancel() : Ini menetapkan properti pemberitahuan yang dapat dibatalkan.
  • setPriority() : Ini menetapkan prioritas pemberitahuan.

Contoh Notifikasi Android

Dalam contoh ini, kita akan membuat pesan pemberitahuan yang akan meluncurkan aktivitas lain setelah mengkliknya.
Tambahkan kode berikut dalam berkas activity_main.xml.

File : 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="example.android.com.androidnotification.MainActivity">  
 
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="ANDROID NOTIFICATION"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.091"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
 
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/button"  
        android:layout_marginBottom="112dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:text="Notify"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent" />  
 
</android.support.constraint.ConstraintLayout>  


Membuat aktivitas activity_notification_view.xml dan menambahkan kode berikut. Kegiatan ini akan diluncurkan dengan mengklik notifikasi. TextView digunakan untuk menampilkan pesan pemberitahuan.

File : activity_notification_view.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="example.android.com.androidnotification.NotificationView">  
 
    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center"  
        android:text="your detail of notification..."  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />  
 
    <TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.096"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/textView2"  
        app:layout_constraintVertical_bias="0.206"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
 
</android.support.constraint.ConstraintLayout>  


Di kelas MainActivity.java menambahkan kode berikut. Di kelas ini, mengklik tombol memanggil metode addNotification() di mana kita menerapkan objek NotificationCompat.Builder untuk mengatur properti pemberitahuan. Metode NotificationManager.notify() digunakan untuk menampilkan pemberitahuan. Kelas Intent digunakan untuk memanggil aktivitas lain (NotificationView.java) pada rekaman pemberitahuan.

File : MainActivity.java

package example.android.com.androidnotification;  
 
import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.content.Context;  
import android.content.Intent;  
import android.support.v4.app.NotificationCompat;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
 
public class MainActivity extends AppCompatActivity {  
    Button button;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        button = findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                addNotification();  
            }  
        });  
    }  
 
    private void addNotification() {  
        NotificationCompat.Builder builder =  
                new NotificationCompat.Builder(this)  
                        .setSmallIcon(R.drawable.messageicon) //set icon for notification  
                        .setContentTitle("Notifications Example") //set title of notification  
                        .setContentText("This is a notification message")//this is notification message  
                        .setAutoCancel(true) // makes auto cancel of notification  
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT); //set priority of notification  
 
 
        Intent notificationIntent = new Intent(this, NotificationView.class);  
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        //notification message will get at NotificationView  
        notificationIntent.putExtra("message", "This is a notification message");  
 
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,  
                PendingIntent.FLAG_UPDATE_CURRENT);  
        builder.setContentIntent(pendingIntent);  
 
        // Add as notification  
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        manager.notify(0, builder.build());  
    }  
}  


Kelas NotificationView.java menerima pesan pemberitahuan dan ditampilkan di TextView. Kelas ini dipanggil saat merekam pemberitahuan.

File : NotificationView.java

package example.android.com.androidnotification;  
 
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.widget.TextView;  
import android.widget.Toast;  
 
public class NotificationView extends AppCompatActivity {  
    TextView textView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_notification_view);  
        textView = findViewById(R.id.textView);  
        //getting the notification message  
        String message=getIntent().getStringExtra("message");  
        textView.setText(message);  
    }  
}  


File : strings.xml

<resources>  
    <string name="app_name">AndroidNotification</string>  
    <string name="notification_activity">NotificationView</string>  
</resources>  


Tambahkan kode berikut dalam file AndroidManifest.xml.

File : AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.android.com.androidnotification">  
 
    <application  
        android:allowBackup="true"  
        android:icon="@mipmap/ic_launcher"  
        android:label="@string/app_name"  
        android:roundIcon="@mipmap/ic_launcher_round"  
        android:supportsRtl="true"  
        android:theme="@style/AppTheme">  
        <activity android:name=".MainActivity">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
 
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
        <activity android:name=".NotificationView"  
            android:label="@string/notification_activity"  
            android:parentActivityName=".MainActivity">  
            <meta-data  
                android:name="android.support.PARENT_ACTIVITY"  
                android:value=".MainActivity"/>  
        </activity>  
    </application>  
 
</manifest>  


Output :

Notifikasi Android


Berlangganan update artikel terbaru via email:

1 Response to "Contoh Notifikasi Android"

  1. Check any offer’s T&Cs so you don’t waste time wagering on a sport that makes no or a diminished contribution. Safety – Because you cannot operate an internet reside casino in Malaysia, all sites that Malaysian people can play at are worldwide ones. This 1xbet korea implies that people in Malaysia are able to to} play at unlicensed sites. All the sites in our top ten record have some type of licensing, and are completely protected to play at. Online roulette is a completely random sport outcome of|as a result of} the winners aren't chosen by somebody who's a moderator of the game, and who could be bribed to tilt the game in one’s favour. Rather, the winner is chosen by a machine known as the random quantity generator that ensures that the ball lands on a quantity that is randomly chosen by the machine.

    BalasHapus

Iklan Atas Artikel

Iklan Bawah Artikel