Show Notifications on any device - Java
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"> <Button android: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 identity private static final String CHANNEL_ID = "channel_id01"; private static final int NOTIFICATION_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button showNotificattionBtn = findViewById(R.id.showNotificationBtn); showNotificattionBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //click button to show notification showNotification(); } }); } private void showNotification() { createNotificationChannel(); //creating notification NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); //icon builder.setSmallIcon(R.drawable.ic_notification); //title builder.setContentTitle("Title of Notification"); //description builder.setContentText("This is the description of the notification"); //set priority builder.setPriority(NotificationCompat.PRIORITY_DEFAULT); //notification manager NotificationManagerCompat 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 notification int 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 |
Comments
Post a Comment