- Android - Intents/Filters
- Android - Fragments
- Android - Content Providers
- Android - Broadcast Receivers
- Android - Services
- Android - Activities
- Android - Resources
- Android - Hello World Example
- Android - Application Components
- Android - Architecture
- Android - Environment Setup
- Android - Overview
- Android - Home
Android - User Interface
- Android - Custom Components
- Android - Styles and Themes
- Android - Event Handling
- Android - UI Controls
- Android - UI Layouts
Android Advanced Concepts
- Publishing Android Application
- Android - Phone Calls
- Android - Sending SMS
- Android - Sending Email
- Location Based Services
- Android - Notifications
- Android - Drag and Drop
Android Useful Examples
- Android - XML Parsers
- Android - Widgets
- Android - Wi-Fi
- Android - WebView Layout
- Android - UI Testing
- Android - UI Patterns
- Android - UI Design
- Android - Twitter Integration
- Android - TextureView
- Android - Text to Speech
- Android - Testing
- Android - Support Library
- Android - SQLite Database
- Android - Spelling Checker
- Android - SIP Protocol
- Android - Shared Preferences
- Android - Session Management
- Android - Sensors
- Android - SDK Manager
- Android - Screen Cast
- Android - RSS Reader
- Android - RenderScript
- Android - Push Notification
- Android - ProgressBar
- Android - Progress Circle
- Android - PHP/MySQL
- Android - NFC Guide
- Android - Network Connection
- Android - Navigation
- Android - Multitouch
- Android - MediaPlayer
- Android - Login Screen
- Android - Localization
- Android - Loading Spinner
- Android - Linkedin Integration
- Android - JSON Parser
- Android - JetPlayer
- Android - Internal Storage
- Android - ImageSwitcher
- Android - Image Effects
- Android - Google Maps
- Android - Gestures
- Android - Facebook Integration
- Android - Emulator
- Android - Developer Tools
- Android - Data Backup
- Android - Custom Fonts
- Android - Clipboard
- Android - Camera
- Android - Bluetooth
- Android - Best Practices
- Android - Auto Complete
- Android - AudioManager
- Android - Audio Capture
- Android - Animations
- Android - Alert Dialoges
Android Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Android - Push Notification
A notification is a message you can display to the user outside of your apppcation 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.currentTimeMilps());
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 apppcation, you are granting it the right to perform the operation you have specified as if the other apppcation was yourself.
PendingIntent pending = PendingIntent.getActivity(getApppcationContext(), 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 notification subject and body details. Its syntax is given below. And then finally call the notify method of the NotificationManager class.
notify.setLatestEventInfo(getApppcationContext(), subject, body,pending); NM.notify(0, notify);
Apart from the notify method, there are other methods available in the NotificationManager class. They are psted 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 apppcation 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 Android studio to create an Android apppcation under a packagecom.example.sairamkrishna.myapppcation. |
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 | Run the apppcation and choose a running android device and install the apppcation on it and verify the results. |
Here is the content of MainActivity.java.
In the following code abc indicates the logo of tutorialspoint.com
package com.example.sairamkrishna.myapppcation; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; pubpc class MainActivity extends ActionBarActivity { EditText ed1,ed2,ed3; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1=(EditText)findViewById(R.id.editText); ed2=(EditText)findViewById(R.id.editText2); ed3=(EditText)findViewById(R.id.editText3); Button b1=(Button)findViewById(R.id.button); b1.setOnCpckListener(new View.OnCpckListener() { @Override pubpc void onCpck(View v) { String tittle=ed1.getText().toString().trim(); String subject=ed2.getText().toString().trim(); String body=ed3.getText().toString().trim(); NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notify=new Notification.Builder (getApppcationContext()).setContentTitle(tittle).setContentText(body). setContentTitle(subject).setSmallIcon(R.drawable.abc).build(); notify.flags |= Notification.FLAG_AUTO_CANCEL; notif.notify(0, notify); } }); } }
Here is the content of activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notification" android:id="@+id/textView" android:layout_apgnParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> . <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:textSize="35dp" android:textColor="#ff16ff01" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/textView2" android:layout_apgnLeft="@+id/textView2" android:layout_apgnStart="@+id/textView2" android:layout_marginTop="52dp" android:layout_apgnRight="@+id/textView2" android:layout_apgnEnd="@+id/textView2" android:hint="Name" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:hint="Subject" android:layout_below="@+id/editText" android:layout_apgnLeft="@+id/editText" android:layout_apgnStart="@+id/editText" android:layout_apgnRight="@+id/editText" android:layout_apgnEnd="@+id/editText" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/editText3" android:hint="Body" android:layout_below="@+id/editText2" android:layout_apgnLeft="@+id/editText2" android:layout_apgnStart="@+id/editText2" android:layout_apgnRight="@+id/editText2" android:layout_apgnEnd="@+id/editText2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notification" android:id="@+id/button" android:layout_marginTop="77dp" android:layout_below="@+id/editText3" android:layout_apgnRight="@+id/textView" android:layout_apgnEnd="@+id/textView" /> </RelativeLayout>
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.sairamkrishna.myapppcation" > <apppcation android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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> </apppcation> </manifest>
Let s try to run our apppcation. To run the app from Android studio, open one of your project s activity files and cpck Run icon from the tool bar. Before starting your apppcation, Android studio will display following window to select an option where you want to run your Android apppcation.
Now fill in the field with the title , subject and the body. This has been shown below in the figure −
Now cpck 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 −
Advertisements