×
Namespaces

Variants
Actions
(Difference between revisions)

Implementing Notifications in Nokia X

From Nokia Developer Wiki
Jump to: navigation, search
BuildNokia (Talk | contribs)
pardeepsharma10 (Talk | contribs)
(Pardeepsharma10 -)
Line 95: Line 95:
  
 
===Updating Notification===
 
===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 {{Icode|notify()}} method with the NID of the otification 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 notification you want to update.
  
 
Here is a screenshot of an updated notification:
 
Here is a screenshot of an updated notification:

Revision as of 03:38, 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: pardeepsharma10 (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 notification you want to update.

Here is a screenshot of an updated notification:

Summary

This article explains how to implement notification in Nokia X.

172 page views in the last 30 days.
×