×
Namespaces

Variants
Actions
(Difference between revisions)

Implementing Notifications in Nokia X

From Nokia Developer Wiki
Jump to: navigation, search
pardeepsharma10 (Talk | contribs)
(Pardeepsharma10 -)
mfabiop (Talk | contribs)
(Mfabiop -)
Line 31: Line 31:
 
1. Create NotificationManager object to access System Notification Service.  
 
1. Create NotificationManager object to access System Notification Service.  
  
<code-java>
+
<code java>
        NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+
    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 
</code>
 
</code>
  
 
2. Create Notification and Specify it's icon, App Name and time when Notification goes Live.
 
2. Create Notification and Specify it's icon, App Name and time when Notification goes Live.
  
<code-java>
+
<code java>
        Notification notification=new Notification(android.R.drawable.stat_notify_more, "Notification Alert",  
+
    Notification notification=new Notification(android.R.drawable.stat_notify_more, "Notification Alert", System.currentTimeMillis());
      System.currentTimeMillis());
+
 
</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, 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.
  
<code-java>
+
<code java>
        Intent intent=new Intent(context,MainActivity.class);
+
    Intent intent=new Intent(context,MainActivity.class);
 
+
    PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
    PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
+
   
 
+
    notification.setLatestEventInfo(context, title, detail, pending);
    notification.setLatestEventInfo(context, title, detail, pending);
+
 
</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 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.
  
<code-java>
+
<code java>
      nm.notify(NID, notification);
+
    nm.notify(NID, notification);
 
</code>
 
</code>
  
Line 62: Line 60:
 
Here is complete Code Again.
 
Here is complete Code Again.
  
<code-java>
+
<code java>
        NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+
    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 +
    Notification notification=new Notification(android.R.drawable.stat_notify_more, "this is important", System.currentTimeMillis());
  
    Notification notification=new Notification(android.R.drawable.stat_notify_more, "this is important",
+
    Context context=MainActivity.this;
        System.currentTimeMillis());
+
    CharSequence title="hello there";
 
+
    CharSequence detail="this is demo";
    Context context=MainActivity.this;
+
    Intent intent=new Intent(context,MainActivity.class);
 
+
    PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
    CharSequence title="hello there";
+
   
 
+
    notification.setLatestEventInfo(context, title, detail, pending);
    CharSequence detail="this is demo";
+
    nm.notify(NID, notification);
 
+
    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);
+
 
</code>
 
</code>
  
Line 95: Line 86:
 
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. Here is the Code
  
<code-java>
+
<code java>
NotificationManager nMgr = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
+
    NotificationManager nMgr = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
 
+
    nMgr.cancel(NID);
nMgr.cancel(NID);
+
 
</code>
 
</code>
  

Revision as of 19:06, 15 April 2014

This Article explain how to create and Delete Notification in NOKIA X App

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

Notification are generally used by the Background Apps to notify the User about certain Events. This Article explain step-by-step procedure of creating and Deleting Notification.


Implementation

Create Notification

1. Create 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.

179 page views in the last 30 days.
×