Skip to content

Commit 8c4214f

Browse files
Problems with notification id #137
1 parent 8cf803d commit 8c4214f

File tree

6 files changed

+66
-53
lines changed

6 files changed

+66
-53
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ You can pass several options to this function, everything is optional:
8888

8989
|option|description|
9090
|------|-----------|
91-
|`id` |A number so you can easily distinguish your notifications. Default 0.|
91+
|`id` |A number so you can easily distinguish your notifications. Will be generated if not set.|
9292
|`title` |The title which is shown in the statusbar. Default not set.|
9393
|`subtitle` |Shown below the title on iOS, and next to the App name on Android. Default not set. All android and iOS >= 10 only.|
9494
|`body` |The text below the title. If not provided, the subtitle or title (in this order or priority) will be swapped for it on iOS, as iOS won't display notifications without a body. Default not set on Android, `' '` on iOS, as otherwise the notification won't show up at all.|
@@ -124,7 +124,7 @@ You can pass several options to this function, everything is optional:
124124

125125
```js
126126
LocalNotifications.schedule([{
127-
id: 1,
127+
id: 1, // generated id if not set
128128
title: 'The title',
129129
body: 'Recurs every minute until cancelled',
130130
ticker: 'The ticker',
@@ -141,8 +141,8 @@ You can pass several options to this function, everything is optional:
141141
sound: "customsound-ios.wav", // falls back to the default sound on Android
142142
at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
143143
}]).then(
144-
function() {
145-
console.log("Notification scheduled");
144+
function(scheduledIds) {
145+
console.log("Notification id(s) scheduled: " + JSON.stringify(scheduledIds));
146146
},
147147
function(error) {
148148
console.log("scheduling error: " + error);

demo/app/main-view-model.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Observable } from "tns-core-modules/data/observable";
22
import { alert } from "tns-core-modules/ui/dialogs";
33
import { LocalNotifications } from "nativescript-local-notifications";
44
import { Color } from "tns-core-modules/color";
5+
import { ScheduleOptions } from "../../src";
56

67
export class HelloWorldModel extends Observable {
78

@@ -38,39 +39,45 @@ export class HelloWorldModel extends Observable {
3839
}
3940

4041
public doScheduleWithButtons(): void {
41-
LocalNotifications.schedule(
42-
[{
43-
id: 1,
44-
title: 'THE TITLE',
45-
subtitle: 'The subtitle',
46-
body: 'The big body. The big body. The big body. The big body. The big body. The big body. The big body. The big body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body.',
47-
bigTextStyle: true, // Allow more than 1 row of the 'body' text
48-
sound: "customsound",
49-
color: new Color("green"),
50-
forceShowWhenInForeground: true,
51-
channel: "My Awesome Channel", // not that this is revealed in the notification tray when you longpress it on Android
52-
ticker: "Special ticker text (Android only)",
53-
at: new Date(new Date().getTime() + (10 * 1000)),
54-
notificationLed: true,
55-
actions: [
56-
{
57-
id: "yes",
58-
type: "button",
59-
title: "Yes (and launch app)",
60-
launch: true
61-
},
62-
{
63-
id: "no",
64-
type: "button",
65-
title: "No",
66-
launch: false
67-
}
68-
]
69-
}])
70-
.then(() => {
42+
const options: Array<ScheduleOptions> = [
43+
{
44+
id: 1,
45+
title: 'THE TITLE',
46+
subtitle: 'The subtitle',
47+
body: 'The big body. The big body. The big body. The big body. The big body. The big body. The big body. The big body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body. The big fat body.',
48+
bigTextStyle: true, // Allow more than 1 row of the 'body' text
49+
sound: "customsound",
50+
color: new Color("green"),
51+
forceShowWhenInForeground: true,
52+
channel: "My Awesome Channel", // not that this is revealed in the notification tray when you longpress it on Android
53+
ticker: "Special ticker text (Android only)",
54+
at: new Date(new Date().getTime() + (10 * 1000)),
55+
notificationLed: true,
56+
actions: [
57+
{
58+
id: "yes",
59+
type: "button",
60+
title: "Yes (and launch app)",
61+
launch: true
62+
},
63+
{
64+
id: "no",
65+
type: "button",
66+
title: "No",
67+
launch: false
68+
}
69+
]
70+
},
71+
{
72+
title: 'Generated ID',
73+
at: new Date(new Date().getTime() + (5 * 1000))
74+
}
75+
];
76+
LocalNotifications.schedule(options)
77+
.then((scheduledIds: Array<number>) => {
7178
alert({
7279
title: "Notification scheduled",
73-
message: "ID: 1",
80+
message: `ID: ${JSON.stringify(scheduledIds)}`,
7481
okButtonText: "OK, thanks"
7582
});
7683
})

src/local-notifications-common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface NotificationAction {
3232
export interface ScheduleOptions {
3333
/**
3434
* A number so you can easily distinguish your notifications.
35-
* Default 0.
35+
* Default <generated>.
3636
*/
3737
id?: number;
3838

@@ -200,7 +200,7 @@ export interface LocalNotificationsApi {
200200
* (the notification will be scheduled in case the user granted permission),
201201
* or you can manually invoke `requestPermission` if that's your thing.
202202
*/
203-
schedule(options: ScheduleOptions[]): Promise<void>;
203+
schedule(options: ScheduleOptions[]): Promise<Array<number>>;
204204

205205
/**
206206
* Tapping a notification in the notification center will launch your app.
@@ -290,7 +290,7 @@ export abstract class LocalNotificationsCommon {
290290
}
291291

292292
protected static generateNotificationID(): number {
293-
return Date.now() + (10e6 * Math.random() | 0);
293+
return Math.round((Date.now() + Math.round((100000 * Math.random()))) / 1000);
294294
}
295295

296296
protected static ensureID(opts: ScheduleOptions): number {

src/local-notifications.android.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
194194
});
195195
}
196196

197-
schedule(scheduleOptions: ScheduleOptions[]): Promise<void> {
197+
schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>> {
198198
return new Promise((resolve, reject) => {
199199
try {
200200
const context = utils.ad.getApplicationContext();
201201
const resources = context.getResources();
202+
const scheduledIds: Array<number> = [];
202203

203204
// TODO: All these changes in the options (other than setting the ID) should rather be done in Java so that
204205
// the persisted options are exactly like the original ones.
@@ -229,11 +230,12 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
229230

230231
com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(
231232
new org.json.JSONObject(JSON.stringify(options)),
232-
context,
233-
);
233+
context);
234+
235+
scheduledIds.push(options.id);
234236
}
235237

236-
resolve();
238+
resolve(scheduledIds);
237239
} catch (ex) {
238240
console.log("Error in LocalNotifications.schedule: " + ex);
239241
reject(ex);

src/local-notifications.ios.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,21 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
104104
}
105105
}
106106

107-
private static schedulePendingNotifications(pending: ScheduleOptions[]): void {
107+
private static schedulePendingNotifications(pending: ScheduleOptions[]): Array<number> {
108108
if (LocalNotificationsImpl.isUNUserNotificationCenterAvailable()) {
109-
LocalNotificationsImpl.schedulePendingNotificationsNew(pending);
109+
return LocalNotificationsImpl.schedulePendingNotificationsNew(pending);
110110
} else {
111-
LocalNotificationsImpl.schedulePendingNotificationsLegacy(pending);
111+
return LocalNotificationsImpl.schedulePendingNotificationsLegacy(pending);
112112
}
113113
}
114114

115-
private static schedulePendingNotificationsNew(pending: ScheduleOptions[]): void {
115+
private static schedulePendingNotificationsNew(pending: ScheduleOptions[]): Array<number> {
116+
const scheduledIds: Array<number> = [];
116117
for (const n in pending) {
117118
const options: ScheduleOptions = LocalNotificationsImpl.merge(pending[n], LocalNotificationsImpl.defaults);
118119

119120
LocalNotificationsImpl.ensureID(options);
121+
scheduledIds.push(options.id);
120122

121123
// Notification content
122124
const content = UNMutableNotificationContent.new();
@@ -230,6 +232,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
230232
});
231233
}
232234
}
235+
return scheduledIds;
233236
}
234237

235238
private static calendarWithMondayAsFirstDay(): NSCalendar {
@@ -239,11 +242,13 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
239242
return cal;
240243
}
241244

242-
private static schedulePendingNotificationsLegacy(pending: ScheduleOptions[]): void {
245+
private static schedulePendingNotificationsLegacy(pending: ScheduleOptions[]): Array<number> {
246+
const scheduledIds: Array<number> = [];
243247
for (const n in pending) {
244248
const options = LocalNotificationsImpl.merge(pending[n], LocalNotificationsImpl.defaults);
245249

246250
LocalNotificationsImpl.ensureID(options);
251+
scheduledIds.push(options.id);
247252

248253
const notification = UILocalNotification.new();
249254
notification.fireDate = options.at ? options.at : new Date();
@@ -282,6 +287,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
282287

283288
UIApplication.sharedApplication.scheduleLocalNotification(notification);
284289
}
290+
return scheduledIds;
285291
}
286292

287293
addOrProcessNotification(notificationDetails: ReceivedNotification): void {
@@ -426,19 +432,17 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
426432
});
427433
}
428434

429-
schedule(options: ScheduleOptions[]): Promise<void> {
435+
schedule(options: ScheduleOptions[]): Promise<Array<number>> {
430436
return new Promise((resolve, reject) => {
431437
try {
432438
if (!LocalNotificationsImpl.hasPermission()) {
433439
this.requestPermission().then(granted => {
434440
if (granted) {
435-
LocalNotificationsImpl.schedulePendingNotifications(options);
436-
resolve();
441+
resolve(LocalNotificationsImpl.schedulePendingNotifications(options));
437442
}
438443
});
439444
} else {
440-
LocalNotificationsImpl.schedulePendingNotifications(options);
441-
resolve();
445+
resolve(LocalNotificationsImpl.schedulePendingNotifications(options));
442446
}
443447
} catch (ex) {
444448
console.log("Error in LocalNotifications.schedule: " + ex);

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-local-notifications",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "The Local Notifications plugin allows your app to show notifications when the app is not running. Just like remote push notifications, but a few orders of magnitude easier to set up.",
55
"main": "local-notifications",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)