forked from fechanique/cordova-plugin-fcm
-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed incompatibility with cordova-plugin-local-notification@0.9.0-be…
…ta.2
- Loading branch information
1 parent
e602729
commit 01f7c2c
Showing
4 changed files
with
147 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
@interface FCMNotificationCenterDelegate : NSObject {} | ||
|
||
- (void)configureForNotifications; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#import "AppDelegate+FCMPlugin.h" | ||
#import "FCMPlugin.h" | ||
#import "FCMNotificationCenterDelegate.h" | ||
#import <objc/runtime.h> | ||
#import <Foundation/Foundation.h> | ||
|
||
@import UserNotifications; | ||
|
||
// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices | ||
// running iOS 10 and above. | ||
@interface FCMNotificationCenterDelegate () <UNUserNotificationCenterDelegate> | ||
@end | ||
|
||
@implementation FCMNotificationCenterDelegate | ||
|
||
NSMutableArray<NSObject<UNUserNotificationCenterDelegate>*> *subNotificationCenterDelegates; | ||
|
||
- (void) forceNotificationCenterDelegate:(float)timeout { | ||
[self setNotificationCenterDelegate]; | ||
if(timeout < 0) { | ||
// The job should be done. | ||
return; | ||
} | ||
SEL thisMethodSelector = NSSelectorFromString(@"forceNotificationCenterDelegate:"); | ||
if([self respondsToSelector:thisMethodSelector]) { | ||
// NSLog(@"FCMNotificationCenterDelegate found: %@", [UNUserNotificationCenter currentNotificationCenter].delegate); | ||
float remainingTimeout = timeout - 0.1f; | ||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:thisMethodSelector]]; | ||
[invocation setSelector:thisMethodSelector]; | ||
[invocation setTarget:self]; | ||
[invocation setArgument:&(remainingTimeout) atIndex:2]; | ||
[NSTimer scheduledTimerWithTimeInterval:0.1f invocation:invocation repeats:NO]; | ||
return; | ||
} | ||
NSLog(@"forceNotificationCenterDelegate selector not found in FCMNotificationCenterDelegate"); | ||
} | ||
|
||
- (void)configureForNotifications { | ||
subNotificationCenterDelegates = [[NSMutableArray alloc]initWithCapacity:0]; | ||
[self setNotificationCenterDelegate]; | ||
[self forceNotificationCenterDelegate:10]; | ||
} | ||
|
||
- (void) setNotificationCenterDelegate { | ||
if([UNUserNotificationCenter currentNotificationCenter].delegate == self) { | ||
return; | ||
} | ||
if([UNUserNotificationCenter currentNotificationCenter].delegate != nil) { | ||
[subNotificationCenterDelegates addObject:[UNUserNotificationCenter currentNotificationCenter].delegate]; | ||
// NSLog(@"subNotificationCenterDelegates: %@", subNotificationCenterDelegates); | ||
} | ||
[UNUserNotificationCenter currentNotificationCenter].delegate = self; | ||
} | ||
|
||
|
||
// Handle incoming notification messages while app is in the foreground. | ||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center | ||
willPresentNotification:(UNNotification *)notification | ||
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { | ||
NSDictionary *userInfo = notification.request.content.userInfo; | ||
|
||
// Print full message. | ||
NSLog(@"willPresentNotification: %@", userInfo); | ||
|
||
NSError *error; | ||
NSDictionary *userInfoMutable = [userInfo mutableCopy]; | ||
[userInfoMutable setValue:@(NO) forKey:@"wasTapped"]; | ||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfoMutable | ||
options:0 | ||
error:&error]; | ||
[FCMPlugin.fcmPlugin notifyOfMessage:jsonData]; | ||
|
||
if(subNotificationCenterDelegates.count == 0){ | ||
// No subNotificationCenterDelegates to work with | ||
// Change this to your preferred presentation option | ||
completionHandler(UNNotificationPresentationOptionNone); | ||
} | ||
|
||
// Change this to your preferred presentation option | ||
__block UNNotificationPresentationOptions notificationPresentationOptions = UNNotificationPresentationOptionNone; | ||
void (^subDelegateCompletionHandler)(UNNotificationPresentationOptions) = ^(UNNotificationPresentationOptions possibleNotificationPresentationOptions) | ||
{ | ||
if(notificationPresentationOptions < possibleNotificationPresentationOptions) { | ||
notificationPresentationOptions = possibleNotificationPresentationOptions; | ||
} | ||
}; | ||
SEL thisMethodSelector = NSSelectorFromString(@"userNotificationCenter:willPresentNotification:withCompletionHandler:"); | ||
for (NSObject<UNUserNotificationCenterDelegate>* subNotificationCenterDelegate in subNotificationCenterDelegates) { | ||
if([subNotificationCenterDelegate respondsToSelector:thisMethodSelector]) { | ||
[subNotificationCenterDelegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:subDelegateCompletionHandler]; | ||
// } else { | ||
// NSLog(@"subNotificationCenterDelegates[i] not found willPresentNotification: %@", subNotificationCenterDelegate ); | ||
} | ||
} | ||
|
||
completionHandler(notificationPresentationOptions); | ||
} | ||
|
||
// Handle notification messages after display notification is tapped by the user. | ||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center | ||
didReceiveNotificationResponse:(UNNotificationResponse *)response | ||
withCompletionHandler:(void (^)(void))completionHandler { | ||
NSDictionary *userInfo = response.notification.request.content.userInfo; | ||
|
||
// Print full message. | ||
NSLog(@"didReceiveNotificationResponse: %@", userInfo); | ||
|
||
NSError *error; | ||
NSDictionary *userInfoMutable = [userInfo mutableCopy]; | ||
[userInfoMutable setValue:@(YES) forKey:@"wasTapped"]; | ||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfoMutable options:0 error:&error]; | ||
[AppDelegate setLastPush:jsonData]; | ||
|
||
if(subNotificationCenterDelegates.count == 0){ | ||
// No subNotificationCenterDelegates to work with | ||
completionHandler(); | ||
} | ||
|
||
void (^noopCompletionHandler)(void) = ^(){}; | ||
SEL thisMethodSelector = NSSelectorFromString(@"userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:"); | ||
for (NSObject<UNUserNotificationCenterDelegate>* subNotificationCenterDelegate in subNotificationCenterDelegates) { | ||
if([subNotificationCenterDelegate respondsToSelector:thisMethodSelector]) { | ||
[subNotificationCenterDelegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:noopCompletionHandler]; | ||
} | ||
} | ||
completionHandler(); | ||
} | ||
|
||
@end |