Skip to content

Commit

Permalink
refactor(ios)!: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Oct 17, 2024
1 parent 89d7f3c commit f251f49
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 84 deletions.
7 changes: 0 additions & 7 deletions src/ios/CDVAppDelegate+notification.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//
// CDVAppDelegate+notification.h
// pushtest
//
// Created by Robert Easterday on 10/26/12.
//
//

#import <Cordova/CDVAppDelegate.h>

Expand All @@ -16,9 +14,4 @@
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler;

- (id) getCommandInstance:(NSString*)className;

@property (nonatomic, retain) NSDictionary *launchNotification;
@property (nonatomic, retain) NSNumber *coldstart;

@end
38 changes: 0 additions & 38 deletions src/ios/CDVAppDelegate+notification.m
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
//
// CDVAppDelegate+notification.m
// pushtest
//
// Created by Robert Easterday on 10/26/12.
//
//

#import "CDVAppDelegate+notification.h"
#import "PushPlugin.h"
#import <objc/runtime.h>

static char launchNotificationKey;
static char coldstartKey;

@implementation CDVAppDelegate (notification)

- (id) getCommandInstance:(NSString*)className
{
return [self.viewController getCommandInstance:className];
}

// its dangerous to override a method from within a category.
// Instead we will use method swizzling. we set this up in the load call.
+ (void)load
Expand Down Expand Up @@ -103,32 +93,4 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
[NSNotificationCenter.defaultCenter postNotificationName:@"CordovaPluginPushDidReceiveNotificationResponse" object:nil userInfo:notificationInfo];
}

// The accessors use an Associative Reference since you can't define a iVar in a category
// http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocAssociativeReferences.html
- (NSMutableArray *)launchNotification
{
return objc_getAssociatedObject(self, &launchNotificationKey);
}

- (void)setLaunchNotification:(NSDictionary *)aDictionary
{
objc_setAssociatedObject(self, &launchNotificationKey, aDictionary, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)coldstart
{
return objc_getAssociatedObject(self, &coldstartKey);
}

- (void)setColdstart:(NSNumber *)aNumber
{
objc_setAssociatedObject(self, &coldstartKey, aNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dealloc
{
self.launchNotification = nil; // clear the association and release the object
self.coldstart = nil;
}

@end
19 changes: 0 additions & 19 deletions src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,15 @@
#import <PushKit/PushKit.h>

@interface PushPlugin : CDVPlugin
{
NSDictionary *notificationMessage;
BOOL isInline;
BOOL clearBadge;
BOOL forceShow;

NSMutableDictionary *handlerObj;
void (^completionHandler)(UIBackgroundFetchResult);
}

@property (nonatomic, copy) NSString *callbackId;

@property (nonatomic, strong) NSDictionary *notificationMessage;
@property BOOL isInline;
@property BOOL coldstart;
@property BOOL clearBadge;
@property BOOL forceShow;
@property (nonatomic, strong) NSMutableDictionary *handlerObj;

- (void)init:(CDVInvokedUrlCommand*)command;
- (void)unregister:(CDVInvokedUrlCommand*)command;
- (void)subscribe:(CDVInvokedUrlCommand*)command;
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
- (void)clearNotification:(CDVInvokedUrlCommand*)command;

- (void)setNotificationMessage:(NSDictionary *)notification;
- (void)notificationReceived;

// VoIP Features
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type;
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type;
Expand Down
51 changes: 31 additions & 20 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@
@interface PushPlugin ()

@property (nonatomic, strong) PushPluginFCM *pushPluginFCM;

@property (nonatomic, strong) NSDictionary *launchNotification;
@property (nonatomic, strong) NSDictionary *notificationMessage;
@property (nonatomic, strong) NSMutableDictionary *handlerObj;

@property (nonatomic, assign) BOOL isInline;
@property (nonatomic, assign) BOOL clearBadge;
@property (nonatomic, assign) BOOL forceShow;
@property (nonatomic, assign) BOOL coldstart;

@property (nonatomic, copy) void (^backgroundTaskcompletionHandler)(UIBackgroundFetchResult);

@end

@implementation PushPlugin

@synthesize notificationMessage;
@synthesize isInline;
@synthesize coldstart;
@synthesize callbackId;
@synthesize clearBadge;
@synthesize forceShow;
@synthesize handlerObj;

- (void)pluginInitialize {
self.pushPluginFCM = [[PushPluginFCM alloc] initWithGoogleServicePlist];
Expand Down Expand Up @@ -182,7 +186,7 @@ - (void)init:(CDVInvokedUrlCommand *)command {
[center setNotificationCategories:[settings categories]];

// If there is a pending startup notification, we will delay to allow JS event handlers to setup
if (notificationMessage) {
if (self.notificationMessage) {
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 0.5];
});
Expand Down Expand Up @@ -406,14 +410,14 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
- (void)notificationReceived {
NSLog(@"[PushPlugin] Notification received");

if (notificationMessage && self.callbackId != nil)
if (self.notificationMessage && self.callbackId != nil)
{
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:4];
NSMutableDictionary* additionalData = [NSMutableDictionary dictionaryWithCapacity:4];

for (id key in notificationMessage) {
for (id key in self.notificationMessage) {
if ([key isEqualToString:@"aps"]) {
id aps = [notificationMessage objectForKey:@"aps"];
id aps = [self.notificationMessage objectForKey:@"aps"];

for(id key in aps) {
NSLog(@"[PushPlugin] key: %@", key);
Expand Down Expand Up @@ -448,17 +452,17 @@ - (void)notificationReceived {
}
}
} else {
[additionalData setObject:[notificationMessage objectForKey:key] forKey:key];
[additionalData setObject:[self.notificationMessage objectForKey:key] forKey:key];
}
}

if (isInline) {
if (self.isInline) {
[additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"foreground"];
} else {
[additionalData setObject:[NSNumber numberWithBool:NO] forKey:@"foreground"];
}

if (coldstart) {
if (self.coldstart) {
[additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"coldstart"];
} else {
[additionalData setObject:[NSNumber numberWithBool:NO] forKey:@"coldstart"];
Expand Down Expand Up @@ -584,13 +588,13 @@ - (void)stopBackgroundTask:(NSTimer *)timer {

NSLog(@"[PushPlugin] stopBackgroundTask called");

if (handlerObj) {
if (self.handlerObj) {
NSLog(@"[PushPlugin] handlerObj");
completionHandler = [handlerObj[[timer userInfo]] copy];
if (completionHandler) {
self.backgroundTaskcompletionHandler = [self.handlerObj[[timer userInfo]] copy];
if (self.backgroundTaskcompletionHandler) {
NSLog(@"[PushPlugin] stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
completionHandler(UIBackgroundFetchResultNewData);
completionHandler = nil;
self.backgroundTaskcompletionHandler(UIBackgroundFetchResultNewData);
self.backgroundTaskcompletionHandler = nil;
}
}
}
Expand Down Expand Up @@ -661,8 +665,7 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza
}];
}

- (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler
{
- (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

switch (settings.authorizationStatus)
Expand All @@ -671,11 +674,19 @@ - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull voi
case UNAuthorizationStatusNotDetermined:
completionHandler(NO);
break;

case UNAuthorizationStatusAuthorized:
case UNAuthorizationStatusEphemeral:
case UNAuthorizationStatusProvisional:
completionHandler(YES);
break;
}
}];
}

- (void)dealloc {
self.launchNotification = nil;
self.coldstart = nil;
}

@end

0 comments on commit f251f49

Please sign in to comment.