Skip to content

Commit 9e92cf9

Browse files
committed
hasPermission now supports Android
1 parent be75cce commit 9e92cf9

File tree

7 files changed

+28
-11
lines changed

7 files changed

+28
-11
lines changed

ionic/FCM.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export declare class FCMPluginOnIonic {
5656
*/
5757
getToken(): Promise<string>;
5858
/**
59-
* Checking for permissions on iOS. On android, it always returns `true`.
59+
* Checking for permissions.
6060
*
6161
* @returns {Promise<boolean | null>} Returns a Promise of:
6262
* - true: push was allowed (or platform is android)

ionic/ngx/FCM.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export declare class FCM {
6767
*/
6868
getToken(): Promise<string>;
6969
/**
70-
* Checking for permissions on iOS. On android, it always returns `true`.
70+
* Checking for permissions.
7171
*
7272
* @returns {Promise<boolean | null>} Returns a Promise of:
7373
* - true: push was allowed (or platform is android)

ionic/v4/FCM.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export declare class FCM {
5656
*/
5757
getToken(): Promise<string>;
5858
/**
59-
* Checking for permissions on iOS. On android, it always returns `true`.
59+
* Checking for permissions.
6060
*
6161
* @returns {Promise<boolean | null>} Returns a Promise of:
6262
* - true: push was allowed (or platform is android)

src/android/com/gae/scaffolder/plugin/FCMPlugin.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gae.scaffolder.plugin;
22

3+
import androidx.core.app.NotificationManagerCompat;
34
import android.app.NotificationManager;
45
import android.content.Context;
56
import android.util.Log;
@@ -137,6 +138,8 @@ public void run() {
137138
});
138139
} else if (action.equals("deleteInstanceId")) {
139140
this.deleteInstanceId(callbackContext);
141+
} else if (action.equals("hasPermission")) {
142+
this.hasPermission(callbackContext);
140143
} else {
141144
callbackContext.error("Method not found");
142145
return false;
@@ -230,6 +233,20 @@ public void run() {
230233
});
231234
}
232235

236+
private void hasPermission(final CallbackContext callbackContext) {
237+
cordova.getThreadPool().execute(new Runnable() {
238+
public void run() {
239+
try {
240+
NotificationManagerCompat notificationManagerCompat =
241+
NotificationManagerCompat.from(cordova.getActivity().getApplicationContext());
242+
callbackContext.success(notificationManagerCompat.areNotificationsEnabled() ? 1 : 0);
243+
} catch (Exception e) {
244+
callbackContext.error(e.getMessage());
245+
}
246+
}
247+
});
248+
}
249+
233250
private JSONObject exceptionToJson(final Exception exception) throws JSONException {
234251
return new JSONObject() {
235252
{

src/www/FCMPlugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ export class FCMPlugin {
113113
}
114114

115115
/**
116-
* Checking for permissions on iOS. On android, it always returns `true`.
116+
* Checking for permissions.
117117
*
118118
* @returns {Promise<boolean | null>} Returns a Promise of:
119119
* - true: push was allowed (or platform is android)
120120
* - false: push will not be available
121121
* - null: still not answered, recommended checking again later.
122122
*/
123123
public hasPermission(): Promise<boolean> {
124-
return window.cordova.platformId !== 'ios'
125-
? Promise.resolve(true)
126-
: execAsPromise('hasPermission')
124+
return window.cordova.platformId === 'ios'
125+
? execAsPromise('hasPermission')
126+
: execAsPromise('hasPermission').then((value) => !!value)
127127
}
128128

129129
/**

typings/FCMPlugin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export declare class FCMPlugin {
6868
*/
6969
getToken(): Promise<string>;
7070
/**
71-
* Checking for permissions on iOS. On android, it always returns `true`.
71+
* Checking for permissions.
7272
*
7373
* @returns {Promise<boolean | null>} Returns a Promise of:
7474
* - true: push was allowed (or platform is android)

www/FCMPlugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ var FCMPlugin = (function () {
6868
return execAsPromise('getToken');
6969
};
7070
FCMPlugin.prototype.hasPermission = function () {
71-
return window.cordova.platformId !== 'ios'
72-
? Promise.resolve(true)
73-
: execAsPromise('hasPermission');
71+
return window.cordova.platformId === 'ios'
72+
? execAsPromise('hasPermission')
73+
: execAsPromise('hasPermission').then(function (value) { return !!value; });
7474
};
7575
FCMPlugin.prototype.onNotification = function (callback, options) {
7676
return asDisposableListener(this.eventTarget, 'notification', callback, options);

0 commit comments

Comments
 (0)