Show Notification in StatusBar

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Show Notification in StatusBar

Postby plusminus » Fri Feb 22, 2008 11:08 pm

Show Notification in StatusBar

What you will learn: You will learn how to show Notifications in the StatusBar.

:idea: Designed/Tested with sdk-version: m5-rc14

:?: Problems/Questions: Write it right below...

Difficulty: 2 of 5 :)

What it will look like:
Image Image Image Image Image


Description:
Description follows tomorrow... ;)


The full source:
"/res/layout/main.xml"
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <Button  
  8.         android:id="@+id/btn_showsample"
  9.    android:layout_width="fill_parent"
  10.    android:layout_height="wrap_content"
  11.    android:text="Show sample Notification." />
  12. <Button  
  13.         android:id="@+id/btn_clear"
  14.    android:layout_width="fill_parent"
  15.    android:layout_height="wrap_content"
  16.    android:text="Clear sample Notification." />
  17. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



"/src/your_package_structure/StatusbarNotificator.java"
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.statusbarnotificator;
  2.  
  3. import android.app.Activity;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11.  
  12. public class StatusbarNotificator extends Activity {
  13.        
  14.         private NotificationManager mNotificationManager;
  15.         private int YOURAPP_NOTIFICATION_ID;
  16.        
  17.     /** Called when the activity is first created. */
  18.     @Override
  19.     public void onCreate(Bundle icicle) {
  20.         super.onCreate(icicle);
  21.         setContentView(R.layout.main);
  22.        
  23.         // Get the notification manager service.
  24.         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  25.        
  26.         /* Show a sample notification when the first button was clicked. */
  27.         findViewById(R.id.btn_showsample).setOnClickListener(new OnClickListener(){
  28.                         @Override
  29.                         public void onClick(View v) {
  30.                                 StatusbarNotificator.this.showNotification(
  31.                                                 R.drawable.tinyandroid,
  32.                                                 R.string.notif_short,
  33.                                                 R.string.notif_detailed,
  34.                                                 false);
  35.                         }
  36.         });
  37.        
  38.         /* Clear the notification. */
  39.         findViewById(R.id.btn_clear).setOnClickListener(new OnClickListener(){
  40.                         @Override
  41.                         public void onClick(View v) {
  42.                                 StatusbarNotificator.this.mNotificationManager.cancel(YOURAPP_NOTIFICATION_ID);
  43.                         }
  44.         });
  45.     }
  46.    
  47.     private void showNotification(int statusBarIconID, int statusBarTextID, int detailedTextID, boolean showIconOnly) {
  48.         // This is who should be launched if the user selects our notification.
  49.         Intent contentIntent = new Intent(this, StatusbarNotificator.class);
  50.  
  51.         // This is who should be launched if the user selects the app icon in the notification.
  52.         Intent appIntent = new Intent(android.content.Intent.VIEW_ACTION,
  53.                         Uri.parse("http://www.anddev.org"));
  54.  
  55.         // choose the ticker text
  56.         String tickerText = showIconOnly ? null : this.getString(statusBarTextID);
  57.  
  58.         mNotificationManager.notify(
  59.                    YOURAPP_NOTIFICATION_ID, // we use a string id because it is a unique
  60.                                                       // number.  we use it later to cancel the
  61.                                                       // notification
  62.                    new Notification(
  63.                        this,                        // our context
  64.                        statusBarIconID,             // the icon for the status bar
  65.                        tickerText,                  // the text to display in the ticker
  66.                        System.currentTimeMillis(),  // the timestamp for the notification
  67.                        "anddev.org - Notification", // the title for the notification
  68.                        getText(detailedTextID),     // the details to display in the notification
  69.                        contentIntent,               // the contentIntent (see above)
  70.                        R.drawable.icon,                         // the app icon
  71.                        "anddev",                                        // the name of the app
  72.                        appIntent));                 // the appIntent (see above)
  73.     }
  74. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

That's it :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Re: Show Notification in StatusBar

Postby mhobbins » Tue Mar 18, 2008 3:30 pm

Hello pluminus, the Notification functionality in SDK m3 had the option to set the message on the status bar, which automatically disappeared after a couple of seconds. Is this still available on SDK m5? An alternate solution (but not smart) would be to have a service running, clearing the message every so often.

Rergards
Michael
mhobbins
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 07, 2008 1:07 am

Postby E.Jyothsna » Tue Apr 22, 2008 1:50 pm

in my status bar notification it will display one message icon.after reading my message also dat icon is appering.i want just like our mobiles,i mean once we saw message dat symbol immediately will go.just like that i want in status notification bar.how can i do it?plz reply me
E.Jyothsna
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:25 pm
Location: chennai

Postby E.Jyothsna » Wed Apr 23, 2008 4:16 pm

once see my code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mobeegal.android;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.app.Notification;
  8.  
  9. import android.app.NotificationManager;
  10.  
  11. import android.content.Context;
  12.  
  13. import android.content.Intent;
  14.  
  15. import android.content.IntentReceiver;
  16.  
  17. import android.os.Bundle;
  18.  
  19. import android.widget.Toast;
  20.  
  21. import android.util.*;
  22.  
  23. import java.io.IOException;
  24.  
  25.  
  26.  
  27. import org.apache.commons.httpclient.HttpClient;
  28.  
  29. import org.apache.commons.httpclient.NameValuePair;
  30.  
  31. import org.apache.commons.httpclient.methods.PostMethod;
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public class RepeatingRequest extends IntentReceiver
  38.  
  39. {
  40.  
  41.  
  42.  
  43.         public Welcome welcome;
  44.  
  45.         String sendingService;
  46.  
  47.         String gettingvalue;
  48.  
  49.        
  50.  
  51.             public void onReceiveIntent(Context context, Intent intent)
  52.  
  53.     {
  54.  
  55.                
  56.  
  57.         try{
  58.  
  59.             HttpClient httpclient;
  60.  
  61.             httpclient = new HttpClient();
  62.  
  63.             final Bundle bundles = intent.getExtras();
  64.  
  65.             if(bundles!= null) {
  66.  
  67.                sendingService = bundles.getString("passingcategory");//getting value from spinner
  68.  
  69.                sendingService = "Dating";//statically assigned the service
  70.  
  71.                 }
  72.  
  73.                 //String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
  74.  
  75.             String IMSI="310995000000002";
  76.  
  77.             NameValuePair[] data = {new NameValuePair("IMSI", IMSI),new NameValuePair("Service",sendingService)};
  78.  
  79.             PostMethod httpPost = new PostMethod("http://192.168.1.6/index.php");
  80.  
  81.             httpPost.setRequestBody(data);
  82.  
  83.             httpPost.setQueryString(data);
  84.  
  85.             httpclient.executeMethod(httpPost);
  86.  
  87.             String serverresponse1 = httpPost.getResponseBodyAsString();
  88.  
  89.             String serverresponse = httpPost.getQueryString();//sim no reciving
  90.  
  91.             //bundles.putString("serverres", serverresponse);
  92.  
  93.             Toast.makeText(context,serverresponse+"   " + "Response from server is : "+ serverresponse1,Toast.LENGTH_LONG).show();
  94.  
  95.            
  96.  
  97.          
  98.  
  99.             MatchingData matching=new MatchingData();
  100.  
  101.             matching.showNotification(serverresponse1);
  102.  
  103.             Log.i("Matching Data", serverresponse1);
  104.  
  105.             // bundles.putString("serverres",serverres);
  106.  
  107.            
  108.  
  109.             /*---------------------------------*/
  110.  
  111.            
  112.  
  113.         }
  114.  
  115.      catch(IOException e){}
  116.  
  117.      catch (NullPointerException ne) {
  118.  
  119.  
  120.  
  121.         }
  122.  
  123.     }
  124.  
  125.    
  126.  
  127. class MatchingData extends Activity{
  128.  
  129.  public void showNotification(String string) {
  130.  
  131.        
  132.  
  133.          NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  134.  
  135.      CharSequence from = "Matching Data";
  136.  
  137.      CharSequence message = string;
  138.  
  139.      String tickerText = getString(R.string.incoming_message_ticker_text, message);
  140.  
  141.      Intent appIntent = new Intent();
  142.  
  143.      /*Bundle b12 = new Bundle();
  144.  
  145.      b12.putString("serverres", string);*/
  146.  
  147.      Intent contentIntent = new Intent();
  148.  
  149.      /*contentIntent.putExtras(b12);*/
  150.  
  151.      //startActivity(contentIntent);
  152.  
  153.      Notification notif = new Notification(
  154.  
  155.              this, // our context
  156.  
  157.              R.drawable.message, // the icon for the status bar
  158.  
  159.              tickerText, // the text to display in the ticker
  160.  
  161.              System.currentTimeMillis(), // the timestamp for the notification
  162.  
  163.              from, // the title for the notification
  164.  
  165.              message, // the details to display in the notification
  166.  
  167.              contentIntent, // the contentIntent (see above)
  168.  
  169.              R.drawable.message, // the app icon
  170.  
  171.              getText(R.string.app_name), // the name of the app
  172.  
  173.              appIntent);
  174.  
  175.      // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
  176.  
  177.      // then vibrate for 500ms.
  178.  
  179.      notif.vibrate = new long[]{100, 250, 100, 500};
  180.  
  181.      nm.notify(0, notif);
  182.  
  183.     // nm.cancel(R.drawable.message);
  184.  
  185.       nm.cancel(R.string.incoming_message_ticker_text);
  186.  
  187.                        
  188.  
  189.                 }
  190.  
  191.  
  192.  
  193. }
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4

in this code, what ever code i am giving after "matching.showNotification(serverresponse1);"that is not working.Why?wat ever code giving before this line it is comming.give me fast reply.Its so urgent
E.Jyothsna
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:25 pm
Location: chennai

Postby CraigW » Mon May 26, 2008 4:46 am

Having a issue with the overriding of onClickView, I have a direct copy of your code however I receive the error

"The method onClick(View) of type new View.OnClickListener(){} must override a superclass method"

Any ideas?
CraigW
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 26, 2008 4:42 am

Postby E.Jyothsna » Mon May 26, 2008 4:59 am

try this one i think it will work.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mobeegal.android;
  2.  
  3.  
  4.  
  5. import android.app.Notification;
  6.  
  7. import android.app.NotificationManager;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.content.Intent;
  12.  
  13. import android.content.IntentReceiver;
  14.  
  15. import android.database.Cursor;
  16.  
  17. import android.database.sqlite.SQLiteDatabase;
  18.  
  19. import android.os.Bundle;
  20.  
  21. import java.io.IOException;
  22.  
  23. import java.util.logging.Level;
  24.  
  25. import java.util.logging.Logger;
  26.  
  27. import android.content.Intent;
  28.  
  29. import android.database.Cursor;
  30.  
  31. import android.database.sqlite.SQLiteDatabase;
  32.  
  33. import android.os.Bundle;
  34.  
  35. import java.io.IOException;
  36.  
  37. import java.util.logging.Logger;
  38.  
  39. import android.app.Notification;
  40.  
  41. import android.app.NotificationManager;
  42.  
  43. import android.content.Context;
  44.  
  45. import android.content.Intent;
  46.  
  47. import android.content.IntentReceiver;
  48.  
  49. import android.database.Cursor;
  50.  
  51. import android.database.sqlite.SQLiteDatabase;
  52.  
  53. import android.os.Bundle;
  54.  
  55. import java.io.IOException;
  56.  
  57. import java.util.logging.Level;
  58.  
  59. import java.util.logging.Logger;
  60.  
  61. import android.content.Intent;
  62.  
  63. import android.database.Cursor;
  64.  
  65. import android.database.sqlite.SQLiteDatabase;
  66.  
  67. import android.os.Bundle;
  68.  
  69. import android.widget.Toast;
  70.  
  71. import java.io.IOException;
  72.  
  73. import java.util.ArrayList;
  74.  
  75. import java.util.logging.Logger;
  76.  
  77. import org.apache.commons.httpclient.HttpClient;
  78.  
  79. import org.apache.commons.httpclient.NameValuePair;
  80.  
  81. //import org.apache.commons.httpclient.methods.PostMethod;
  82.  
  83. import org.apache.commons.httpclient.methods.PostMethod;
  84.  
  85. import java.util.ArrayList;
  86.  
  87.  
  88.  
  89. public class RepeatingRequest extends IntentReceiver {
  90.  
  91.  
  92.  
  93.     private String catalogname1;
  94.  
  95.     private String catalogimsi1;
  96.  
  97.     private String mStuffresults;
  98.  
  99.     private String Longitude;
  100.  
  101.     private String Latitude;
  102.  
  103.     private String Country;
  104.  
  105.     private String City;
  106.  
  107.     private String Area;
  108.  
  109.     private String Weight;
  110.  
  111.     private String Height;
  112.  
  113.     private String Sex;
  114.  
  115.     private String Age;
  116.  
  117.     private String Catalog;
  118.  
  119.     private String Id;
  120.  
  121.     private final String ActivateMessage = "Activate the Service";
  122.  
  123.     private final String mY_DATING_DATABASE = "Mobeegal";
  124.  
  125.     private final String mY_IStuff_TABLE = "IStuff";
  126.  
  127.     private final String mY_UStuff_TABLE = "UStuff";
  128.  
  129.     private final String mY_MobeegalUser_TABLE = "MobeegalUser";
  130.  
  131.     private SQLiteDatabase myDB = null;
  132.  
  133.     String iStufftoServer = null;
  134.  
  135.     String iStuffPost = null;
  136.  
  137.     String uStufftoServer = null;
  138.  
  139.     String uStuffPost = null;
  140.  
  141.     String iStuffAge = null;
  142.  
  143.     String iStuffSex = null;
  144.  
  145.     String iStuffHeight = null;
  146.  
  147.     String iStuffWeight = null;
  148.  
  149.     String iStuffLocation = null;
  150.  
  151.     String iStuffCity = null;
  152.  
  153.     String iStuffCountry = null;
  154.  
  155.     String uStuffAge = null;
  156.  
  157.     String uStuffSex = null;
  158.  
  159.     String uStuffHeight = null;
  160.  
  161.     String uStuffWeight = null;
  162.  
  163.     String uStuffLocation = null;
  164.  
  165.     String uStuffCity = null;
  166.  
  167.     String uStuffCountry = null;
  168.  
  169.     String useridColumn = null;
  170.  
  171.     ArrayList results = new ArrayList();
  172.  
  173.     private static Logger logger = Logger.getLogger("Request Response");
  174.  
  175.  
  176.  
  177.     @Override
  178.  
  179.     public void onReceiveIntent(Context context, Intent intent) {
  180.  
  181.         NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  182.  
  183. //Fetching MStuff data from MStuffstore Table
  184.  
  185.         try {
  186.  
  187.             myDB = context.openDatabase("Mobeegal", null);
  188.  
  189.             String[] col = {"mStuffId", " mStuffCatalog", " mStuffAge", "mStuffsex", "mStuffHeight", "mStuffWeight", "mStuffArea", "mStuffCity", "mStuffcountry", "mStuffLatitude", "mStuffLongitude"};
  190.  
  191.             Cursor c = myDB.query(true, "MStuffstore", col, null, null, null,
  192.  
  193.                     null, null);
  194.  
  195.             int id = c.getColumnIndex("mStuffId");
  196.  
  197.             int catalog = c.getColumnIndex("mStuffCatalog");
  198.  
  199.             int age = c.getColumnIndex("mStuffAge");
  200.  
  201.             int sex = c.getColumnIndex("mStuffsex");
  202.  
  203.             int height = c.getColumnIndex("mStuffHeight");
  204.  
  205.             int weight = c.getColumnIndex("mStuffWeight");
  206.  
  207.             int area = c.getColumnIndex("mStuffArea");
  208.  
  209.             int city = c.getColumnIndex("mStuffCity");
  210.  
  211.             int country = c.getColumnIndex("mStuffcountry");
  212.  
  213.             int latitude = c.getColumnIndex("mStuffLatitude");
  214.  
  215.             int longitude = c.getColumnIndex("mStuffLongitude");
  216.  
  217.  
  218.  
  219.             if (c != null) {
  220.  
  221.                 if (c.first()) {
  222.  
  223.                     do {
  224.  
  225.                         Id = c.getString(id);
  226.  
  227.                         Catalog = c.getString(catalog);
  228.  
  229.                         Age = c.getString(age);
  230.  
  231.                         Sex = c.getString(sex);
  232.  
  233.                         Height = c.getString(height);
  234.  
  235.                         Weight = c.getString(weight);
  236.  
  237.                         Area = c.getString(area);
  238.  
  239.                         City = c.getString(city);
  240.  
  241.                         Country = c.getString(country);
  242.  
  243.                         Latitude = c.getString(latitude);
  244.  
  245.                         Longitude = c.getString(longitude);
  246.  
  247.                         results.add(Id);
  248.  
  249.                         results.add(Catalog);
  250.  
  251.                         results.add(Age);
  252.  
  253.                         results.add(Sex);
  254.  
  255.                         results.add(Height);
  256.  
  257.                         results.add(Weight);
  258.  
  259.                         results.add(Area);
  260.  
  261.                         results.add(City);
  262.  
  263.                         results.add(Country);
  264.  
  265.                         results.add(Latitude);
  266.  
  267.                         results.add(Latitude);
  268.  
  269.                         results.add(Longitude);
  270.  
  271.                     //results.add()
  272.  
  273.                     } while (c.next());
  274.  
  275.                 }
  276.  
  277.             }
  278.  
  279.             mStuffresults = results.toString();
  280.  
  281.  
  282.  
  283.             CharSequence from = "Matching Data";
  284.  
  285.             CharSequence message = mStuffresults;
  286.  
  287.             String tickerText = "New text Message";
  288.  
  289.             Intent appIntent = new Intent();
  290.  
  291.             Intent contentIntent = new Intent(context, DisplayingMStuff.class);
  292.  
  293.             Bundle b12 = new Bundle();
  294.  
  295.             b12.putString("serverresponse", mStuffresults);
  296.  
  297.             contentIntent.putExtras(b12);
  298.  
  299.             Notification notif = new Notification(context,
  300.  
  301.                     R.drawable.mobeegal1,
  302.  
  303.                     tickerText,
  304.  
  305.                     System.currentTimeMillis(),
  306.  
  307.                     from,
  308.  
  309.                     message,
  310.  
  311.                     contentIntent,
  312.  
  313.                     R.drawable.mobeegal1,
  314.  
  315.                     "Mobeegal",
  316.  
  317.                     appIntent);
  318.  
  319.             notif.vibrate = new long[]{100, 250, 100, 500};
  320.  
  321.             nm.notify(R.string.notification_message, notif);
  322.  
  323.         } catch (IOException ex) {
  324.  
  325.             Logger.getLogger(RepeatingRequest.class.getName()).log(Level.SEVERE, null, ex);
  326.  
  327.         }
  328.  
  329.        }
  330.  
  331. }
Parsed in 0.065 seconds, using GeSHi 1.0.8.4
E.Jyothsna
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:25 pm
Location: chennai

Top

Notification in the latest sdk 1.0?

Postby michang05 » Wed Nov 05, 2008 4:11 am

are they the same or not can you guys give us some examples, btw im using a Chronometer and i wanted to run it in the background if i exit the app and it will show in the notification status bar and when clicked it a timer will then be seen .

one more thing is the back button of the emulator is the only way to exit an activity? how about if i have one activity like MapView?

thanks
Everybody Lies!
michang05
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Sep 29, 2008 11:15 am
Location: Philippines

Postby soniya » Tue Nov 11, 2008 3:32 pm

i used in new sdk
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent contentIntent = new Intent(this,KeypadLayout.class);
  2.  
  3.         contentIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
  4.  
  5.         PendingIntent appIntent =PendingIntent.getActivity(this,0, contentIntent, 0);
  6.  
  7.         Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());
  8.  
  9.         notification.setLatestEventInfo(c,  getText(R.string.app_name), getText(R.string.app_name), appIntent);
  10.  
  11.         mNM.notify(0, notification);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


but can't open new Activity (KeypadLayout class)..plz tel me ..what is wrong.???
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby Artur79 » Sun Nov 23, 2008 2:11 pm

Eclipse show errors when trying to compile this part of code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2. mNotificationManager.notify(
  3.                    YOURAPP_NOTIFICATION_ID, // we use a string id because it is a unique
  4.                                                       // number.  we use it later to cancel the
  5.                                                       // notification
  6.                    new Notification(
  7.                        this,                        // our context
  8.                        statusBarIconID,             // the icon for the status bar
  9.                        tickerText,                  // the text to display in the ticker
  10.                        System.currentTimeMillis(),  // the timestamp for the notification
  11.                        "anddev.org - Notification", // the title for the notification
  12.                        getText(detailedTextID),     // the details to display in the notification
  13.                        contentIntent,               // the contentIntent (see above)
  14.                        R.drawable.icon,                // the app icon
  15.                        "anddev",                       // the name of the app
  16.                        appIntent));                 // the appIntent (see above)
  17.     }
  18. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


I've found in manual (http://code.google.com/android/reference/android/app/Notification.html) that this input params have to be splited between constructor and setLatestEventInfo() method, but still don't know what I have to use as the first and last param, any ideas ?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2. Notification nt = new Notification(
  3.                 statusBarIconID,             // the icon for the status bar
  4.                 tickerText,                  // the text to display in the ticker
  5.                 System.currentTimeMillis()  // the timestamp for the notification
  6.         );
  7.  
  8.         nt.setLatestEventInfo(
  9.                 ??????????
  10.                         "anddev.org - Notification",
  11.                          getText(detailedTextID),
  12.                 ??????????
  13.         );
  14.  
  15.         mNotificationManager.notify(YOURAPP_NOTIFICATION_ID,nt);        
  16.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Postby MrSnowflake » Sun Nov 23, 2008 2:16 pm

The first parameter is the context of the Application which creates the notification, and the last is the Intent to launch when the user touches the extended info of the notification (ie. If you receive a new message, the last param will be an intent to start the ViewMessage activity).
I got this info from the link you gave...

Also check the samples directory, there are some good Notification examples in there.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Artur79 » Sun Nov 23, 2008 7:01 pm

thanks for an answer.
I suppose, for plusminus application example, it should be something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         nt.setLatestEventInfo(
  2.                         this.getBaseContext(),
  3.                         "anddev.org - Notification",
  4.                          getText(detailedTextID),
  5.                          appIntent
  6.         );
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but there's a problem with a type or last param, should be PendingIntent, how to get this ?
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Status bar notification : navigation problem

Postby Tinky » Mon Nov 24, 2008 3:07 pm

Hello,

I have created a sample example of statusbar notification, but now i am unable to start another intent when i click on statusbar notification icon,
my code is

Intent contentIntent = new Intent(this, HelloActivity.class);

contentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Notification notification = new Notification(imageID, msg, System.currentTimeMillis());

PendingIntent appIntent = PendingIntent.getActivity(this, 0,contentIntent, 0);

notification.vibrate = new long[]{100, 250, 100, 500};

notification.setLatestEventInfo(this, getText(R.string.app_name), getText(R.string.app_name), appIntent);

nm.notify(id, notification);

Can anyone tell me how to start new intent when we click on statusbar notification icon ??

Thanks in advance
Tinky
Tinky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 09, 2008 9:18 am
Location: India

Postby Tinky » Mon Nov 24, 2008 3:15 pm

I am able to start another intent on clicking the status bar notification icon as given below

==> click and hold on the status bar icon, then drag down to the bottom of the screen and let go

on doing this, a list of all notification get display on that dragged screen and we can click on to that to start the respective intent.

Regards
Tinky
Tinky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 09, 2008 9:18 am
Location: India

Notification Sample with the latest SDK 1.5

Postby saigeethamn » Wed Sep 02, 2009 11:24 am

Hi,

I see that the above example is written in an earlier SDK. I have written a notification example using the APIs of 1.5. Here is the code for the same:

Code: Select all
public class SimpleNotification extends Activity {
   
   private NotificationManager mNotificationManager;
   private int SIMPLE_NOTFICATION_ID;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
      

        Button start = (Button)findViewById(R.id.notifyButton);
        Button cancel = (Button)findViewById(R.id.cancelButton);
       
        start.setOnClickListener(new OnClickListener() {
           
           public void onClick(View v) {
              
              
              Context context = getApplicationContext();
              CharSequence contentTitle = "Notification Details...";
              CharSequence contentText = "Browse Android Official Site by clicking me";
              Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));
              PendingIntent intent =
                 PendingIntent.getActivity(SimpleNotification.this, 0,
                 notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
              
              notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
              mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
           }
        });
       
        cancel.setOnClickListener(new OnClickListener() {
           
           public void onClick(View v) {
              
              mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
           }
        });
    }
}


If you are interested in seeing more examples for other basic aspects of android development, visit Sai Geetha's Blog

Hope this helps people who are looking at the latest SDK.
A person who loves to share my ten cents of knowledge
http://saigeethamn.blogspot.com
saigeethamn
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Aug 26, 2009 10:51 am

Error in StatusbarNotificator application

Postby Pushpalata » Sat Oct 03, 2009 1:44 pm

Hello plusminus,
I followed ur code for StatusbarNotificator as it is by creating new android project .
I addes the code for StatusbarNotificator.java & main.xml . bt i got many errors, to remove some of them a added 'notif_short' and 'notif_detailed' strings in strings.xml file and also an image to drawable in " res " folder.
bt still then there r some errors and one of them which I couldn't remove is : android.content.Intent.VIEW_ACTION cannot be resolved

what should i do now plz suggest some solution to this.....is anything remained.....should i add anything to AndroidManifest.xml ?

reply soon........! :(
PUSHPALATA
Pushpalata
Freshman
Freshman
 
Posts: 6
Joined: Wed Sep 23, 2009 5:12 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests