When we talk about notification we need to consider 3 major aspects.
1. What is Notification
2. Why Notification
3. How to create Notification
What is Notification:
Notification are the messages which are produced at top in the status bar in your android phones. Its a service which is used to show data of your application to the users even when the application is not running on the screen.
Why Notification
Something similar to you SMS Application. As soon as you recieve a SMS you recieve a notification at the top of your screen. The same results can be obtained by your application when using Notification.
Hence, multiple number of times when you are doing some background processes using your Services (e.g. Server request to check for new data on server) and you want to tell user that you have obtained new data and he should launch your application, at this point you will show them Notification message stating that new data has arrived.
How To Use
/**
* Copyright [2010] [Abhinava Srivastava]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package abhi.blog.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
/**
* Notification Example class
*
* @author Abhinava Srivastava
*
*/
public class NotificationCaller extends Activity {
// PASSED AS PAREMETER TO NOTIFICATION
private final int NOTIFICATION_ID = 0;
// NotificationManager Reference Holder
private NotificationManager mNotificationManager = null;
// NOtification Reference Holder
private Notification mNotification = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* Need to use NotificationManager Instance of NotificationManger is
* available in our SystemServices which is available in our context.
*/
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/**
* To clear any existing NOtification with same ID
*/
mNotificationManager.cancel(NOTIFICATION_ID);
}
/**
* Function to handle click event generated by our button
*
* @param v
*/
public void launchNotification(View v) {
/**
* After we have obtained instance of notification manager we need to
* create the notification that we need to show The Constructor of
* Notification takes in 3 Parameters 1. Resource id of icon you want to
* show. 2. Text that should be shown 3. When do you wish to shoe this
* Notification
*/
mNotification = new Notification(R.drawable.icon,
"abhinavasblog.blogspot.com", System.currentTimeMillis());
/**
* Need to setup the view that you need show when user expands his/her
* Status bar.
*/
mNotification.contentView = new RemoteViews("abhi.blog.notification",
R.layout.special_notification);
/**
* Setting up the PendingIntent (Pending Intent is a special kind of
* intent which can be invoked at any specific event) Here we launch our
* application again as soon as we click on the Notification
*/
mNotification.contentIntent = PendingIntent.getActivity(
NotificationCaller.this, 0, new Intent(NotificationCaller.this,
NotificationCaller.class),
PendingIntent.FLAG_CANCEL_CURRENT);
/**
* Finally, we set the created Notification in NotificationManagers
* instance.
*/
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
}
main.xmlandroid:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
special_notification.xmlxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content">
Wish you all a Very Happy New Year
1 comment:
This does not help . It throws exception saying that , it found bad remote view for given package .
Post a Comment