@nativescript-community/perms
Advanced tools
Comparing version 2.1.8 to 2.2.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [2.2.0](https://github.com/nativescript-community/perms/compare/v2.1.8...v2.2.0) (2021-10-21) | ||
### Features | ||
* **android:** native-api-usage ([8ff8972](https://github.com/nativescript-community/perms/commit/8ff897237dc5715d34aefd66a38da895ddf77965)) | ||
## [2.1.8](https://github.com/nativescript-community/perms/compare/v2.1.7...v2.1.8) (2021-08-09) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "@nativescript-community/perms", | ||
"version": "2.1.8", | ||
"version": "2.2.0", | ||
"description": "An unified permissions API for NativeScript on iOS and Android.", | ||
@@ -35,3 +35,3 @@ "main": "./permissions", | ||
"readmeFilename": "README.md", | ||
"gitHead": "d5203f5d16bfc84d1d90933ecc89a4c78cb767c9" | ||
"gitHead": "e64e6576f8906bf15566ad91638b0b08789228cb" | ||
} |
@@ -431,5 +431,12 @@ import { Device, Trace } from '@nativescript/core'; | ||
const NSPDidAskForNotification = 'NSPDidAskForNotification'; | ||
function getStatus() { | ||
async function getStatus() { | ||
const didAskForPermission = NSUserDefaults.standardUserDefaults.boolForKey(NSPDidAskForNotification); | ||
const isEnabled = UIApplication.sharedApplication.currentUserNotificationSettings.types !== 0; | ||
let isEnabled = false; | ||
const osVersion = parseFloat(Device.osVersion); | ||
if (osVersion >= 10) { | ||
isEnabled = await (new Promise(resolve => UNUserNotificationCenter.currentNotificationCenter().getNotificationSettingsWithCompletionHandler(resolve))) !== UNAuthorizationOptionNone; | ||
} | ||
else { | ||
isEnabled = UIApplication.sharedApplication.currentUserNotificationSettings.types !== 0; | ||
} | ||
if (isEnabled) { | ||
@@ -447,3 +454,3 @@ status = Status.Authorized; | ||
if (status[0] === Status.Undetermined) { | ||
return new Promise(resolve => { | ||
return new Promise((resolve, reject) => { | ||
const observer = function () { | ||
@@ -454,7 +461,22 @@ resolve(getStatus()); | ||
NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(UIApplicationDidBecomeActiveNotification, null, null, observer); | ||
const settings = UIUserNotificationSettings.settingsForTypesCategories(types, null); | ||
UIApplication.sharedApplication.registerUserNotificationSettings(settings); | ||
UIApplication.sharedApplication.registerForRemoteNotifications(); | ||
NSUserDefaults.standardUserDefaults.setBoolForKey(true, NSPDidAskForNotification); | ||
NSUserDefaults.standardUserDefaults.synchronize(); | ||
const osVersion = parseFloat(Device.osVersion); | ||
if (osVersion >= 10) { | ||
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptionsCompletionHandler(types, (p1, error) => { | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
UIApplication.sharedApplication.registerForRemoteNotifications(); | ||
NSUserDefaults.standardUserDefaults.setBoolForKey(true, NSPDidAskForNotification); | ||
NSUserDefaults.standardUserDefaults.synchronize(); | ||
} | ||
}); | ||
} | ||
else { | ||
const settings = UIUserNotificationSettings.settingsForTypesCategories(types, null); | ||
UIApplication.sharedApplication.registerUserNotificationSettings(settings); | ||
UIApplication.sharedApplication.registerForRemoteNotifications(); | ||
NSUserDefaults.standardUserDefaults.setBoolForKey(true, NSPDidAskForNotification); | ||
NSUserDefaults.standardUserDefaults.synchronize(); | ||
} | ||
}); | ||
@@ -596,3 +618,3 @@ } | ||
PermissionsIOS.canOpenSettings = canOpenSettings; | ||
function getPermissionStatus(type, json) { | ||
async function getPermissionStatus(type, json) { | ||
let status; | ||
@@ -629,3 +651,3 @@ if (Trace.isEnabled()) { | ||
case NSType.Notification: | ||
status = NSPNotification.getStatus(); | ||
status = await NSPNotification.getStatus(); | ||
break; | ||
@@ -647,3 +669,3 @@ case NSType.BackgroundRefresh: | ||
} | ||
return Promise.resolve(status); | ||
return (status); | ||
} | ||
@@ -675,11 +697,28 @@ PermissionsIOS.getPermissionStatus = getPermissionStatus; | ||
const typeStrings = json; | ||
if (typeStrings.indexOf('alert') !== -1) { | ||
types = types | 4; | ||
const osVersion = parseFloat(Device.osVersion); | ||
if (osVersion >= 10) { | ||
if (typeStrings.indexOf('alert') !== -1) { | ||
types = types | 4; | ||
} | ||
if (typeStrings.indexOf('badge') !== -1) { | ||
types = types | 1; | ||
} | ||
if (typeStrings.indexOf('sound') !== -1) { | ||
types = types | 2; | ||
} | ||
if (typeStrings.indexOf('providesAppNotificationSettings') !== -1 && parseFloat(Device.osVersion) >= 12) { | ||
types = types | 32; | ||
} | ||
} | ||
if (typeStrings.indexOf('badge') !== -1) { | ||
types = types | 1; | ||
else { | ||
if (typeStrings.indexOf('alert') !== -1) { | ||
types = types | 4; | ||
} | ||
if (typeStrings.indexOf('badge') !== -1) { | ||
types = types | 1; | ||
} | ||
if (typeStrings.indexOf('sound') !== -1) { | ||
types = types | 2; | ||
} | ||
} | ||
if (typeStrings.indexOf('sound') !== -1) { | ||
types = types | 2; | ||
} | ||
return NSPNotification.request(types); | ||
@@ -686,0 +725,0 @@ case NSType.NSPTypeSpeechRecognition: |
@@ -77,2 +77,3 @@ [](https://www.npmjs.com/package/@nativescript-community/perms) | ||
`alert`, `badge` and `sound` (default requests all three). | ||
* iOS 12+: The second parameter also takes this type inside of the array `providesAppNotificationSettings`. | ||
* If you are not requesting mediaLibrary then you can remove MediaPlayer.framework from the xcode project | ||
@@ -79,0 +80,0 @@ |
Sorry, the diff of this file is not supported yet
109593
15
1202
202