Skip to content

Commit e56a1a6

Browse files
Merge pull request #23 from JoaoCaixinha/master
added methods to set the notification display mode
2 parents f0ac86b + 136fc2a commit e56a1a6

File tree

8 files changed

+112
-62
lines changed

8 files changed

+112
-62
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ Push Notifications only work in real devices for the iOS platform (not on simula
128128
<color name="notification_color">#ff0000</color>
129129
</resources>
130130

131+
### Set notifications display mode
131132

133+
Only available for android. [Check android documentation](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up)
134+
135+
**enableHeadsUpNotifications** Use this method to set the notification display type to Heads-up. This method persists the set value, to disable the heads-up notifications you must call `disableHeadsUpNotifications`.
136+
137+
**disableHeadsUpNotifications** Use this method to set the default notifications display (only the small icon is shown in the notification bar) and disable Heads-up.
132138

133139
## Usage example
134140

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordovapush",
3-
"version": "0.1.25",
3+
"version": "0.1.26",
44
"description": "This Cordova plugin should be used with the iOS/Android platforms together with the Realtime Messaging library (ORTC) for Push Notifications support.",
55
"main": "index.js",
66
"scripts": {

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
id="co.realtime.plugins.CordovaPush"
4-
version="0.1.25">
4+
version="0.1.26">
55

66
<name>cordovapush</name>
77
<author>Reatime</author>

src/android/GcmReceiver.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import android.app.PendingIntent;
66
import android.content.Context;
77
import android.content.Intent;
8+
import android.content.SharedPreferences;
89
import android.graphics.Bitmap;
910
import android.graphics.BitmapFactory;
1011
import android.os.Bundle;
12+
import android.preference.PreferenceManager;
1113
import android.support.v4.app.NotificationCompat;
1214
import android.support.v4.content.ContextCompat;
1315
import android.util.Log;
@@ -74,6 +76,7 @@ public void createNotification(Context context, Bundle extras)
7476
NotificationCompat.Builder mBuilder =
7577
new NotificationCompat.Builder(context)
7678
.setDefaults(defaults)
79+
.setPriority(getAppPriority(context))
7780
.setLargeIcon(appIcon)
7881
.setSmallIcon(smallIcon)
7982
.setWhen(System.currentTimeMillis())
@@ -129,4 +132,18 @@ private static String getAppName(Context context)
129132

130133
return (String)appName;
131134
}
135+
136+
public static int getAppPriority(Context context){
137+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
138+
int val = preferences.getInt("APP_PRIORITY", 0);
139+
return val;
140+
}
141+
142+
public static void setAppPriority(Context context, int priority){
143+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
144+
SharedPreferences.Editor editor = preferences.edit();
145+
editor.putInt("APP_PRIORITY", priority);
146+
editor.apply();
147+
}
148+
132149
}

src/android/OrtcPushPlugin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package co.realtime.plugins.android.cordovapush;
22

33
import android.app.NotificationManager;
4+
import android.content.Context;
45
import android.os.Bundle;
56
import android.util.Log;
67

@@ -39,6 +40,8 @@ public class OrtcPushPlugin extends CordovaPlugin {
3940
public static final String ACTION_UNSUBSCRIBE = "unsubscribe";
4041
public static final String ACTION_CANCEL_NOTIFICATIONS = "cancelAllLocalNotifications";
4142
public static final String ACTION_SEND_MESSAGE = "send";
43+
private static final String ACTION_ENABLE_HEADS_UP_NOTIFICATIONS = "enableHeadsUpNotifications";
44+
private static final String ACTION_DISABLE_HEADS_UP_NOTIFICATIONS = "disableHeadsUpNotifications";
4245
private OrtcClient client;
4346
private static CordovaWebView gWebView;
4447
private static Bundle gCachedExtras = null;
@@ -256,6 +259,16 @@ else if(ACTION_SET_ICON.equals(action)){
256259
callbackContext.success();
257260
return true;
258261
}
262+
else if(ACTION_ENABLE_HEADS_UP_NOTIFICATIONS.equals(action)){
263+
Context context = cordova.getActivity().getApplicationContext();
264+
GcmReceiver.setAppPriority(context, 1);
265+
return true;
266+
}
267+
else if(ACTION_DISABLE_HEADS_UP_NOTIFICATIONS.equals(action)){
268+
Context context = cordova.getActivity().getApplicationContext();
269+
GcmReceiver.setAppPriority(context, 0);
270+
return true;
271+
}
259272
callbackContext.error("Invalid action");
260273
return false;
261274
} catch(Exception e) {

src/ios/OrtcPushPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;
2727
- (void)cancelAllLocalNotifications:(CDVInvokedUrlCommand*)command;
2828
- (void)log:(CDVInvokedUrlCommand*)command;
29+
- (void)enableHeadsUpNotifications:(CDVInvokedUrlCommand*)command;
30+
- (void)disableHeadsUpNotifications:(CDVInvokedUrlCommand*)command;
2931

3032
@end

src/ios/OrtcPushPlugin.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ - (void)log:(CDVInvokedUrlCommand*)command
169169
NSLog(@"OrtcPushPlugin: %@",[command.arguments objectAtIndex:0]);
170170
}
171171

172+
- (void)enableHeadsUpNotifications:(CDVInvokedUrlCommand*)command{
173+
174+
}
175+
176+
- (void)disableHeadsUpNotifications:(CDVInvokedUrlCommand*)command{
177+
178+
}
179+
172180
@end
173181

174182

www/OrtcPlugin.js

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,96 @@
11
(function(cordova) {
22

3-
function OrtcPushPlugin() {}
3+
function OrtcPushPlugin() {}
44

55

6-
OrtcPushPlugin.prototype.checkForNotifications = function(success) {
6+
OrtcPushPlugin.prototype.checkForNotifications = function(success) {
77
cordova.exec(success, success, "OrtcPushPlugin", "checkForNotifications", []);
8-
};
9-
10-
OrtcPushPlugin.prototype.removeNotifications = function(success) {
8+
};
9+
10+
OrtcPushPlugin.prototype.removeNotifications = function(success) {
1111
cordova.exec(success, success, "OrtcPushPlugin", "removeNotifications", []);
12-
};
13-
14-
OrtcPushPlugin.prototype.connect = function(config, success) {
12+
};
13+
14+
OrtcPushPlugin.prototype.connect = function(config, success) {
1515
cordova.exec(success, success, "OrtcPushPlugin", "connect", config ? [config] : []);
16-
};
17-
18-
OrtcPushPlugin.prototype.disconnect = function(success) {
16+
};
17+
18+
OrtcPushPlugin.prototype.enableHeadsUpNotifications = function(config, success) {
19+
cordova.exec(success, success, "OrtcPushPlugin", "enableHeadsUpNotifications", []);
20+
};
21+
22+
OrtcPushPlugin.prototype.disableHeadsUpNotifications = function(config, success) {
23+
cordova.exec(success, success, "OrtcPushPlugin", "disableHeadsUpNotifications", []);
24+
};
25+
26+
OrtcPushPlugin.prototype.disconnect = function(success) {
1927
cordova.exec(success, success, "OrtcPushPlugin", "disconnect", []);
20-
};
21-
22-
OrtcPushPlugin.prototype.subscribe = function(config, success) {
28+
};
29+
30+
OrtcPushPlugin.prototype.subscribe = function(config, success) {
2331
cordova.exec(success, success, "OrtcPushPlugin", "subscribe", config ? [config] : []);
24-
};
25-
26-
27-
OrtcPushPlugin.prototype.unsubscribe = function(config, success) {
32+
};
33+
34+
35+
OrtcPushPlugin.prototype.unsubscribe = function(config, success) {
2836
cordova.exec(success, success, "OrtcPushPlugin", "unsubscribe", config ? [config] : []);
29-
};
30-
31-
OrtcPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
37+
};
38+
39+
OrtcPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
3240
cordova.exec(callback, callback, "OrtcPushPlugin", "setApplicationIconBadgeNumber", [badge]);
33-
};
34-
35-
OrtcPushPlugin.prototype.send = function(config) {
41+
};
42+
43+
OrtcPushPlugin.prototype.send = function(config) {
3644
cordova.exec(null, null, "OrtcPushPlugin", "send", config ? [config] : []);
37-
};
38-
39-
// Call this to clear all notifications from the notification center
40-
OrtcPushPlugin.prototype.cancelAllLocalNotifications = function(callback) {
45+
};
46+
47+
// Call this to clear all notifications from the notification center
48+
OrtcPushPlugin.prototype.cancelAllLocalNotifications = function(callback) {
4149
cordova.exec(callback, callback, "OrtcPushPlugin", "cancelAllLocalNotifications", []);
42-
};
43-
44-
OrtcPushPlugin.prototype.log = function(log) {
50+
};
51+
52+
OrtcPushPlugin.prototype.log = function(log) {
4553
cordova.exec(null, null, "OrtcPushPlugin", "log", log ? [log] : []);
46-
};
47-
48-
OrtcPushPlugin.prototype.receiveRemoteNotification = function(channel, payload) {
54+
};
55+
56+
OrtcPushPlugin.prototype.receiveRemoteNotification = function(channel, payload) {
4957
var ev = document.createEvent('HTMLEvents');
5058
ev.channel = channel;
5159
ev.payload = payload;
5260
ev.initEvent('push-notification', true, true, arguments);
5361
document.dispatchEvent(ev);
54-
};
62+
};
5563

56-
OrtcPushPlugin.prototype.onException = function(error){
64+
OrtcPushPlugin.prototype.onException = function(error){
5765
var ev = document.createEvent('HTMLEvents');
5866
ev.description = error;
5967
ev.initEvent('onException', true, true, arguments);
6068
document.dispatchEvent(ev);
61-
};
62-
63-
cordova.addConstructor(function() {
69+
};
70+
71+
cordova.addConstructor(function() {
6472
if(!window.plugins) window.plugins = {};
6573
window.plugins.OrtcPushPlugin = new OrtcPushPlugin();
6674
});
67-
68-
})(window.cordova || window.Cordova || window.PhoneGap);
69-
70-
71-
// call when device is ready
72-
document.addEventListener("deviceready", function () {
73-
if(window.plugins && window.plugins.OrtcPushPlugin){
74-
var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
75-
OrtcPushPlugin.checkForNotifications();
76-
77-
}
78-
});
79-
80-
81-
// call when app resumes
82-
document.addEventListener("resume", function () {
83-
if(window.plugins && window.plugins.OrtcPushPlugin){
84-
var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
85-
OrtcPushPlugin.checkForNotifications();
86-
}
87-
});
8875

76+
})(window.cordova || window.Cordova || window.PhoneGap);
77+
78+
79+
// call when device is ready
80+
document.addEventListener("deviceready", function () {
81+
if(window.plugins && window.plugins.OrtcPushPlugin){
82+
var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
83+
OrtcPushPlugin.checkForNotifications();
8984

85+
}
86+
});
9087

9188

89+
// call when app resumes
90+
document.addEventListener("resume", function () {
91+
if(window.plugins && window.plugins.OrtcPushPlugin){
92+
var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
93+
OrtcPushPlugin.checkForNotifications();
94+
}
95+
});
9296

0 commit comments

Comments
 (0)