How to Create Notification in Android App Using NotificationManager to Send Notifications

0
Shares
How to Create Notification in Android App Using NotificationManager to Send Notifications
5 (100%) 1 vote

notification in android

Notifications are a powerful tool in Android developer’s toolkit to keep users up-to-date about events happening inside your Android app, even when users are not looking at your app directly.

In fact, a well-timed and useful notification is a great way to tempt users into launching and using your Android app, especially when they haven’t seen our app in a while.

What Generally Goes in an Android App Notification?

The most simple notifications must contain the following.

An Icon: Almost every Android phone user is busy and is always on the go. In fact, there’s no guarantee that the users will even have time to read your notification’s text.

Therefore, users must be able to recognize your app’s notifications at a glance, from the icon alone.

For this reason, you may choose your app’s icon for your notifications, but occasionally, you can also use a different image. For example, if you’re developing an instant messaging app, you can use the sender’s profile pic instead of your app’s icon.

Create images that you want to use and add them to your project’s drawable folder.

Title Text: You can either set your notification’s title by referencing a string resource, or by adding the text to your notification directly.

Detail Text: This is the most important part of notification in Android app, therefore this text part must include everything a user needs to understand about what exactly the user is being notified. However, since most mobile users are in rush, so keep this detailed text short and snappy.

Like everything in the mobile platform, notification in Android has also evolved over time. There are lots of another notification setting you can use, although some of them are quite difficult to integrate if you’re a beginner, therefore you may want to consult with an Android app development company to add them in your Android app correctly.

Now, since you know basics about Android app notification, it’s time to learn how to add one!

Creating Your Notification in Android

Add dependency in the build.gradle file

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

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.spaceo.notificationalertdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

activity_notifymessage.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.spaceo.notificationalertdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
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

package com.spaceo.notificationalertdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import com.spaceo.notificationalertdemo.NotifyMessage;
public class MainActivity extends AppCompatActivity {
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
Intent notificationIntent = new Intent(this,NotifyMessage.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.tvTitleofnotification))
.setContentText(getResources().getString(R.string.tvBodyofNotification))
.setContentIntent(pendingIntent);
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.getNotification();
notificationManager.notify(R.mipmap.ic_launcher, notification);
}
}

NotifyMessage

package com.spaceo.notificationalertdemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class NotifyMessage extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifymessage);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spaceo.notificationalertdemo">
<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>
<activity android:name=".NotifyMessage"></activity>
</application>
</manifest>

And Done!

device-2017-05-16-120754

Now of course, in this Android app tutorial, we’ve only covered the basics of creating and employing notification in Android app. But, depending on your app and functionalities of your app, you can go much deeper while making sure to be thoughtful when using this powerful tool.

Grab a free copy of Android Notification Example from Github.

This page was last edited on June 30th, 2018, at 6:12.
 
0
Shares
 

Have an Idea for Android App? Let's Discuss!

Get your free consultation now