1+ var application = require ( "application" ) ;
2+ var utils = require ( "utils/utils" ) ;
3+ var BLE = require ( "./ble-common" ) ;
4+
5+ /*
6+ LocalNotifications.addOnMessageReceivedCallback = function (callback) {
7+ return new Promise(function (resolve, reject) {
8+ try {
9+ // note that this is ONLY triggered when the user clicked the notification in the statusbar
10+ com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageReceivedCallback(
11+ new com.telerik.localnotifications.LocalNotificationsPluginListener({
12+ success: function(notification) {
13+ callback(JSON.parse(notification))
14+ }
15+ })
16+ );
17+ resolve();
18+ } catch (ex) {
19+ console.log("Error in LocalNotifications.addOnMessageReceivedCallback: " + ex);
20+ reject(ex);
21+ }
22+ });
23+ };
24+
25+ LocalNotifications.schedule = function (arg) {
26+ return new Promise(function (resolve, reject) {
27+ try {
28+ var context = application.android.foregroundActivity;
29+
30+ for (var n in arg) {
31+ var options = LocalNotifications.merge(arg[n], LocalNotifications.defaults);
32+ options.icon = application.android.nativeApp.getApplicationInfo().icon;
33+ options.atTime = options.at ? options.at.getTime() : new Date().getTime();
34+
35+ // note that setting options.sound to explicitly to null will not add the default sound
36+ if (options.sound === undefined) {
37+ options.sound = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION).toString();
38+ }
39+
40+ // TODO best move this to native lib so we can reuse it in the restorereceiver (dupl for now)
41+ var builder = new android.support.v4.app.NotificationCompat.Builder(context)
42+ .setDefaults(0)
43+ .setContentTitle(options.title)
44+ .setContentText(options.body)
45+ .setSmallIcon(options.icon)
46+ .setAutoCancel(true) // removes the notification from the statusbar once tapped
47+ .setSound(options.sound == null ? null : android.net.Uri.parse(options.sound))
48+ .setNumber(options.badge)
49+ .setTicker(options.ticker || options.body);
50+
51+ // add the intent that handles the event when the notification is clicked (which should launch the app)
52+ var reqCode = new java.util.Random().nextInt();
53+ var clickIntent = new android.content.Intent(context, com.telerik.localnotifications.NotificationClickedActivity.class)
54+ .putExtra("pushBundle", JSON.stringify(options))
55+ .setFlags(android.content.Intent.FLAG_ACTIVITY_NO_HISTORY);
56+
57+ var pendingContentIntent = android.app.PendingIntent.getActivity(context, reqCode, clickIntent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
58+ builder.setContentIntent(pendingContentIntent);
59+
60+ var not = builder.build();
61+
62+ // add the intent which schedules the notification
63+ var notificationIntent = new android.content.Intent(context, com.telerik.localnotifications.NotificationPublisher.class)
64+ .setAction("" + options.id)
65+ .putExtra(com.telerik.localnotifications.NotificationPublisher.NOTIFICATION_ID, options.id)
66+ .putExtra(com.telerik.localnotifications.NotificationPublisher.NOTIFICATION, not);
67+
68+ var pendingIntent = android.app.PendingIntent.getBroadcast(context, 0, notificationIntent, android.app.PendingIntent.FLAG_CANCEL_CURRENT);
69+
70+ // configure when we'll show the event
71+ var alarmManager = utils.ad.getApplicationContext().getSystemService(android.content.Context.ALARM_SERVICE);
72+ alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, options.atTime, pendingIntent);
73+
74+ LocalNotifications._persist(options);
75+ }
76+
77+ resolve();
78+ } catch (ex) {
79+ console.log("Error in LocalNotifications.schedule: " + ex);
80+ reject(ex);
81+ }
82+ });
83+ };
84+
85+ LocalNotifications._persist = function (options) {
86+ var sharedPreferences = LocalNotifications._getSharedPreferences();
87+ var sharedPreferencesEditor = sharedPreferences.edit();
88+ sharedPreferencesEditor.putString("" + options.id, JSON.stringify(options));
89+ sharedPreferencesEditor.apply();
90+ };
91+
92+ LocalNotifications._unpersist = function (id) {
93+ var sharedPreferences = LocalNotifications._getSharedPreferences();
94+ var sharedPreferencesEditor = sharedPreferences.edit();
95+ sharedPreferencesEditor.remove("" + id);
96+ sharedPreferencesEditor.apply();
97+ };
98+
99+ LocalNotifications._cancelById = function (id) {
100+ var context = application.android.foregroundActivity;
101+
102+ var notificationIntent = new android.content.Intent(context, com.telerik.localnotifications.NotificationPublisher.class)
103+ .setAction("" + id);
104+
105+ var pendingIntent = android.app.PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
106+
107+ var alarmManager = utils.ad.getApplicationContext().getSystemService(android.content.Context.ALARM_SERVICE);
108+ alarmManager.cancel(pendingIntent);
109+
110+ var notificationManager = utils.ad.getApplicationContext().getSystemService(android.content.Context.NOTIFICATION_SERVICE);
111+ notificationManager.cancel(id);
112+
113+ LocalNotifications._unpersist(id);
114+ };
115+
116+ LocalNotifications.cancel = function (id) {
117+ return new Promise(function (resolve, reject) {
118+ try {
119+ LocalNotifications._cancelById(id);
120+ resolve(true);
121+ } catch (ex) {
122+ console.log("Error in LocalNotifications.cancel: " + ex);
123+ reject(ex);
124+ }
125+ });
126+ };
127+
128+ LocalNotifications.cancelAll = function () {
129+ return new Promise(function (resolve, reject) {
130+ try {
131+ var sharedPreferences = LocalNotifications._getSharedPreferences();
132+ var keys = sharedPreferences.getAll().keySet();
133+
134+ console.log("-----will cancel " + keys.size() + " notification(s): " + keys);
135+
136+ var iterator = keys.iterator();
137+ while (iterator.hasNext()) {
138+ LocalNotifications._cancelById(iterator.next());
139+ }
140+
141+ resolve();
142+ } catch (ex) {
143+ console.log("Error in LocalNotifications.cancelAll: " + ex);
144+ reject(ex);
145+ }
146+ });
147+ };
148+
149+ LocalNotifications.getScheduledIds = function () {
150+ return new Promise(function (resolve, reject) {
151+ try {
152+ var scheduledIds = [];
153+
154+ var sharedPreferences = LocalNotifications._getSharedPreferences();
155+ var keys = sharedPreferences.getAll().keySet();
156+
157+ var iterator = keys.iterator();
158+ while (iterator.hasNext()) {
159+ scheduledIds.push(iterator.next());
160+ }
161+
162+ resolve(scheduledIds);
163+ } catch (ex) {
164+ console.log("Error in LocalNotifications.getScheduledIds: " + ex);
165+ reject(ex);
166+ }
167+ });
168+ };
169+
170+ LocalNotifications.hasPermission = function (arg) {
171+ return new Promise(function (resolve, reject) {
172+ try {
173+ // nothing to do on this platform
174+ resolve(true);
175+ } catch (ex) {
176+ console.log("Error in LocalNotifications.hasPermission: " + ex);
177+ reject(ex);
178+ }
179+ });
180+ };
181+
182+ LocalNotifications.requestPermission = function (arg) {
183+ return new Promise(function (resolve, reject) {
184+ try {
185+ // nothing to do on this platform
186+ resolve(true);
187+ } catch (ex) {
188+ console.log("Error in LocalNotifications.requestPermission: " + ex);
189+ reject(ex);
190+ }
191+ });
192+ };
193+
194+ LocalNotifications._getSharedPreferences = function () {
195+ var context = application.android.foregroundActivity;
196+ var PREF_KEY = "LocalNotificationsPlugin"; // TODO get constant from native, as the restorereceiver needs it as well
197+ return context.getSharedPreferences(PREF_KEY, android.content.Context.MODE_PRIVATE);
198+ };
199+ */
200+
201+ module . exports = BLE ;
0 commit comments