Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): add forceRegister option #337

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ All iOS boolean options can also be specified as `string`
| `ios.categories` | `Object` | `{}` | Optional. The data required in order to enable Action Buttons for iOS. See [Action Buttons on iOS](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#action-buttons-1) for more details. |
| `ios.critical` | `boolean` | `false` | Optional. If `true` the device can show up critical alerts. (Possible since iOS 12 with a special entitlement) **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings > Notifications > `App Name`. This is normal iOS behaviour. |
| `ios.forceShow` | `boolean` | `false` | Optional. Controls the behavior of the notification when app is in foreground. If `true` and app is in foreground, it will show a notification in the notification drawer, the same way as when the app is in background (and `on('notification')` callback will be called _only when the user clicks the notification_). When `false` and app is in foreground, the `on('notification')` callback will be called immediately. |
| `ios.forceRegister` | `boolean` | `false` | Optional. If `true` the app will register for remote notifications, even if the user has denied notification permissions. On iOS, notification permissions only control *user-facing* notifications – background pushes can still be received. |
JGreenlee marked this conversation as resolved.
Show resolved Hide resolved

#### iOS GCM support

Expand Down
6 changes: 4 additions & 2 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ @interface PushPlugin ()
@property (nonatomic, assign) BOOL isInline;
@property (nonatomic, assign) BOOL clearBadge;
@property (nonatomic, assign) BOOL forceShow;
@property (nonatomic, assign) BOOL forceRegister;
@property (nonatomic, assign) BOOL coldstart;

@property (nonatomic, copy) void (^backgroundTaskcompletionHandler)(UIBackgroundFetchResult);
Expand Down Expand Up @@ -164,6 +165,7 @@ - (void)init:(CDVInvokedUrlCommand *)command {
self.isInline = NO;
self.forceShow = [settings forceShowEnabled];
self.clearBadge = [settings clearBadgeEnabled];
self.forceRegister = [settings forceRegisterEnabled];
if (self.clearBadge) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Expand Down Expand Up @@ -749,7 +751,7 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza
NSLog(@"[PushPlugin] Error during authorization request: %@", error.localizedDescription);
}

if (granted) {
if (granted || self.forceRegister) {
NSLog(@"[PushPlugin] Notification permissions granted.");
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
Expand Down Expand Up @@ -787,7 +789,7 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza
NSLog(@"[PushPlugin] Error during authorization request: %@", error.localizedDescription);
}

if (granted) {
if (granted || self.forceRegister) {
NSLog(@"[PushPlugin] New notification permissions granted.");
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
Expand Down
1 change: 1 addition & 0 deletions src/ios/PushPluginSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@property (nonatomic, readonly) BOOL criticalEnabled;
@property (nonatomic, readonly) BOOL clearBadgeEnabled;
@property (nonatomic, readonly) BOOL forceShowEnabled;
@property (nonatomic, readonly) BOOL forceRegisterEnabled;
@property (nonatomic, readonly) BOOL voipEnabled;

@property (nonatomic, readonly, strong) NSArray *fcmTopics;
Expand Down
5 changes: 5 additions & 0 deletions src/ios/PushPluginSettings.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (instancetype)initWithDefaults {
@"critical" : @(NO),
@"clearBadge" : @(NO),
@"forceShow" : @(NO),
@"forceRegister" : @(NO),
@"voip" : @(NO),
@"fcmTopics" : @[],
@"categories" : [NSSet set]
Expand Down Expand Up @@ -164,6 +165,10 @@ - (BOOL)forceShowEnabled {
return [self.settingsDictionary[@"forceShow"] boolValue];
}

- (BOOL)forceRegisterEnabled {
return [self.settingsDictionary[@"forceRegister"] boolValue];
}

- (BOOL)voipEnabled {
return [self.settingsDictionary[@"voip"] boolValue];
}
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ declare namespace PhonegapPluginPush {
* If true will always show a notification, even when the app is on the foreground. Default is false.
*/
forceShow?: boolean
/**
* If true the app will register for remote notifications, even if the user has denied notification permissions.
* On iOS, notification permissions only control user-facing notifications – background pushes can still be received.
* Default is false.
*/
forceRegister?: boolean
}
}

Expand Down
Loading