├── README ├── LICENSE └── android-statusbar-notificaion ├── SystemNotification.js ├── README └── SystemNotification.java /README: -------------------------------------------------------------------------------- 1 | Note: 2 | 3 | This repo is no longer managed. You may find what you are looking for here: 4 | https://github.com/phonegap/phonegap-plugins/tree/master/Android/StatusBarNotification 5 | 6 | 7 | PhoneGap plugin for system notification 8 | 9 | This code is completely dependent on the PhoneGap project, also hosted on 10 | GitHub ( github.com/phonegap/phonegap ) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011 Sailesh Mittal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | 11 | -------------------------------------------------------------------------------- /android-statusbar-notificaion/SystemNotification.js: -------------------------------------------------------------------------------- 1 | function SystemNotification() { 2 | } 3 | 4 | SystemNotification.prototype.notificationEnabled = false; 5 | 6 | SystemNotification.prototype.newCount = 0; //to keep track of multiple notifications events 7 | 8 | SystemNotification.prototype.enableNotification = function () { 9 | this.notificationEnabled = true; 10 | }; 11 | 12 | SystemNotification.prototype.disableNotification = function () { 13 | this.notificationEnabled = false; 14 | }; 15 | 16 | SystemNotification.prototype.onBackground = function () { 17 | this.enableNotification(); 18 | }; 19 | 20 | SystemNotification.prototype.onForeground = function () { 21 | this.disableNotification(); 22 | }; 23 | 24 | SystemNotification.prototype.createStatusBarNotification = function (contentTitle, contentText, tickerText) { 25 | PhoneGap.exec(null, null, "systemNotification", "createStatusBarNotification", [contentTitle, contentText, tickerText]); 26 | }; 27 | 28 | SystemNotification.prototype.updateNotification = function (contentText, tickerText, number) { 29 | this.newCount++; 30 | var contentTitle = "my title"; 31 | if (this.newCount === 1) { 32 | this.createStatusBarNotification(contentTitle, contentText, tickerText); 33 | } else { 34 | PhoneGap.exec(null, null, "systemNotification", "updateNotification", [contentTitle, contentText, this.newCount]); 35 | this.showTickerText(tickerText); //optional 36 | } 37 | }; 38 | 39 | SystemNotification.prototype.cancelNotification = function (contentText) { 40 | this.newCount--; 41 | if (this.newCount === 0) { 42 | PhoneGap.exec(null, null, "systemNotification", "cancelNotification", []); 43 | } 44 | else { 45 | //updating the notification 46 | var contentTitle = "my title"; 47 | PhoneGap.exec(null, null, "systemNotification", "updateNotification", [contentTitle, contentText, this.newCount]); 48 | } 49 | }; 50 | 51 | SystemNotification.prototype.showTickerText = function (tickerText) { 52 | PhoneGap.exec(null, null, "systemNotification", "showTickerText", [tickerText]); 53 | }; 54 | 55 | SystemNotification.prototype.touch = function () { 56 | PhoneGap.exec(null, null, "systemNotification", "touch", []); 57 | }; 58 | 59 | PhoneGap.addConstructor(function () { 60 | if (typeof(navigator.systemNotification) == "undefined") { 61 | navigator.systemNotification = new SystemNotification(); 62 | navigator.systemNotification.touch(); //this ensures that the plugin is added when phonegap kicks off 63 | } 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /android-statusbar-notificaion/README: -------------------------------------------------------------------------------- 1 | This is a phonegap-android-plugin for displaying status bar notifications in an android device. 2 | The code may be a little specific according to my needs, but can still be used in most general cases. (And you can always modify it :) ) 3 | 4 | 5 | STEPS 6 | 7 | Add a flag to the activity in AndroidManifest.xml inside the activity tag. 8 | // 9 | 10 | Add the following method to your java file. This lets the javascript code to continue running when the app goes into the background. 11 | Note: The latest version of phonegap allows this by default, so skip this step if using the latest phonegap. 12 | //@override 13 | //public void onPause() 14 | //{ 15 | // super.onPause(); 16 | // super.appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};"); 17 | // super.appView.resumeTimers(); 18 | //} 19 | 20 | Make the following change to the SystemNotification.java: 21 | In the method "createStatusBarNotification", look for a line 22 | // Intent notificationIntent = new Intent(this.ctx, myActivityClass.class); 23 | Change the "myActivityClass.class" to .class 24 | 25 | Use either of the methods: 26 | Method 1 (no modifications to phonegap code) : 27 | 28 | Add the SystemNotification.java file to "src/your_package_name/" folder 29 | 30 | Add following to the java file that is being created for you, in the onCreate method. Make sure to call this AFTER the super.onCreate(...) call. 31 | // super.addService("systemNotification","com.your_package_name.SystemNotification"); 32 | 33 | Append the contents of the SystemNotification.js file to the phonegap.js OR Add this file in your index.html after adding the phonegap.js 34 | 35 | 36 | Method 2 (modify the phonegap libraries) : Files need to be added to the phonegap framework and libraries are re-built and then added to your project folders. 37 | 38 | Add SystemNotification.java to the directory where all other phonegap java files are located (framework/src/phonegap/) 39 | Add SystemNotification.js to the directory where all other js files are located (framework/assets/www/js/) 40 | 41 | Add the service in the DroidGap.java, where other services are being added 42 | // super.addService("systemNotification","com.phonegap.SystemNotification"); 43 | 44 | Build phonegap.jar and phonegap.js and replace the old files in your project with these new ones. 45 | 46 | 47 | 48 | USAGE 49 | All the methods of SystemNotification.js can be called from you javascript code as: 50 | navigator.systemNotification.(...); 51 | 52 | I used dojo.connect() to connect to the "onBackground" and "onForeground" events, to fire my required functions. 53 | -------------------------------------------------------------------------------- /android-statusbar-notificaion/SystemNotification.java: -------------------------------------------------------------------------------- 1 | package com.your_package_name; 2 | 3 | import org.json.JSONArray; 4 | import org.json.JSONException; 5 | import android.app.Notification; 6 | import android.app.NotificationManager; 7 | import android.app.PendingIntent; 8 | import android.content.Context; 9 | import android.content.Intent; 10 | import com.phonegap.api.Plugin; 11 | import com.phonegap.api.PluginResult; 12 | import org.json.JSONArray; 13 | 14 | public class SystemNotification extends Plugin { 15 | 16 | final int notif_ID = 1234; 17 | NotificationManager notificationManager; 18 | Notification note; 19 | PendingIntent contentIntent; 20 | 21 | @Override 22 | public PluginResult execute(String action, JSONArray args, String callbackId) 23 | { 24 | PluginResult.Status status = PluginResult.Status.OK; 25 | String result = ""; 26 | 27 | try { 28 | if (action.equals("createStatusBarNotification")) { 29 | this.createStatusBarNotification(args.getString(0), args.getString(1), args.getString(2)); 30 | } 31 | else if (action.equals("updateNotification")) { 32 | this.updateNotification(args.getString(0), args.getString(1), args.getInt(2)); 33 | } 34 | else if (action.equals("cancelNotification")) { 35 | this.cancelNotification(); 36 | } 37 | else if (action.equals("showTickerText")) { 38 | this.showTickerText(args.getString(0)); 39 | } 40 | return new PluginResult(status, result); 41 | } catch(JSONException e) { 42 | return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 43 | } 44 | } 45 | 46 | private void updateNotification(String contentTitle, String contentText, int number) 47 | { 48 | note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent); 49 | note.number = number; 50 | notificationManager.notify(notif_ID,note); 51 | } 52 | 53 | private void createStatusBarNotification(String contentTitle, String contentText, String tickerText) 54 | { 55 | notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE); 56 | note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis() ); 57 | //change the icon 58 | 59 | Intent notificationIntent = new Intent(this.ctx, myActivityClass.class); 60 | notificationIntent.setAction(Intent.ACTION_MAIN); 61 | notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 62 | contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0); 63 | 64 | note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent); 65 | 66 | note.number = 1; //Just created notification so number=1. Remove this line if you dont want numbers 67 | 68 | notificationManager.notify(notif_ID,note); 69 | } 70 | 71 | private void cancelNotification() 72 | { 73 | notificationManager.cancel(notif_ID); 74 | } 75 | 76 | private void showTickerText(String tickerText) 77 | { 78 | note.tickerText = tickerText; 79 | notificationManager.notify(notif_ID,note); 80 | } 81 | 82 | @Override 83 | public void onPause() 84 | { 85 | super.webView.loadUrl("javascript:navigator.systemNotification.onBackground();"); 86 | } 87 | 88 | @Override 89 | public void onResume() 90 | { 91 | super.webView.loadUrl("javascript:navigator.systemNotification.onForeground();"); 92 | } 93 | } 94 | 95 | 96 | --------------------------------------------------------------------------------