Working with android notifications using Android Studio and Java
In this video we will simply show notification on clicking of button. The notification will work on any Android OS.Video
Step 1: Create a new project OR Open your project
Step 2: Code:
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=".MainActivity"><Buttonandroid:id="@+id/showNotificationBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Show Notification"style="@style/Base.Widget.AppCompat.Button.Colored"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
MainActivity.java
package com.blogspot.adtechofficial.simplenotification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.os.Build;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationManagerCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {//there can be multiple notifications so it can be used as notification identityprivate static final String CHANNEL_ID = "channel_id01";private static final int NOTIFICATION_ID = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button showNotificattionBtn = findViewById(R.id.showNotificationBtn);showNotificattionBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//click button to show notificationshowNotification();}});}private void showNotification() {createNotificationChannel();//creating notificationNotificationCompat.Builder builder = new NotificationCompat.Builder(this , CHANNEL_ID);//iconbuilder.setSmallIcon(R.drawable.ic_notification);//titlebuilder.setContentTitle("Title of Notification");//descriptionbuilder.setContentText("This is the description of the notification");//set prioritybuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);//notification managerNotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);notificationManagerCompat.notify(NOTIFICATION_ID , builder.build());}private void createNotificationChannel() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){CharSequence name = "My Notification";String description = "My notification description";//importance of your notificationint importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID , name , importance);notificationChannel.setDescription(description);NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);notificationManager.createNotificationChannel(notificationChannel);}}}
Step 3: Run Project
OutputShow Notifications on any device - Java |
0 Response to "Show Notifications On Any Device - Java"