Skip to content

Commit 8df6baf

Browse files
fix/android-has-permission #175
1 parent 96d73ad commit 8df6baf

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

demo/app/main-page.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<ScrollView>
88
<GridLayout columns="*,*" rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto" horizontalAlignment="center" class="tab-content">
99

10+
<Label row="0" colSpan="2" text="You need permission before being able to schedule local notifications. Either defer to when you schedule it, or do it when the app first loads." class="hint" textWrap="true" />
11+
<Button row="1" col="0" text="has permission?" tap="{{ doCheckHasPermission }}" class="button button-neutral"/>
1012
<iOS>
11-
<Label row="0" colSpan="2" text="On iOS you need permission before being able to schedule local notifications. Either defer to when you schedule it, or do it when the app first loads." class="hint" textWrap="true" />
12-
<Button row="1" col="0" text="has permission?" tap="{{ doCheckHasPermission }}" class="button button-neutral"/>
1313
<Button row="1" col="1" text="request perm." tap="{{ doRequestPermission }}" class="button button-neutral"/>
1414
</iOS>
1515

src/local-notifications.android.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
7878
hasPermission(): Promise<boolean> {
7979
return new Promise((resolve, reject) => {
8080
try {
81-
const context = utils.ad.getApplicationContext();
82-
const hasPermission = NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
83-
resolve(hasPermission);
81+
resolve(LocalNotificationsImpl.hasPermission());
8482
} catch (ex) {
8583
console.log("Error in LocalNotifications.hasPermission: " + ex);
8684
reject(ex);
@@ -91,7 +89,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
9189
requestPermission(): Promise<boolean> {
9290
return new Promise((resolve, reject) => {
9391
try {
94-
// nothing to do on this platform
92+
// AFAIK can't do it on this platform
9593
resolve(true);
9694
} catch (ex) {
9795
console.log("Error in LocalNotifications.requestPermission: " + ex);
@@ -198,6 +196,11 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
198196
schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>> {
199197
return new Promise((resolve, reject) => {
200198
try {
199+
if (!LocalNotificationsImpl.hasPermission()) {
200+
reject("Permission not granted");
201+
return;
202+
}
203+
201204
const context = utils.ad.getApplicationContext();
202205
const resources = context.getResources();
203206
const scheduledIds: Array<number> = [];
@@ -243,6 +246,11 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
243246
}
244247
});
245248
}
249+
250+
private static hasPermission(): boolean {
251+
const context = utils.ad.getApplicationContext();
252+
return !context || NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
253+
}
246254
}
247255

248256
export const LocalNotifications = new LocalNotificationsImpl();

src/local-notifications.ios.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
439439
this.requestPermission().then(granted => {
440440
if (granted) {
441441
resolve(LocalNotificationsImpl.schedulePendingNotifications(options));
442+
} else {
443+
reject("Permission not granted");
442444
}
443445
});
444446
} else {

src/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-local-notifications",
3-
"version": "4.0.1",
3+
"version": "4.1.0",
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",
@@ -60,9 +60,9 @@
6060
},
6161
"homepage": "https://github.com/eddyverbruggen/nativescript-local-notifications",
6262
"devDependencies": {
63-
"nativescript-dev-typescript": "rc",
64-
"tns-core-modules": "rc",
65-
"tns-platform-declarations": "rc",
63+
"nativescript-dev-typescript": "~0.10.0",
64+
"tns-core-modules": "~6.1.2",
65+
"tns-platform-declarations": "~6.1.2",
6666
"tslint": "^5.10.0",
6767
"typescript": "~3.4.5"
6868
}

0 commit comments

Comments
 (0)