Skip to content

Commit

Permalink
Problems with notification id #137
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jul 9, 2019
1 parent 8cf803d commit 8c4214f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 53 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ You can pass several options to this function, everything is optional:

|option|description|
|------|-----------|
|`id` |A number so you can easily distinguish your notifications. Default 0.|
|`id` |A number so you can easily distinguish your notifications. Will be generated if not set.|
|`title` |The title which is shown in the statusbar. Default not set.|
|`subtitle` |Shown below the title on iOS, and next to the App name on Android. Default not set. All android and iOS >= 10 only.|
|`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.|
Expand Down Expand Up @@ -124,7 +124,7 @@ You can pass several options to this function, everything is optional:

```js
LocalNotifications.schedule([{
id: 1,
id: 1, // generated id if not set
title: 'The title',
body: 'Recurs every minute until cancelled',
ticker: 'The ticker',
Expand All @@ -141,8 +141,8 @@ You can pass several options to this function, everything is optional:
sound: "customsound-ios.wav", // falls back to the default sound on Android
at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
}]).then(
function() {
console.log("Notification scheduled");
function(scheduledIds) {
console.log("Notification id(s) scheduled: " + JSON.stringify(scheduledIds));
},
function(error) {
console.log("scheduling error: " + error);
Expand Down
69 changes: 38 additions & 31 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Observable } from "tns-core-modules/data/observable";
import { alert } from "tns-core-modules/ui/dialogs";
import { LocalNotifications } from "nativescript-local-notifications";
import { Color } from "tns-core-modules/color";
import { ScheduleOptions } from "../../src";

export class HelloWorldModel extends Observable {

Expand Down Expand Up @@ -38,39 +39,45 @@ export class HelloWorldModel extends Observable {
}

public doScheduleWithButtons(): void {
LocalNotifications.schedule(
[{
id: 1,
title: 'THE TITLE',
subtitle: 'The subtitle',
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.',
bigTextStyle: true, // Allow more than 1 row of the 'body' text
sound: "customsound",
color: new Color("green"),
forceShowWhenInForeground: true,
channel: "My Awesome Channel", // not that this is revealed in the notification tray when you longpress it on Android
ticker: "Special ticker text (Android only)",
at: new Date(new Date().getTime() + (10 * 1000)),
notificationLed: true,
actions: [
{
id: "yes",
type: "button",
title: "Yes (and launch app)",
launch: true
},
{
id: "no",
type: "button",
title: "No",
launch: false
}
]
}])
.then(() => {
const options: Array<ScheduleOptions> = [
{
id: 1,
title: 'THE TITLE',
subtitle: 'The subtitle',
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.',
bigTextStyle: true, // Allow more than 1 row of the 'body' text
sound: "customsound",
color: new Color("green"),
forceShowWhenInForeground: true,
channel: "My Awesome Channel", // not that this is revealed in the notification tray when you longpress it on Android
ticker: "Special ticker text (Android only)",
at: new Date(new Date().getTime() + (10 * 1000)),
notificationLed: true,
actions: [
{
id: "yes",
type: "button",
title: "Yes (and launch app)",
launch: true
},
{
id: "no",
type: "button",
title: "No",
launch: false
}
]
},
{
title: 'Generated ID',
at: new Date(new Date().getTime() + (5 * 1000))
}
];
LocalNotifications.schedule(options)
.then((scheduledIds: Array<number>) => {
alert({
title: "Notification scheduled",
message: "ID: 1",
message: `ID: ${JSON.stringify(scheduledIds)}`,
okButtonText: "OK, thanks"
});
})
Expand Down
6 changes: 3 additions & 3 deletions src/local-notifications-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface NotificationAction {
export interface ScheduleOptions {
/**
* A number so you can easily distinguish your notifications.
* Default 0.
* Default <generated>.
*/
id?: number;

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

/**
* Tapping a notification in the notification center will launch your app.
Expand Down Expand Up @@ -290,7 +290,7 @@ export abstract class LocalNotificationsCommon {
}

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

protected static ensureID(opts: ScheduleOptions): number {
Expand Down
10 changes: 6 additions & 4 deletions src/local-notifications.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
});
}

schedule(scheduleOptions: ScheduleOptions[]): Promise<void> {
schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>> {
return new Promise((resolve, reject) => {
try {
const context = utils.ad.getApplicationContext();
const resources = context.getResources();
const scheduledIds: Array<number> = [];

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

com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(
new org.json.JSONObject(JSON.stringify(options)),
context,
);
context);

scheduledIds.push(options.id);
}

resolve();
resolve(scheduledIds);
} catch (ex) {
console.log("Error in LocalNotifications.schedule: " + ex);
reject(ex);
Expand Down
24 changes: 14 additions & 10 deletions src/local-notifications.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
}
}

private static schedulePendingNotifications(pending: ScheduleOptions[]): void {
private static schedulePendingNotifications(pending: ScheduleOptions[]): Array<number> {
if (LocalNotificationsImpl.isUNUserNotificationCenterAvailable()) {
LocalNotificationsImpl.schedulePendingNotificationsNew(pending);
return LocalNotificationsImpl.schedulePendingNotificationsNew(pending);
} else {
LocalNotificationsImpl.schedulePendingNotificationsLegacy(pending);
return LocalNotificationsImpl.schedulePendingNotificationsLegacy(pending);
}
}

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

LocalNotificationsImpl.ensureID(options);
scheduledIds.push(options.id);

// Notification content
const content = UNMutableNotificationContent.new();
Expand Down Expand Up @@ -230,6 +232,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
});
}
}
return scheduledIds;
}

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

private static schedulePendingNotificationsLegacy(pending: ScheduleOptions[]): void {
private static schedulePendingNotificationsLegacy(pending: ScheduleOptions[]): Array<number> {
const scheduledIds: Array<number> = [];
for (const n in pending) {
const options = LocalNotificationsImpl.merge(pending[n], LocalNotificationsImpl.defaults);

LocalNotificationsImpl.ensureID(options);
scheduledIds.push(options.id);

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

UIApplication.sharedApplication.scheduleLocalNotification(notification);
}
return scheduledIds;
}

addOrProcessNotification(notificationDetails: ReceivedNotification): void {
Expand Down Expand Up @@ -426,19 +432,17 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
});
}

schedule(options: ScheduleOptions[]): Promise<void> {
schedule(options: ScheduleOptions[]): Promise<Array<number>> {
return new Promise((resolve, reject) => {
try {
if (!LocalNotificationsImpl.hasPermission()) {
this.requestPermission().then(granted => {
if (granted) {
LocalNotificationsImpl.schedulePendingNotifications(options);
resolve();
resolve(LocalNotificationsImpl.schedulePendingNotifications(options));
}
});
} else {
LocalNotificationsImpl.schedulePendingNotifications(options);
resolve();
resolve(LocalNotificationsImpl.schedulePendingNotifications(options));
}
} catch (ex) {
console.log("Error in LocalNotifications.schedule: " + ex);
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-local-notifications",
"version": "4.0.0",
"version": "4.0.1",
"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.",
"main": "local-notifications",
"typings": "index.d.ts",
Expand Down

0 comments on commit 8c4214f

Please sign in to comment.