Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Implementing Notifications in Nokia X

From Wiki
Jump to: navigation, search

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: pardeepsharma10 (22 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 timestamp of notification .

    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 notification you want to update.

Here is a screenshot of an updated notification:

Summary

This article explains how to implement notification in Nokia X.If you want to see actual implementation of this code, Download Body Fat available on the Nokia Store.

This page was last modified on 22 April 2014, at 02:48.
251 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.

×