How to create Notifications in Android?

Notifications: A notification is a message you can display to the user outside of your application’s normal UI.

When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

notify1

Notifications in the notification drawer can appear in one of two visual styles, depending on the version and the state of the drawer:

Normal view
The standard view of the notifications in the notification drawer.
Big view
A large view that’s visible when the notification is expanded. Big view is part of the expanded notification feature available as of Android 4.1.

These styles are described in the following sections.

Normal view

A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded. This is an example of a normal view:

notify2

The callouts in the illustration refer to the following:

  1. Content title

  2. Large icon

  3. Content text

  4. Content info

  5. Small icon

  6. Time that the notification was issued. You can set an explicit value with setWhen(); if you don’t it defaults to the time that the system received the notification.

Big view

A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.

The following screenshot shows an inbox-style notification:

notify3

Notice that the big view shares most of its visual elements with the normal view. The only difference is callout number 7, the details area. Each big view style sets this area in a different way. The available styles are:

Big picture style
The details area contains a bitmap up to 256 dp tall in its detail section.
Big text style
Displays a large text block in the details section.
Inbox style
Displays lines of text in the details section.

All of the big view styles also have the following content options that aren’t available in normal view:

Big content title
Allows you to override the normal view’s content title with a title that appears only in the expanded view.
Summary text
Allows you to add a line of text below the details area.

Steps to create Notification:

This example has an activity containing a button. Clicking on this button will create the notification in the notification bar. On expanding the notification, will give the details of notice and on tapping the detail will lead you to new activity.

Here are the files:

Specify a button in layout_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <Button 
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/notify"
 android:onClick="sendNotification"/>
 

</LinearLayout>

Here is the MainActivity.java code:

(Read comments inside code to have better understanding)

package com.example.simplenotification;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.layout_main);

 
 }

 public void sendNotification(View view){
 //Creating Notification Builders
 NotificationCompat.Builder myNotificationBuilder = new NotificationCompat.Builder(this);
 
 //Setting Notification properties
 myNotificationBuilder.setSmallIcon(R.drawable.ic_launcher);
 myNotificationBuilder.setContentTitle("First Notification");
 myNotificationBuilder.setContentText("This is a test notification");
 int notificationid = 3452;
 
 // Creates an explicit intent for an Activity in your app
 Intent myIntent = new Intent(this, ResultActivity.class);
 myIntent.putExtra("myNotificationId", notificationid);
 myIntent.putExtra("AnyValue", 12345);
 //myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
 PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 
 // start the activity when the user clicks the notification text
 myNotificationBuilder.setContentIntent(myPendingIntent);
 
 //Issue the Notification
 NotificationManager myManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 myManager.notify(notificationid, myNotificationBuilder.build());
 }
}

The activity that  opens up on tapping the notification is:

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;

public class ResultActivity extends Activity {
 public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.layout_result);
 TextView output = (TextView)findViewById(R.id.textView1);
 Intent myIntent = getIntent();
 int myNotificationId = myIntent.getIntExtra("myNotificationId",-1);
 
 //Removing notification from top after tap on notification
 if (myNotificationId != -1)
 {
 NotificationManager myManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 myManager.cancel(myNotificationId);
 }
 output.setText("Data sent by first activity: "+myIntent.getIntExtra("AnyValue", -1));
 }
}

And you are done. 🙂

 

See in code: You used pending intents. Following is the detail about pending intents.

A Pending Intent specifies an action to take in the future. It lets you pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as your application, whether or not your application is still around when the Intent is eventually invoked.

It is a token that you give to a foreign application which allows the foreign application to use your application’s permissions to execute a predefined piece of code.
If you give the foreign application an Intent, and that application sends/broadcasts the Intent you gave, they will execute the Intent with their own permissions. But if you instead give the foreign application a Pending Intent you created using your own permission, that application will execute the contained Intent using your application’s permission.

To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent.getBroadcast(). To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity()

The reason it’s needed is because an Intent must be created and launched from a valid Context in your application, but there are certain cases where one is not available at the time you want to run the action because you are technically outside the application’s context (the two common examples are launching an Activity from a Notification or a BroadcastReceiver.

A PendingIntent provides a means for applications to work, even after their process exits. It€™s important to note that even after the application that created the PendingIntent has been killed, that Intent can still run. A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

Ref: www.simplecodestuffs.com/what-is-pending-intent-in-android/

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

 

Advertisements

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s