Android Push Notification Tutorial
A notification is a message you can display to the user outside of your application's normal UI. You can create your own notifications in android very easily.
Android provides NotificationManager class for this purpose. In order to use this class, you need to instantiate an object of this class by requesting the android system through getSystemService() method. Its syntax is given below:
NotificationManager NM; NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
After that you will create Notification through Notification class and specify its attributes such as icon,title and time e.t.c. Its syntax is given below:
Notification notify=new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());
The next thing you need to do is to create a PendingIntent by passing context and intent as a parameter. By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself.
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);
The last thing you need to do is to call setLatestEventInfo method of the Notification class and pass the pending intent along with notfication subject and body details. Its syntax is given below. And then finally call the notify method of the NotificationManager class.
notify.setLatestEventInfo(getApplicationContext(), subject, body,pending); NM.notify(0, notify);
Apart from the notify method, there are other methods availaible in the NotificationManager class. They are listed below:
Sr.No | Method & description |
---|---|
1 | cancel(int id) This method cancel a previously shown notification. |
2 | cancel(String tag, int id) This method also cancel a previously shown notification. |
3 | cancelAll() This method cancel all previously shown notifications. |
4 | notify(int id, Notification notification) This method post a notification to be shown in the status bar. |
5 | notify(String tag, int id, Notification notification) This method also Post a notification to be shown in the status bar. |
Example
The below example demonstrates the use of NotificationManager class. It crates a basic application that allows you to create a notification.
To experiment with this example , you need to run this on an actual device or in an emulator.
Steps | Description |
---|---|
1 | You will use Eclipse IDE to create an Android application and name it as Status under a package com.example.status. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs. |
2 | Modify src/MainActivity.java file to add Notification code. |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Modify res/values/string.xml file and add necessary string components. |
5 | Run the application and choose a running android device and install the application on it and verify the results. |
Here is the content of src/com.example.status/MainActivity.java.
package com.example.status; 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.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { NotificationManager NM; EditText one,two,three; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (EditText)findViewById(R.id.editText1); two = (EditText)findViewById(R.id.editText2); three = (EditText)findViewById(R.id.editText3); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @SuppressWarnings("deprecation") public void notify(View vobj){ String title = one.getText().toString(); String subject = two.getText().toString(); String body = three.getText().toString(); NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notify=new Notification(android.R.drawable. stat_notify_more,title,System.currentTimeMillis()); PendingIntent pending=PendingIntent.getActivity( getApplicationContext(),0, new Intent(),0); notify.setLatestEventInfo(getApplicationContext(),subject,body,pending); NM.notify(0, notify); } }
Here is the content of activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="86dp" android:onClick="notify" android:text="@string/notification" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="53dp" android:ems="10" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="28dp" android:ems="10" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="23dp" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/editText1" android:layout_marginRight="14dp" android:layout_toLeftOf="@+id/editText1" android:text="@string/title" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/editText3" android:layout_alignRight="@+id/textView1" android:text="@string/heading" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/editText3" android:layout_alignLeft="@+id/textView2" android:text="@string/body" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView2" android:text="@string/create" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Status</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="notification">Notify</string> <string name="title">Title</string> <string name="heading">Subject</string> <string name="body">Body</string> <string name="create">Create Notification</string> </resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.status" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.status.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your TextToSpeech application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen:
Now fill in the field with the title , subject and the body. This has been shown below in the figure:
Now click on the notify button and you will see a notification in the top notification bar. It has been shown below:
Now scroll down the notification bar and see the notification. This has been shown below in the figure:
Comments
Post a Comment