Skip to content

Commit 4ebe97c

Browse files
Added TypeScript definitions
1 parent af04a0b commit 4ebe97c

File tree

5 files changed

+122
-10
lines changed

5 files changed

+122
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You can pass several options to this function, everything is optional:
2222

2323
|option|description|
2424
|------|-----------|
25-
|`id` |A number so you can easilty distinguish your notifications. Default 0.|
25+
|`id` |A number so you can easily distinguish your notifications. Default 0.|
2626
|`title` |The title which is shown in the statusbar. Default empty.|
2727
|`body` |The text below the title. Default empty.|
2828
|`ticker` |On Android you can show a different text in the statusbar, instead of the `body`. Default not set, so `body` is used.|

local-notifications.android.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LocalNotifications.addOnMessageReceivedCallback = function (callback) {
99
com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageReceivedCallback(
1010
new com.telerik.localnotifications.LocalNotificationsPluginListener({
1111
success: function(notification) {
12-
callback(JSON.parse(notification))
12+
callback(JSON.parse(notification));
1313
}
1414
})
1515
);
@@ -43,7 +43,7 @@ LocalNotifications.schedule = function (arg) {
4343
.setContentText(options.body)
4444
.setSmallIcon(options.icon)
4545
.setAutoCancel(true) // removes the notification from the statusbar once tapped
46-
.setSound(options.sound == null ? null : android.net.Uri.parse(options.sound))
46+
.setSound(options.sound === null ? null : android.net.Uri.parse(options.sound))
4747
.setNumber(options.badge)
4848
.setTicker(options.ticker || options.body);
4949

@@ -133,7 +133,7 @@ LocalNotifications.cancel = function (id) {
133133
return new Promise(function (resolve, reject) {
134134
try {
135135
LocalNotifications._cancelById(id);
136-
resolve(true);
136+
resolve(true); // TODO can we do this like iOS?
137137
} catch (ex) {
138138
console.log("Error in LocalNotifications.cancel: " + ex);
139139
reject(ex);

local-notifications.d.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
declare module "nativescript-local-notifications" {
2+
3+
/**
4+
* The options object passed into the schedule function.
5+
*/
6+
export interface ScheduleOptions {
7+
/**
8+
* A number so you can easily distinguish your notifications.
9+
* Default 0.
10+
*/
11+
id?: number;
12+
13+
/**
14+
* The title which is shown in the statusbar.
15+
* Default empty.
16+
*/
17+
title?: string;
18+
19+
/**
20+
* The text below the title.
21+
* Default empty.
22+
*/
23+
body?: string;
24+
25+
/**
26+
* On Android you can show a different text in the statusbar, instead of the 'body'.
27+
* Default not set, so `body` is used.
28+
*/
29+
ticker?: string;
30+
31+
/**
32+
* A JavaScript Date object indicating when the notification should be shown.
33+
* Default 'now'.
34+
*/
35+
at?: Date;
36+
37+
/**
38+
* On iOS (and some Android devices) you see a number on top of the app icon. On most Android devices you'll see this number in the notification center.
39+
* Default not set (0).
40+
*/
41+
badge?: number;
42+
43+
/**
44+
* Currently this is only used on Android where you can set this to 'null' to suppress the sound.
45+
* Default 'the default notification sound'.
46+
*/
47+
sound?: string;
48+
}
49+
50+
export interface ReceivedNotification {
51+
id: number;
52+
title?: string;
53+
body?: string;
54+
}
55+
56+
/**
57+
* On iOS you need to ask permission to schedule a notification.
58+
* You can have the `schedule` funtion do that for you automatically
59+
* (the notification will be scheduled in case the user granted permission),
60+
* or you can manually invoke `requestPermission` if that's your thing.
61+
*/
62+
export function schedule(options: ScheduleOptions[]): Promise<any>;
63+
64+
/**
65+
* Tapping a notification in the notification center will launch your app.
66+
* But what if you scheduled two notifications and you want to know which one the user tapped?
67+
*
68+
* Use this function to have a callback invoked when a notification was used to launch your app.
69+
* Note that on iOS it will even be triggered when your app is in the foreground and a notification is received.
70+
*/
71+
export function addOnMessageReceivedCallback(onReceived: (data: ReceivedNotification) => void): Promise<any>;
72+
73+
/**
74+
* Use when you want to know the id's of all notifications which have been scheduled.
75+
*/
76+
export function getScheduledIds(): Promise<any>;
77+
78+
/**
79+
* Cancels the 'id' passed in.
80+
* On iOS returns whether or not it was found (and cancelled).
81+
* On Android we always return true currently.
82+
*/
83+
export function cancel(id: number): Promise<boolean>;
84+
85+
/**
86+
* Use when you just want to cancel all previously scheduled notifications.
87+
*/
88+
export function cancelAll(): Promise<any>;
89+
90+
/**
91+
* On Android you don't need permission, but on iOS you do.
92+
* Android will simply return true.
93+
*
94+
* If the 'requestPermission' or 'schedule' functions previously ran
95+
* you may want to check whether or not the user granted permission.
96+
*/
97+
export function hasPermission(): Promise<boolean>;
98+
99+
/**
100+
* On Android you don't need permission, but on iOS you do.
101+
* Android will simply return true.
102+
*
103+
* If the 'requestPermission' or 'schedule' function previously ran
104+
* the user has already been prompted to grant permission.
105+
*
106+
* If the user granted permission this function returns true,
107+
* but if he denied permission this function will return false
108+
* since an iOS can only request permission once. In which case the user needs
109+
* to go to the iOS settings app and manually enable permissions for your app.
110+
*/
111+
export function requestPermission(): Promise<boolean>;
112+
}

local-notifications.ios.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var pendingReceivedNotifications = [],
1414
LocalNotifications.notificationReceivedObserver = LocalNotifications._addObserver("notificationReceived", function (result) {
1515
var notificationDetails = JSON.parse(result.userInfo.objectForKey('message'));
1616
console.log("------- notificationReceivedObserver: " + notificationDetails);
17-
if (receivedNotificationCallback != null) {
17+
if (receivedNotificationCallback !== null) {
1818
receivedNotificationCallback(notificationDetails);
1919
} else {
2020
pendingReceivedNotifications.push(notificationDetails);
@@ -64,7 +64,7 @@ LocalNotifications.requestPermission = function (arg) {
6464
try {
6565
LocalNotifications._requestPermission(function(granted) {
6666
resolve(granted);
67-
})
67+
});
6868
} catch (ex) {
6969
console.log("Error in LocalNotifications.requestPermission: " + ex);
7070
reject(ex);
@@ -142,7 +142,7 @@ LocalNotifications.cancelAll = function () {
142142
try {
143143
UIApplication.sharedApplication().cancelAllLocalNotifications();
144144
UIApplication.sharedApplication().applicationIconBadgeNumber = 0;
145-
resolve(true);
145+
resolve();
146146
} catch (ex) {
147147
console.log("Error in LocalNotifications.cancelAll: " + ex);
148148
reject(ex);
@@ -176,7 +176,7 @@ LocalNotifications.schedule = function (arg) {
176176
if (!LocalNotifications._hasPermission()) {
177177
LocalNotifications._requestPermission(function() {
178178
LocalNotifications._schedulePendingNotifications();
179-
})
179+
});
180180
} else {
181181
LocalNotifications._schedulePendingNotifications();
182182
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nativescript-local-notifications",
3-
"version": "1.0.0",
4-
"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.",
3+
"version": "1.0.1",
4+
"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.js",
66
"nativescript": {
77
"platforms": {

0 commit comments

Comments
 (0)