add notification permission status
This commit is contained in:
1 parent
1c3886e10c
commit
b52d3e4281
4 files changed
+158
No files matched your search
@@ -111,6 +111,7 @@ WiFi options:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
BluememeUtils.requestPushPermission(success, error);
|
BluememeUtils.requestPushPermission(success, error);
|
||||||
|
BluememeUtils.getPushPermissionStatus(success, error);
|
||||||
BluememeUtils.getFirebaseToken(function (result) {
|
BluememeUtils.getFirebaseToken(function (result) {
|
||||||
console.log(result.token);
|
console.log(result.token);
|
||||||
}, error);
|
}, error);
|
||||||
@@ -134,6 +135,31 @@ BluememeUtils.getInitialNotification(success, error);
|
|||||||
BluememeUtils.stopNotificationListener(success, error);
|
BluememeUtils.stopNotificationListener(success, error);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`getPushPermissionStatus` 只读取当前系统通知状态,不弹授权框。可用于填充后端 `NotificationsEnabled`。
|
||||||
|
|
||||||
|
返回示例:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "notification",
|
||||||
|
"platform": "Android",
|
||||||
|
"enabled": true,
|
||||||
|
"granted": true,
|
||||||
|
"runtimePermissionGranted": true,
|
||||||
|
"appNotificationsEnabled": true,
|
||||||
|
"authorizationStatus": "authorized",
|
||||||
|
"missing": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
推荐注册 token 前调用:
|
||||||
|
|
||||||
|
```js
|
||||||
|
BluememeUtils.getPushPermissionStatus(function (status) {
|
||||||
|
var notificationsEnabled = status.enabled === true;
|
||||||
|
}, error);
|
||||||
|
```
|
||||||
|
|
||||||
事件也会派发到 `window`:
|
事件也会派发到 `window`:
|
||||||
|
|
||||||
- `bluememeutils.token`
|
- `bluememeutils.token`
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.Manifest;
|
|||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.KeyguardManager;
|
import android.app.KeyguardManager;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
@@ -115,6 +116,10 @@ public class BluememeUtils extends CordovaPlugin {
|
|||||||
requestPushPermission(callbackContext);
|
requestPushPermission(callbackContext);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if ("getPushPermissionStatus".equals(action)) {
|
||||||
|
getPushPermissionStatus(callbackContext);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if ("getFirebaseToken".equals(action)) {
|
if ("getFirebaseToken".equals(action)) {
|
||||||
getFirebaseToken(callbackContext);
|
getFirebaseToken(callbackContext);
|
||||||
return true;
|
return true;
|
||||||
@@ -387,6 +392,14 @@ public class BluememeUtils extends CordovaPlugin {
|
|||||||
PermissionHelper.requestPermission(this, REQ_NOTIFICATION_PERMISSION, Manifest.permission.POST_NOTIFICATIONS);
|
PermissionHelper.requestPermission(this, REQ_NOTIFICATION_PERMISSION, Manifest.permission.POST_NOTIFICATIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getPushPermissionStatus(CallbackContext callbackContext) {
|
||||||
|
try {
|
||||||
|
callbackContext.success(pushPermissionStatus());
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
sendError(callbackContext, "JSON_ERROR", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void getFirebaseToken(final CallbackContext callbackContext) {
|
private void getFirebaseToken(final CallbackContext callbackContext) {
|
||||||
if (!ensureFirebaseInitialized(true)) {
|
if (!ensureFirebaseInitialized(true)) {
|
||||||
sendError(callbackContext, "FIREBASE_NOT_CONFIGURED", "Firebase is not configured. Add Google services files or pass Firebase plugin preferences.");
|
sendError(callbackContext, "FIREBASE_NOT_CONFIGURED", "Firebase is not configured. Add Google services files or pass Firebase plugin preferences.");
|
||||||
@@ -1109,6 +1122,41 @@ public class BluememeUtils extends CordovaPlugin {
|
|||||||
return (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
return (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JSONObject pushPermissionStatus() throws JSONException {
|
||||||
|
Context context = cordova.getActivity().getApplicationContext();
|
||||||
|
boolean runtimeGranted = Build.VERSION.SDK_INT < 33
|
||||||
|
|| PermissionHelper.hasPermission(this, Manifest.permission.POST_NOTIFICATIONS);
|
||||||
|
boolean appNotificationsEnabled = areAppNotificationsEnabled(context);
|
||||||
|
boolean enabled = runtimeGranted && appNotificationsEnabled;
|
||||||
|
|
||||||
|
JSONArray missing = new JSONArray();
|
||||||
|
if (!runtimeGranted && Build.VERSION.SDK_INT >= 33) {
|
||||||
|
missing.put(Manifest.permission.POST_NOTIFICATIONS);
|
||||||
|
}
|
||||||
|
if (!appNotificationsEnabled) {
|
||||||
|
missing.put("app_notifications_disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject result = new JSONObject();
|
||||||
|
result.put("type", "notification");
|
||||||
|
result.put("platform", "Android");
|
||||||
|
result.put("enabled", enabled);
|
||||||
|
result.put("granted", enabled);
|
||||||
|
result.put("runtimePermissionGranted", runtimeGranted);
|
||||||
|
result.put("appNotificationsEnabled", appNotificationsEnabled);
|
||||||
|
result.put("authorizationStatus", enabled ? "authorized" : "denied");
|
||||||
|
result.put("missing", missing);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean areAppNotificationsEnabled(Context context) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
return notificationManager == null || notificationManager.areNotificationsEnabled();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private static JSONObject permissionResult(String type, boolean granted, JSONArray missing) throws JSONException {
|
private static JSONObject permissionResult(String type, boolean granted, JSONArray missing) throws JSONException {
|
||||||
JSONObject result = new JSONObject();
|
JSONObject result = new JSONObject();
|
||||||
result.put("type", type);
|
result.put("type", type);
|
||||||
|
|||||||
@@ -167,6 +167,31 @@ static NSString * const BluememeDeviceKeyDefaultsKey = @"bluememe_utils_device_k
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)getPushPermissionStatus:(CDVInvokedUrlCommand *)command
|
||||||
|
{
|
||||||
|
if (@available(iOS 10.0, *)) {
|
||||||
|
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
[self sendSuccessForCommand:command dictionary:[self dictionaryFromNotificationSettings:settings]];
|
||||||
|
});
|
||||||
|
}];
|
||||||
|
} else {
|
||||||
|
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
|
||||||
|
BOOL enabled = settings.types != UIUserNotificationTypeNone;
|
||||||
|
[self sendSuccessForCommand:command dictionary:@{
|
||||||
|
@"type": @"notification",
|
||||||
|
@"platform": @"iOS",
|
||||||
|
@"enabled": @(enabled),
|
||||||
|
@"granted": @(enabled),
|
||||||
|
@"authorizationStatus": enabled ? @"authorized" : @"denied",
|
||||||
|
@"alertEnabled": @((settings.types & UIUserNotificationTypeAlert) != 0),
|
||||||
|
@"soundEnabled": @((settings.types & UIUserNotificationTypeSound) != 0),
|
||||||
|
@"badgeEnabled": @((settings.types & UIUserNotificationTypeBadge) != 0),
|
||||||
|
@"missing": enabled ? @[] : @[@"notification"]
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void)getFirebaseToken:(CDVInvokedUrlCommand *)command
|
- (void)getFirebaseToken:(CDVInvokedUrlCommand *)command
|
||||||
{
|
{
|
||||||
NSError *firebaseError = nil;
|
NSError *firebaseError = nil;
|
||||||
@@ -337,6 +362,61 @@ static NSString * const BluememeDeviceKeyDefaultsKey = @"bluememe_utils_device_k
|
|||||||
return @"touch";
|
return @"touch";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSDictionary *)dictionaryFromNotificationSettings:(UNNotificationSettings *)settings API_AVAILABLE(ios(10.0))
|
||||||
|
{
|
||||||
|
NSString *status = [self authorizationStatusName:settings.authorizationStatus];
|
||||||
|
BOOL enabled = settings.authorizationStatus == UNAuthorizationStatusAuthorized;
|
||||||
|
if (@available(iOS 12.0, *)) {
|
||||||
|
enabled = enabled || settings.authorizationStatus == UNAuthorizationStatusProvisional;
|
||||||
|
}
|
||||||
|
if (@available(iOS 14.0, *)) {
|
||||||
|
enabled = enabled || settings.authorizationStatus == UNAuthorizationStatusEphemeral;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL alertEnabled = settings.alertSetting == UNNotificationSettingEnabled;
|
||||||
|
BOOL soundEnabled = settings.soundSetting == UNNotificationSettingEnabled;
|
||||||
|
BOOL badgeEnabled = settings.badgeSetting == UNNotificationSettingEnabled;
|
||||||
|
NSArray *missing = enabled ? @[] : @[@"notification"];
|
||||||
|
|
||||||
|
return @{
|
||||||
|
@"type": @"notification",
|
||||||
|
@"platform": @"iOS",
|
||||||
|
@"enabled": @(enabled),
|
||||||
|
@"granted": @(enabled),
|
||||||
|
@"authorizationStatus": status,
|
||||||
|
@"alertEnabled": @(alertEnabled),
|
||||||
|
@"soundEnabled": @(soundEnabled),
|
||||||
|
@"badgeEnabled": @(badgeEnabled),
|
||||||
|
@"missing": missing
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)authorizationStatusName:(UNAuthorizationStatus)status API_AVAILABLE(ios(10.0))
|
||||||
|
{
|
||||||
|
switch (status) {
|
||||||
|
case UNAuthorizationStatusNotDetermined:
|
||||||
|
return @"notDetermined";
|
||||||
|
case UNAuthorizationStatusDenied:
|
||||||
|
return @"denied";
|
||||||
|
case UNAuthorizationStatusAuthorized:
|
||||||
|
return @"authorized";
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@available(iOS 12.0, *)) {
|
||||||
|
if (status == UNAuthorizationStatusProvisional) {
|
||||||
|
return @"provisional";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (@available(iOS 14.0, *)) {
|
||||||
|
if (status == UNAuthorizationStatusEphemeral) {
|
||||||
|
return @"ephemeral";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return @"unknown";
|
||||||
|
}
|
||||||
|
|
||||||
- (BOOL)ensureFirebaseConfigured:(NSError **)error
|
- (BOOL)ensureFirebaseConfigured:(NSError **)error
|
||||||
{
|
{
|
||||||
if ([FIRApp defaultApp] != nil) {
|
if ([FIRApp defaultApp] != nil) {
|
||||||
|
|||||||
@@ -81,6 +81,10 @@ var api = {
|
|||||||
call('requestPushPermission', [], success, error);
|
call('requestPushPermission', [], success, error);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getPushPermissionStatus: function (success, error) {
|
||||||
|
call('getPushPermissionStatus', [], success, error);
|
||||||
|
},
|
||||||
|
|
||||||
getFirebaseToken: function (success, error) {
|
getFirebaseToken: function (success, error) {
|
||||||
call('getFirebaseToken', [], success, error);
|
call('getFirebaseToken', [], success, error);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user