Showing posts with label NotificationManager. Show all posts
Showing posts with label NotificationManager. Show all posts

How to Post a Notification to be Shown in the Status Bar

public void notify (int id, Notification notification) was added in API level 1
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
Parameters:
id - An identifier for this notification unique within your application.
notification - A Notification object describing what to show the user. Must not be null.

1.In the MainActivity.java file,  Get a Reference to NotificationManager

2. Get the Global Application Context and save it to myContext.

3. Create an Intent for an Activity. Set the Activity name to MainActivity.

4. Set a PendingIntent with getActivity, using a PendingIntent named myPendingIntent. This PendingIntent will be fired if a user clicks the Notification item.

5. Deprecated: Set the Title and Text of a Notification. Use the Context named myContext. Set the title to "This is the title." Set the text to "This is my Text". Set the PendingIntent to myPendingIntent.

6. Add the below line to the onCreate method. This will post the notification to status bar.

myNotificationManager.notify(1, myNotification);

7. Compile and run!


How to Get a Reference to NotificationManager

public abstract Object getSystemService (String name) was added in API level 1
Return the handle to a system-level service by name. The class of the returned object varies by the requested name.

1. In the MainActivity.java file, Save the NotificationServce to a string named svcName.

2. Declare a new NotificationManager named myNotificationManager

3. In the onCreate method, add the following line. This will get a reference to the NotificationManager and save it to myNotificationManager.

myNotificationManager = (NotificationManager)getSystemService(svcName);

4. Compile and run!

Next Recommended Article: How to Save a Drawable to an Integer object

Resources:
http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)

How to Declare a NotificationManager

NotificationManager was added in API Level 1
Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.

1. In the MainActivity.java file, add the below line to the imports section.

import android.app.NotificationManager;

2. In the onCreate method, add the following line. This will create a new NotificationManager object named myNotificationManager.

NotificationManager myNotificationManager;

3. Compile and run!

Next Recommended Article: How to Get a Reference to NotificationManager

Resources:
http://developer.android.com/reference/android/app/NotificationManager.html