Skip to content

Commit

Permalink
Push notification handle push delegate explicitly (#113)
Browse files Browse the repository at this point in the history
* Push notification handle push delegate explicitly

-- Exclude push notification macro added for RN code.

* SDK version updated to 22.06.5

* Forwarding push callbacks to appdelegate

Removed "COUNTLY_NOT_SET_PUSH_DELEGATE" flag

* Update CountlyPushNotifications.m

* Changelog updated

* Update CHANGELOG.md

Co-authored-by: ArtursKadikis <kadikis.arturs@gmail.com>
  • Loading branch information
ijunaid and ArtursKadikis authored Dec 27, 2022
1 parent 25e7833 commit 85318ed
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 22.06.5
* Added COUNTLY_EXCLUDE_PUSHNOTIFICATIONS flag in iOS React Native side to disable push notifications altogether.
* Forwarding push callbacks to appDelegate if CountlyRNPushNotifications.m is acting as push notification delegate.
* Underlying android SDK version is 22.06.2
* Underlying iOS SDK version is 22.06.2

## 22.06.4
* Fixed incorrect iOS push token type when passing "Countly.messagingMode.PRODUCTION" as token type.
* Underlying android SDK version is 22.06.2
Expand Down
4 changes: 2 additions & 2 deletions CountlyReactNative.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CountlyReactNative'
s.version = '22.06.4'
s.version = '22.06.5'
s.license = {
:type => 'COMMUNITY',
:text => <<-LICENSE
Expand Down Expand Up @@ -34,7 +34,7 @@ Pod::Spec.new do |s|
s.author = {'Countly' => 'hello@count.ly'}
s.source = { :git => 'https://github.com/Countly/countly-sdk-ios.git', :tag => s.version.to_s }
s.source_files = 'ios/src/*.{h,m}'
s.public_header_files = 'ios/src/CountlyReactNative.h'
s.public_header_files = 'ios/src/CountlyReactNative.h, ios/src/CountlyPushNotifications.h'
s.requires_arc = true
s.ios.deployment_target = '10.0'
s.osx.deployment_target = '10.14'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String toString(){
public class CountlyReactNative extends ReactContextBaseJavaModule implements LifecycleEventListener {

public static final String TAG = "CountlyRNPlugin";
private String COUNTLY_RN_SDK_VERSION_STRING = "22.06.4";
private String COUNTLY_RN_SDK_VERSION_STRING = "22.06.5";
private String COUNTLY_RN_SDK_NAME = "js-rnb-android";

private static final CountlyConfig config = new CountlyConfig();
Expand Down
2 changes: 1 addition & 1 deletion example/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rm App.js
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/App.js --output App.js
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/Example.js --output Example.js

yarn add countly-sdk-react-native-bridge@22.06.4
yarn add countly-sdk-react-native-bridge@22.06.5

cd ./ios
pod install
Expand Down
3 changes: 2 additions & 1 deletion ios/src/CountlyRNPushNotifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@interface CountlyRNPushNotifications : NSObject

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
+ (instancetype _Nonnull )sharedInstance;

- (void)startObservingNotifications;
Expand All @@ -20,4 +20,5 @@
- (void)setCountlyReactNative:(CountlyReactNative *_Nullable)countlyReactNative;
- (void)onNotification:(NSDictionary *_Nullable)notification;
- (void)onNotificationResponse:(UNNotificationResponse* _Nullable)response;
#endif
@end
22 changes: 17 additions & 5 deletions ios/src/CountlyRNPushNotifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#import "UserNotifications/UserNotifications.h"
#import "CountlyCommon.h"


#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
NSDictionary *lastStoredNotification = nil;
Result notificationListener = nil;
NSMutableArray *notifications = nil;
Expand All @@ -19,12 +19,12 @@
typedef NSString* CLYUserDefaultKey NS_EXTENSIBLE_STRING_ENUM;
CLYUserDefaultKey const CLYPushNotificationsKey = @"notificationsKey";
CLYUserDefaultKey const CLYPushButtonIndexKey = @"notificationBtnIndexKey";

#endif
@interface CountlyRNPushNotifications () <UNUserNotificationCenterDelegate>
@end

@implementation CountlyRNPushNotifications

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
+ (instancetype)sharedInstance
{
static CountlyRNPushNotifications* s_sharedInstance;
Expand Down Expand Up @@ -137,7 +137,12 @@ - (void)openURL:(NSString *)URLString
// when user open the app by tapping notification in any state.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
[self onNotificationResponse: response];
completionHandler();

id<UNUserNotificationCenterDelegate> appDelegate = (id<UNUserNotificationCenterDelegate>)UIApplication.sharedApplication.delegate;
if ([appDelegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)])
[appDelegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
else
completionHandler();
}

// When app is running and notification received
Expand All @@ -162,7 +167,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot
}
completionHandler(presentationOption);
}
completionHandler(UNNotificationPresentationOptionNone);

id<UNUserNotificationCenterDelegate> appDelegate = (id<UNUserNotificationCenterDelegate>)UIApplication.sharedApplication.delegate;

if ([appDelegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)])
[appDelegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
else
completionHandler(UNNotificationPresentationOptionNone);
}

- (void)onNotification:(NSDictionary *)notificationMessage
Expand Down Expand Up @@ -209,5 +220,6 @@ + (NSString *) toJSON: (NSDictionary *) json{
return jsonString;
}
}
#endif

@end
4 changes: 3 additions & 1 deletion ios/src/CountlyReactNative.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ typedef void (^Result)(id _Nullable result);
- (void)recordAttributionID:(NSArray*_Nullable)arguments;
- (void)appLoadingFinished;
- (void)disablePushNotifications;
- (void)notificationCallback:(NSString*_Nullable)notificationJson;

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
- (void)notificationCallback:(NSString*_Nullable)notificationJson;
+ (void)startObservingNotifications;
+ (void)onNotification:(NSDictionary *_Nullable)notification;
+ (void)onNotificationResponse:(UNNotificationResponse* _Nullable)response;
#endif
@end
27 changes: 23 additions & 4 deletions ios/src/CountlyReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#import "Countly.h"
#import "CountlyReactNative.h"
#import "CountlyConfig.h"
#import "CountlyPushNotifications.h"
#import "CountlyConnectionManager.h"
#import "CountlyRemoteConfig.h"
#import "CountlyCommon.h"

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
#import "CountlyRNPushNotifications.h"
#endif

#if DEBUG
#define COUNTLY_RN_LOG(fmt, ...) CountlyRNInternalLog(fmt, ##__VA_ARGS__)
Expand All @@ -22,7 +24,7 @@ @interface CountlyFeedbackWidget ()
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
@end

NSString* const kCountlyReactNativeSDKVersion = @"22.06.4";
NSString* const kCountlyReactNativeSDKVersion = @"22.06.5";
NSString* const kCountlyReactNativeSDKName = @"js-rnb-ios";

CLYPushTestMode const CLYPushTestModeProduction = @"CLYPushTestModeProduction";
Expand Down Expand Up @@ -57,7 +59,9 @@ - (instancetype)init

}

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance setCountlyReactNative:self];
#endif

return self;
}
Expand Down Expand Up @@ -89,15 +93,19 @@ - (instancetype)init

CountlyCommon.sharedInstance.SDKName = kCountlyReactNativeSDKName;
CountlyCommon.sharedInstance.SDKVersion = kCountlyReactNativeSDKVersion;

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
if(enablePushNotifications) {
[self addCountlyFeature:CLYPushNotifications];
}

#endif
if (serverurl != nil && [serverurl length] > 0) {
dispatch_async(dispatch_get_main_queue(), ^
{
[[Countly sharedInstance] startWithConfig:config];
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance recordPushActions];
#endif
resolve(@"Success");
});
}
Expand Down Expand Up @@ -194,13 +202,16 @@ - (instancetype)init

RCT_EXPORT_METHOD(disablePushNotifications)
{
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^ {
enablePushNotifications = false;
});
#endif
}

RCT_EXPORT_METHOD(sendPushToken:(NSArray*)arguments)
{
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^ {

NSString* token = [arguments objectAtIndex:0];
Expand All @@ -213,16 +224,17 @@ - (instancetype)init
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:urlString]];
});
#endif
}
RCT_EXPORT_METHOD(pushTokenType:(NSArray*)arguments)
{
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^ {
if (config == nil){
config = CountlyConfig.new;
}
config.sendPushTokenAlways = YES;
config.pushTestMode = CLYPushTestModeProduction;

NSString* tokenType = [arguments objectAtIndex:0];
if([tokenType isEqualToString: @"1"]){
config.pushTestMode = CLYPushTestModeDevelopment;
Expand All @@ -233,18 +245,24 @@ - (instancetype)init

CountlyPushNotifications.sharedInstance.pushTestMode = config.pushTestMode;
});
#endif
}

RCT_EXPORT_METHOD(askForNotificationPermission:(NSArray*)arguments)
{
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance askForNotificationPermission];
#endif
}
RCT_EXPORT_METHOD(registerForNotification:(NSArray*)arguments)
{
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance registerForNotification];
#endif

};

#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
- (void)notificationCallback:(NSString*_Nullable)notificationJson {
[self sendEventWithName:pushNotificationCallbackName body: notificationJson];
}
Expand All @@ -260,6 +278,7 @@ + (void)onNotification:(NSDictionary *_Nullable)notification {
+ (void)onNotificationResponse:(UNNotificationResponse* _Nullable)response {
[CountlyRNPushNotifications.sharedInstance onNotificationResponse:response];
}
#endif

+ (void) log: (NSString *) theMessage{
if(config.enableDebug == YES){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "countly-sdk-react-native-bridge",
"version": "22.06.4",
"version": "22.06.5",
"author": "Countly <hello@count.ly> (https://count.ly/)",
"bugs": {
"url": "https://github.com/Countly/countly-sdk-react-native-bridge/issues"
Expand Down

0 comments on commit 85318ed

Please sign in to comment.