Notifications Android

By Kunal Kapoor
05-05-2018
586

A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification. Building a notification is an simple process Here's a simple code to create a notification in android Project Overview:

A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

Building a notification is an simple process. Here's a simple code to create a notification in android


Project Overview:



AndroidManifest.xml:

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ourcoaching.simplenotificationbuilder">

<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>
application>

manifest>


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="com.ourcoaching.simplenotificationbuilder.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify"
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.ourcoaching.simplenotificationbuilder;

import android.app.Notification;
import android.app.NotificationManager;
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 {

private Button button;
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.button);
}


@Override
protected void onResume() {
super.onResume();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Create_Notification(view);
}
});
}

public void Create_Notification(View view){
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");

// Set ID for the notification
int mNotificationId = 001;
// Get an instance of the NotificationManager service
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build the notification and issues it.
notificationManager.notify(mNotificationId, builder.build());
}
}


Output:


Download the code from GitHub



Related Tutorials