Notification Example
This is an example Notification for Android Operating System.. It covers a two notification, one has sound, vibration and light effects. And another one is normal.
Following is the screen visual when activity starts.
--------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
This is an example Notification for Android Operating System.. It covers a two notification, one has sound, vibration and light effects. And another one is normal.
Following is the screen visual when activity starts.
--------------------------------------------------------------------------------------------
xml version="1.0" encoding="utf-8"?>Notify1.xml (Design)
---------------------------------------------------------------------------------------------<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_back"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notification Example"
android:textSize="24sp"
android:textAlignment="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff"
android:paddingBottom="50dp"></View>
<EditText
android:id="@+id/et_notify_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>
<Button
android:id="@+id/btnNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification (Vibrate, Light, Sound)"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btnNotify2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Notify1.java
package in.co.mypractise.pranavdv.mypractise1; import android.app.Activity; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Color; import android.media.RingtoneManager; import android.os.Bundle; import android.provider.CalendarContract; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class Notify1 extends Activity { Button btnNOTIFY, btn_NOTIFY2; ImageView img_BACK; EditText et_NAME; public static final String NOTIFICATION_CHANNEL_ID = "10001"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notify1); img_BACK = findViewById(R.id.img_back); img_BACK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); et_NAME = findViewById(R.id.et_notify_name); btnNOTIFY = findViewById(R.id.btnNotify); btnNOTIFY.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(Notify1.this, "Button Click", Toast.LENGTH_SHORT).show(); // ------------ Notification Process----------- String notification_title = "My Practise1"; String notification_message = "Message Will Appear Here"; String message1 = et_NAME.getText().toString(); String click_action = "Action"; String from_user_id = "User ID"; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.ic_import_contacts_black_24dp) .setContentTitle(notification_title) .setContentText("Hi "+message1+" Thanks for using App");
//---- Notification Vibration, Lights, Sound--------- mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); mBuilder.setLights(Color.GREEN,3000, 3000); mBuilder.setSound(RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION));
Intent resultIntent = new Intent(click_action); resultIntent.putExtra("user_id", from_user_id); PendingIntent resultPendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); int mNotificationId = (int) System.currentTimeMillis(); NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance); mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID); mNotifyMgr.createNotificationChannel(notificationChannel); } mNotifyMgr.notify(mNotificationId, mBuilder.build()); //---- Notification Process fiish--------- } }); btn_NOTIFY2 = findViewById(R.id.btnNotify2); btn_NOTIFY2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String notification_title = "Title"; String notification_message = "Message Will Appear Here"; String message1 = et_NAME.getText().toString(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.ic_import_contacts_black_24dp) .setContentTitle(notification_title) .setContentText("Hi "+message1+" Thanks for using App"); int mNotificationId = (int) System.currentTimeMillis(); NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance); mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID); mNotifyMgr.createNotificationChannel(notificationChannel); } mNotifyMgr.notify(mNotificationId, mBuilder.build()); } }); } }