Membuat Notifikasi bergetar di Android
1. Buatlah sebuah Projek di Android
Studio, pilih empty activity
2. tambahkan permission vibrate pada AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
3. buatlah sebuah button untuk mengirim
notifikasi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="giviews.id.notificationview.MainActivity">
<Button
android:id="@+id/btn_displaynotif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Display Notification"
android:onClick="onClick"/>
</RelativeLayout>
<RelativeLayout 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="giviews.id.notificationview.MainActivity">
<Button
android:id="@+id/btn_displaynotif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Display Notification"
android:onClick="onClick"/>
</RelativeLayout>
4. kemudian buat activity baru dengan
nama NotificationViewActivity dengan cara klik kanan pada folder app pilih new
-> activity -> empty activity. dan masukan textview pada
activity_notification_view.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="giviews.id.notificationview.NotificationViewActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Diharapkan Semua Anggota BEM Rapat Di Aula Sekarang..!!" tools:layout_editor_absoluteX="9dp" tools:layout_editor_absoluteY="16dp" /> </RelativeLayout>
5. Selanjutnya untuk file
MainActivity.java masukan kode berikut
package giviews.id.notificationview; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Vibrator; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { int notificationID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { displayNotification(); } protected void displayNotification() { //---PendingIntent to launch activity if the user selects // this notification--- Intent i = new Intent(this, NotificationViewActivity.class); i.putExtra("notificationID", notificationID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder notifBuilder; notifBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.gbrnotif) .setContentTitle("Ada Meeting BEM Sekarang...!") .setContentText("Reminder: Rapat Akan Mulai 5 minutes") .setContentIntent(pendingIntent) .addAction(R.mipmap.ic_launcher,"Read More",pendingIntent); Vibrator vi; vi=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE); if (vi.hasVibrator()){ vi.vibrate(20000); } nm.notify(notificationID, notifBuilder.build()); } }
6. dan pada
NotificationViewActivity.java masukan kode berikut, sesuaikan packagenya dengan
package teman-teman.
package giviews.id.notificationview; import android.app.NotificationManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class NotificationViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_view); //---look up the notification manager service--- NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //---cancel the notification that we started--- nm.cancel(getIntent().getExtras().getInt("notificationID")); } }
Selesai,
silakan run aplikasi anda jika notifikasinya muncul berarti anda telah
berhasil, jika ada yang error silakan ditanyakan pada kolom komentar di bawah.
0 komentar :