×
Namespaces

Variants
Actions
(Difference between revisions)

Implementing Notifications in Nokia X

From Nokia Developer Wiki
Jump to: navigation, search
mfabiop (Talk | contribs)
(Mfabiop -)
mfabiop (Talk | contribs)
(Mfabiop -)
Line 1: Line 1:
 
[[Category:Nokia X 1.0]][[Category:Nokia X]]
 
[[Category:Nokia X 1.0]][[Category:Nokia X]]
{{Abstract|This Article explain how to create and Delete Notification in Nokia X App}}
+
{{Abstract|This article explains how to create and delete notifications in Nokia X applications}}
 
{{Note|This is an entry in the [[Nokia X Wiki Challenge 2014Q1]]}}
 
{{Note|This is an entry in the [[Nokia X Wiki Challenge 2014Q1]]}}
 
{{ArticleMetaData <!-- v1.3 -->
 
{{ArticleMetaData <!-- v1.3 -->

Revision as of 16:13, 15 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 (15 Apr 2014 06 May 2014)
Last edited: mfabiop (15 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 to create and delete notifications.

Implementation

Create Notification

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

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

2. Create Notification and Specify it's icon, App Name and time when Notification goes Live.

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

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.

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

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.

    nm.notify(NID, notification);

On successful Completion of above steps, Notification would be successfully Generated.

Here is complete Code Again.

    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 Apps source code to create, update and Delete Notification is Attached. App have three buttons. on clicking Set Notification button a Notification is Created.


Deleting Notification

It is possible to delete the Notification created by the App. Here is the Code

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

Above mentioned Code would Delete the Existing Notification having id 'NID'. On Clicking Delete Notification Button in the Sample App, Notification would be Deleted

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.

Here is the Screenshot of Updated Notification

Summary

This Article explain easy implementation of Notification in NOKIA X.Suggestion are Welcome and Feel free to edit this Article.

91 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×