×
Namespaces

Variants
Actions
(Difference between revisions)

Implementing Notifications in Nokia X

From Nokia Developer Wiki
Jump to: navigation, search
pardeepsharma10 (Talk | contribs)
(Pardeepsharma10 -)
BuildNokia (Talk | contribs)
(BuildNokia - Edited for clarity, formatted for wiki)
Line 23: Line 23:
  
 
== Introduction ==
 
== Introduction ==
Notifications are generally used by Background Apps to notify the User about certain events. This article is a step-by-step guide to create and delete notifications.
+
Notifications are generally used by Background Apps to notify the User about certain events. This article is a step-by-step guide for creating and deleting notifications.
  
 
==Implementation==
 
==Implementation==
  
 
===Create Notification===
 
===Create Notification===
1. Create a NotificationManager object to access System Notification Service.  
+
1. Create a {{Icode|NotificationManager}} object to access the System Notification Service.  
  
 
<code java>
 
<code java>
Line 34: Line 34:
 
</code>
 
</code>
  
2. Create Notification and Specify it's icon, App Name and time when Notification goes Live.
+
2. Create the notification and specify its icon, app name and the time when the notification should go live.
  
 
<code java>
 
<code java>
Line 40: Line 40:
 
</code>
 
</code>
  
3. Create a Pending Activity, on which control would be Transferred on click the Notification. in this case I am transferring control to the MainActivity but developer can select according to the Requirement.
+
3. Create a pending activity, to which control should be transferred on click the Notification. In this case I am transferring control to the {{Icode|MainActivity}} but the developer can select according to the application's requirements.
  
 
<code java>
 
<code java>
Line 49: Line 49:
 
</code>
 
</code>
  
4. Use notify() method of NotificationManager to Add the notification. this Method require two Argument, First is Notification ID and second is the notification created in the above steps. you need to specify Unique Notification ID because it can also be used to update and delete Notification.
+
4. Use the {{Icode|notify()}} method of {{Icode|NotificationManager}} to add the notification. This method require two argument: the notification ID, and the notification created in the above steps. You need to specify a unique notification ID because the ID you specify will also be used to update and delete the notification.
  
 
<code java>
 
<code java>
Line 55: Line 55:
 
</code>
 
</code>
  
On successful Completion of above steps, Notification would be successfully Generated.
+
On successful completion of the steps above, the notification is successfully generated.
  
Here is complete Code Again.
+
Here is the complete code.
  
 
<code java>
 
<code java>
Line 74: Line 74:
  
  
A simple Apps source code to create, update and Delete Notification is Attached. App have three buttons. on clicking Set Notification button a Notification is Created.  
+
A simple app's source code for that creates, updates and deletes a notification is attached. This app has three buttons. On clicking the '''Set Notification''' button a notification is created.  
  
 
<gallery>
 
<gallery>
Line 82: Line 82:
  
  
===Deleting Notification===
+
===Deleting a Notification===
It is possible to delete the Notification created by the App. Here is the Code
+
It is possible to delete the notification created by the app as follows:
  
 
<code java>
 
<code java>
Line 90: Line 90:
 
</code>
 
</code>
  
Above mentioned Code would Delete the Existing Notification having id 'NID'.
+
This code deletes the existing notification having the id 'NID'.
On Clicking Delete Notification Button in the Sample App, Notification would be Deleted
+
 
 +
On clicking the '''Delete Notification''' button in the sample app, the notification is deleted.
 +
 
 
===Updating Notification===
 
===Updating Notification===
Updating Notification is Similar as Creating Notification. Create a new Notification and use it as argument in the notify() method with NID of Notification which you want to Update.
+
Updating a notification is similar to creating a notification. You can create a new notification and use it as an argument to the {{Icode|notify()}} method with the NID of the otification you want to update.
  
Here is the Screenshot of Updated Notification
+
Here is a screenshot of an updated notification:
  
 
<gallery>
 
<gallery>
Line 102: Line 104:
  
 
</gallery>
 
</gallery>
 +
 
== Summary ==
 
== Summary ==
This Article explain easy implementation of Notification in NOKIA X.Suggestion are Welcome and Feel free to edit this Article.
+
This article explains how to implement notification in Nokia X.

Revision as of 02:23, 17 April 2014

This article explains how to create and delete notifications in Nokia X applications

Note.pngNote: This is an entry in the Nokia X Wiki Challenge 2014Q1

Article Metadata
Code ExampleArticle
Created: pardeepsharma10 (12 Apr 2014)
Last edited: BuildNokia (17 Apr 2014)

Contents

Introduction

Notifications are generally used by Background Apps to notify the User about certain events. This article is a step-by-step guide for creating and deleting notifications.

Implementation

Create Notification

1. Create a NotificationManager object to access the System Notification Service.

    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2. Create the notification and specify its icon, app name and the time when the notification should go live.

    Notification notification=new Notification(android.R.drawable.stat_notify_more, "Notification Alert", System.currentTimeMillis());

3. Create a pending activity, to which control should be transferred on click the Notification. In this case I am transferring control to the MainActivity but the developer can select according to the application's requirements.

    Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
 
notification.setLatestEventInfo(context, title, detail, pending);

4. Use the notify() method of NotificationManager to add the notification. This method require two argument: the notification ID, and the notification created in the above steps. You need to specify a unique notification ID because the ID you specify will also be used to update and delete the notification.

    nm.notify(NID, notification);

On successful completion of the steps above, the notification is successfully generated.

Here is the complete code.

    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(android.R.drawable.stat_notify_more, "this is important", System.currentTimeMillis());
 
Context context=MainActivity.this;
CharSequence title="hello there";
CharSequence detail="this is demo";
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
 
notification.setLatestEventInfo(context, title, detail, pending);
nm.notify(NID, notification);


A simple app's source code for that creates, updates and deletes a notification is attached. This app has three buttons. On clicking the Set Notification button a notification is created.


Deleting a Notification

It is possible to delete the notification created by the app as follows:

    NotificationManager nMgr = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(NID);

This code deletes the existing notification having the id 'NID'.

On clicking the Delete Notification button in the sample app, the notification is deleted.

Updating Notification

Updating a notification is similar to creating a notification. You can create a new notification and use it as an argument to the notify() method with the NID of the otification you want to update.

Here is a screenshot of an updated notification:

Summary

This article explains how to implement notification in Nokia X.

173 page views in the last 30 days.
×