Skip to content

Commit ac02e81

Browse files
committed
Bug fix on CLEAR, that resulted in NPE. Added feature to allow FLAG_NO_CLEAR with backwards compatiblity.
1 parent 02488bc commit ac02e81

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

Android/StatusBarNotification/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ The plugins.xml is no longer supported. The plugins are all located in the confi
2626
5. You will need to add an import line like this to the .java files (see commented out lines inside the files):
2727

2828
import com.my.app.R;
29+
6. If you need the notification to stick, you can pass a paramter to notify function.
30+
E.g.:
31+
window.plugins.statusBarNotification.notify("Put your title here", "Put your sticky message here", Flag.FLAG_NO_CLEAR);
32+
//you can then use clear function to remove it.
33+
window.plugins.statusBarNotification.clear();
2934

3035
## Using the plugin ##
3136

@@ -59,4 +64,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5964
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
6065
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
6166
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62-
OTHER DEALINGS IN THE SOFTWARE.
67+
OTHER DEALINGS IN THE SOFTWARE.

Android/StatusBarNotification/StatusBarNotification.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
import android.app.NotificationManager;
3838
import android.content.Context;
3939
import android.content.Intent;
40-
41-
import android.os.Handler;
4240
import android.util.Log;
4341

4442
public class StatusBarNotification extends Plugin {
@@ -63,8 +61,10 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
6361
String tag = data.getString(0);
6462
String title = data.getString(1);
6563
String body = data.getString(2);
64+
String flag = data.getString(3);
6665
Log.d("NotificationPlugin", "Notification: " + tag + ", " + title + ", " + body);
67-
showNotification(tag, title, body);
66+
int notificationFlag = getFlagValue(flag);
67+
showNotification(tag, title, body, notificationFlag);
6868
result = new PluginResult(Status.OK);
6969
} catch (JSONException jsonEx) {
7070
Log.d("NotificationPlugin", "Got JSON Exception "
@@ -76,6 +76,7 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
7676
String tag = data.getString(0);
7777
Log.d("NotificationPlugin", "Notification cancel: " + tag);
7878
clearNotification(tag);
79+
result = new PluginResult(Status.OK);
7980
} catch (JSONException jsonEx) {
8081
Log.d("NotificationPlugin", "Got JSON Exception " + jsonEx.getMessage());
8182
result = new PluginResult(Status.JSON_EXCEPTION);
@@ -88,18 +89,37 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
8889
}
8990

9091
/**
92+
* Helper method that returns a flag value to be used for notification
93+
* by default it will return 16 representing FLAG_NO_CLEAR
94+
*
95+
* @param flag
96+
* @return int value of the flag
97+
*/
98+
private int getFlagValue(String flag) {
99+
int flagVal = Notification.FLAG_AUTO_CANCEL;
100+
101+
// We trust the flag value as it comes from our JS constant.
102+
// This is also backwards compatible as it will be emtpy.
103+
if (!flag.isEmpty()){
104+
flagVal = Integer.parseInt(flag);
105+
}
106+
107+
return flagVal;
108+
}
109+
110+
/**
91111
* Displays status bar notification
92112
*
93113
* @param tag Notification tag.
94114
* @param contentTitle Notification title
95115
* @param contentText Notification text
96116
* */
97-
public void showNotification( CharSequence tag, CharSequence contentTitle, CharSequence contentText ) {
117+
public void showNotification( CharSequence tag, CharSequence contentTitle, CharSequence contentText, int flag) {
98118
String ns = Context.NOTIFICATION_SERVICE;
99119
context = cordova.getActivity().getApplicationContext();
100120
mNotificationManager = (NotificationManager) context.getSystemService(ns);
101121

102-
Notification noti = StatusNotificationIntent.buildNotification(context, tag, contentTitle, contentText);
122+
Notification noti = StatusNotificationIntent.buildNotification(context, tag, contentTitle, contentText, flag);
103123
mNotificationManager.notify(tag.hashCode(), noti);
104124
}
105125

Android/StatusBarNotification/StatusNotificationIntent.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// This class is used on all Androids below Honeycomb
22
package com.phonegap.plugins.statusBarNotification;
3-
// import com.yourapp.R;
3+
44

55
import android.app.Notification;
66
import android.app.PendingIntent;
77
import android.content.Context;
88
import android.content.Intent;
99
import android.content.pm.PackageManager;
1010

11+
//import com.my.app.R;
12+
1113
public class StatusNotificationIntent {
12-
public static Notification buildNotification( Context context, CharSequence tag, CharSequence contentTitle, CharSequence contentText ) {
14+
public static Notification buildNotification( Context context, CharSequence tag, CharSequence contentTitle, CharSequence contentText, int flag ) {
1315
int icon = R.drawable.notification;
1416
long when = System.currentTimeMillis();
1517
Notification noti = new Notification(icon, contentTitle, when);
16-
noti.flags |= Notification.FLAG_AUTO_CANCEL;
18+
noti.flags |= flag;
1719

1820
PackageManager pm = context.getPackageManager();
1921
Intent notificationIntent = pm.getLaunchIntentForPackage(context.getPackageName());

Android/StatusBarNotification/statusbarnotification.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727

2828
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
2929

30+
/**
31+
* Flags to denote the Android Notification constants
32+
* Values are representation from Android Notification Flag bit vals
33+
*/
34+
35+
function Flag() {}
36+
Flag.FLAG_AUTO_CANCEL="16";
37+
Flag.FLAG_NO_CLEAR="32";
38+
3039
/** @deprecated Use the W3C standard window.Notification API instead. */
3140
var NotificationMessenger = function() { }
3241

@@ -35,10 +44,11 @@ var NotificationMessenger = function() { }
3544
* @param body Body of the notification
3645
* @deprecated Use the W3C standard window.Notification API instead.
3746
*/
38-
NotificationMessenger.prototype.notify = function(title, body) {
47+
NotificationMessenger.prototype.notify = function(title, body, flag) {
3948
if (window.Notification) {
4049
this.activeNotification = new window.Notification(title, {
41-
body: body
50+
body: body,
51+
flag: flag
4252
});
4353
}
4454
}
@@ -83,6 +93,8 @@ if (typeof window.Notification == 'undefined') {
8393
this.onclose = options.onclose;
8494

8595
var content = options.body || '';
96+
97+
var flag = options.flag || '';
8698

8799
cordova.exec(function() {
88100
if (this.onshow) {
@@ -92,7 +104,7 @@ if (typeof window.Notification == 'undefined') {
92104
if (this.onerror) {
93105
this.onerror(error);
94106
}
95-
}, 'StatusBarNotification', 'notify', [this.tag, title, content]);
107+
}, 'StatusBarNotification', 'notify', [this.tag, title, content, flag]);
96108
};
97109

98110
// Permission is always granted on Android.

0 commit comments

Comments
 (0)