Skip to content

Commit 7bc3765

Browse files
authored
Merge pull request #224 from OneSignal/iam-ios
IAM functionality added
2 parents 5eebe49 + 1aa6aa4 commit 7bc3765

File tree

7 files changed

+177
-86
lines changed

7 files changed

+177
-86
lines changed

OneSignalExample/Assets/OneSignal/Editor/PostProcessBuildPlayer_iOS.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public static void OnPostProcessBuild(BuildTarget target, string path)
3434
string targetName = PBXProject.GetUnityTargetName();
3535
string targetGUID = project.TargetGuidByName(targetName);
3636

37-
// UserNotifications.framework is required by libOneSignal.a
38-
project.AddFrameworkToProject(targetGUID, "UserNotifications.framework", false);
37+
var frameworks = new string[] {"NotificationCenter.framework", "UserNotifications.framework", "UIKit.framework", "SystemConfiguration.framework", "CoreGraphics.framework", "WebKit.framework"};
38+
39+
foreach (string framework in frameworks) {
40+
project.AddFrameworkToProject (targetGUID, framework, false);
41+
}
3942

4043
#if UNITY_2017_2_OR_NEWER && !UNITY_CLOUD_BUILD
4144

@@ -63,8 +66,6 @@ public static void OnPostProcessBuild(BuildTarget target, string path)
6366
project.AddFileToBuild (notificationServiceTarget, project.AddFile (sourceDestination + ".h", sourceDestination + ".h", PBXSourceTree.Source));
6467
project.AddFileToBuild (notificationServiceTarget, project.AddFile (sourceDestination + ".m", sourceDestination + ".m", PBXSourceTree.Source));
6568

66-
var frameworks = new string[] {"NotificationCenter.framework", "UserNotifications.framework", "UIKit.framework", "SystemConfiguration.framework"};
67-
6869
foreach (string framework in frameworks) {
6970
project.AddFrameworkToProject (notificationServiceTarget, framework, true);
7071
}

OneSignalExample/Assets/OneSignal/Example/GameControllerExample.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Modified MIT License
33
*
44
* Copyright 2016 OneSignal
@@ -76,7 +76,10 @@ private void OneSignalInAppMessageTriggerExamples() {
7676
OneSignal.AddTrigger("key", "value");
7777

7878
// Get the current value to a trigger by key
79-
var triggerValue = OneSignal.GetTriggerValueForKey("key");
79+
var triggerKey = "key";
80+
var triggerValue = OneSignal.GetTriggerValueForKey(triggerKey);
81+
String output = "Trigger key: " + triggerKey + " value: " + (String) triggerValue;
82+
Console.WriteLine(output);
8083

8184
// Add multiple triggers
8285
OneSignal.AddTriggers(new Dictionary<string, object>() { { "key1", "value1" }, { "key2", 2 } });
@@ -151,7 +154,12 @@ public static void HandleNotificationOpened(OSNotificationOpenedResult result) {
151154
}
152155

153156
public static void HandlerInAppMessageClicked(OSInAppMessageAction action) {
154-
String logInAppClickEvent = "In-App Message opened with action.clickName " + action.clickName;
157+
String logInAppClickEvent = "In-App Message Clicked: " +
158+
"\nClick Name: " + action.clickName +
159+
"\nClick Url: " + action.clickUrl +
160+
"\nFirst Click: " + action.firstClick +
161+
"\nCloses Message: " + action.closesMessage;
162+
155163
print(logInAppClickEvent);
156164
extraMessage = logInAppClickEvent;
157165
}

OneSignalExample/Assets/OneSignal/Platforms/iOS/OneSignal.h

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@
4444
***/
4545

4646
#import <Foundation/Foundation.h>
47-
48-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
49-
#define XC8_AVAILABLE 1
5047
#import <UserNotifications/UserNotifications.h>
51-
#endif
52-
5348

5449
#pragma clang diagnostic push
5550
#pragma clang diagnostic ignored "-Wstrict-prototypes"
@@ -73,6 +68,26 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {
7368
OSNotificationDisplayTypeNotification
7469
};
7570

71+
@interface OSInAppMessageAction : NSObject
72+
73+
// The action name attached to the IAM action
74+
@property (strong, nonatomic, nullable) NSString *clickName;
75+
76+
// The URL (if any) that should be opened when the action occurs
77+
@property (strong, nonatomic, nullable) NSURL *clickUrl;
78+
79+
// Whether or not the click action is first click on the IAM
80+
@property (nonatomic) BOOL firstClick;
81+
82+
// Whether or not the click action dismisses the message
83+
@property (nonatomic) BOOL closesMessage;
84+
85+
@end
86+
87+
@protocol OSInAppMessageDelegate <NSObject>
88+
@optional
89+
- (void)handleMessageAction:(OSInAppMessageAction * _Nonnull)action NS_SWIFT_NAME(handleMessageAction(action:));
90+
@end
7691

7792
@interface OSNotificationAction : NSObject
7893

@@ -133,7 +148,7 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {
133148
@property(readonly)NSString* subtitle;
134149
@property(readonly)NSString* body;
135150

136-
/* Web address to launch within the app via a UIWebView */
151+
/* Web address to launch within the app via a WKWebView */
137152
@property(readonly)NSString* launchURL;
138153

139154
/* Additional key value properties set within the payload */
@@ -181,11 +196,8 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {
181196
@property(readonly, getter=isSilentNotification)BOOL silentNotification;
182197

183198
/* iOS 10+: Indicates whether or not the received notification has mutableContent : 1 assigned to its payload
184-
Used for UNNotificationServiceExtension to launch extension.
185-
*/
186-
#if XC8_AVAILABLE
199+
Used for UNNotificationServiceExtension to launch extension. */
187200
@property(readonly, getter=hasMutableContent)BOOL mutableContent;
188-
#endif
189201

190202
/* Convert object into an NSString that can be convertible into a custom Dictionary / JSON Object */
191203
- (NSString*)stringify;
@@ -218,16 +230,6 @@ typedef NS_ENUM(NSInteger, OSNotificationPermission) {
218230
OSNotificationPermissionProvisional
219231
};
220232

221-
typedef void (^OSNotificationDisplayTypeResponse)(OSNotificationDisplayType displayType);
222-
223-
// Notification Display Type Delegate
224-
// Allows apps to customize per-notification display-type
225-
@protocol OSNotificationDisplayTypeDelegate <NSObject>
226-
- (void)willPresentInFocusNotificationWithPayload:(OSNotificationPayload *)payload
227-
withCompletion:(OSNotificationDisplayTypeResponse)completion;
228-
@end
229-
230-
231233
// Permission Classes
232234
@interface OSPermissionState : NSObject
233235

@@ -318,6 +320,9 @@ typedef void (^OSHandleNotificationReceivedBlock)(OSNotification* notification);
318320
/*Block for handling a user reaction to a notification*/
319321
typedef void (^OSHandleNotificationActionBlock)(OSNotificationOpenedResult * result);
320322

323+
/*Block for handling user click on an in app message*/
324+
typedef void (^OSHandleInAppMessageActionClickBlock)(OSInAppMessageAction* action);
325+
321326
/*Dictionary of keys to pass alongside the init settings*/
322327

323328
/*Let OneSignal directly prompt for push notifications on init*/
@@ -415,10 +420,6 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
415420

416421
+ (OSPermissionSubscriptionState*)getPermissionSubscriptionState;
417422

418-
// When the app is in-focus, this allows you to add a delegate that can customize the
419-
// display type for specific notifications
420-
+ (void)setNotificationDisplayTypeDelegate:(NSObject<OSNotificationDisplayTypeDelegate>*)delegate;
421-
422423
+ (void)addPermissionObserver:(NSObject<OSPermissionObserver>*)observer;
423424
+ (void)removePermissionObserver:(NSObject<OSPermissionObserver>*)observer;
424425

@@ -429,6 +430,8 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
429430
+ (void)removeEmailSubscriptionObserver:(NSObject<OSEmailSubscriptionObserver>*)observer;
430431

431432
+ (void)setSubscription:(BOOL)enable;
433+
+ (BOOL)isInAppMessagingPaused;
434+
+ (void)pauseInAppMessages:(BOOL)pause;
432435

433436
// - Posting Notification
434437
+ (void)postNotification:(NSDictionary*)jsonData;
@@ -444,6 +447,7 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
444447
// Only used for wrapping SDKs, such as Unity, Cordova, Xamarin, etc.
445448
+ (void)setMSDKType:(NSString*)type;
446449

450+
+ (void)setInAppMessageClickHandler:(OSHandleInAppMessageActionClickBlock)delegate;
447451

448452
// iOS 10 only
449453
// Process from Notification Service Extension.
@@ -474,6 +478,14 @@ typedef void (^OSEmailSuccessBlock)();
474478
+ (void)setEmail:(NSString * _Nonnull)email;
475479
+ (void)setEmail:(NSString * _Nonnull)email withEmailAuthHashToken:(NSString * _Nullable)hashToken;
476480

481+
// In App Messaging Trigger methods
482+
+ (void)addTrigger:(NSString * _Nonnull)key withValue:(id _Nonnull)value;
483+
+ (void)addTriggers:(NSDictionary<NSString *, id> * _Nonnull)triggers;
484+
+ (void)removeTriggerForKey:(NSString * _Nonnull)key;
485+
+ (void)removeTriggersForKeys:(NSArray<NSString *> * _Nonnull)keys;
486+
+ (NSDictionary<NSString *, id> * _Nonnull)getTriggers;
487+
+ (id _Nullable)getTriggerValueForKey:(NSString * _Nonnull)key;
488+
477489
+ (void)setExternalUserId:(NSString * _Nonnull)externalId;
478490
+ (void)removeExternalUserId;
479491

OneSignalExample/Assets/OneSignal/Platforms/iOS/OneSignalUnityRuntime.m

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
#import "OneSignal.h"
2929
#import <objc/runtime.h>
3030

31+
char* cStringCopy(const char* string) {
32+
if (string == NULL)
33+
return NULL;
34+
35+
char* res = (char*)malloc(strlen(string) + 1);
36+
strcpy(res, string);
37+
38+
return res;
39+
}
40+
3141
NSString* CreateNSString(const char* string) {
3242
return [NSString stringWithUTF8String: string ? string : ""];
3343
}
@@ -117,7 +127,7 @@ + (void)load {
117127
static Class delegateClass = nil;
118128

119129
- (void) setOneSignalUnityDelegate:(id<UIApplicationDelegate>)delegate {
120-
if(delegateClass) {
130+
if (delegateClass) {
121131
[self setOneSignalUnityDelegate:delegate];
122132
return;
123133
}
@@ -146,6 +156,20 @@ void processNotificationReceived(NSString* notificationString) {
146156
UnitySendMessage(unityListener, "onPushNotificationReceived", [notificationString UTF8String]);
147157
}
148158

159+
void processInAppMessageClicked(char* inAppMessageActionString) {
160+
UnitySendMessage(unityListener, "onInAppMessageClicked", inAppMessageActionString);
161+
}
162+
163+
char* createInAppMessageJsonString(OSInAppMessageAction* action) {
164+
return cStringCopy(dictionaryToJsonChar(
165+
@{
166+
@"click_name" : action.clickName,
167+
@"click_url" : action.clickUrl ? action.clickUrl.absoluteString : @"",
168+
@"first_click" : @(action.firstClick),
169+
@"closes_message" : @(action.closesMessage)
170+
}));
171+
}
172+
149173
void initOneSignalObject(NSDictionary* launchOptions, const char* appId, int displayOption, BOOL inAppLaunchURL, BOOL autoPrompt, BOOL fromColdStart) {
150174

151175
NSString* appIdStr = (appId ? [NSString stringWithUTF8String: appId] : nil);
@@ -165,6 +189,9 @@ void initOneSignalObject(NSDictionary* launchOptions, const char* appId, int dis
165189
kOSSettingsKeyInAppLaunchURL: @(inAppLaunchURL),
166190
@"kOSSettingsKeyInOmitNoAppIdLogging": @(fromColdStart)}];
167191

192+
[OneSignal setInAppMessageClickHandler:^(OSInAppMessageAction* action) {
193+
processInAppMessageClicked(createInAppMessageJsonString(action));
194+
}];
168195
}
169196

170197
void _init(const char* listenerName, const char* appId, BOOL autoPrompt, BOOL inAppLaunchURL, int displayOption, int logLevel, int visualLogLevel, bool requiresUserPrivacyConsent) {
@@ -192,13 +219,13 @@ void _sendTag(const char* tagName, const char* tagValue) {
192219
}
193220

194221
void _sendTags(const char* tags) {
195-
NSString * jsonString = CreateNSString(tags);
222+
NSString* jsonString = CreateNSString(tags);
196223

197224
NSError* jsonError;
198225

199226
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
200227
NSDictionary* keyValuePairs = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
201-
if (jsonError == nil)
228+
if (!jsonError)
202229
[OneSignal sendTags:keyValuePairs];
203230
}
204231

@@ -207,13 +234,13 @@ void _deleteTag(const char* key) {
207234
}
208235

209236
void _deleteTags(const char* keys) {
210-
NSString * jsonString = CreateNSString(keys);
237+
NSString* jsonString = CreateNSString(keys);
211238

212239
NSError* jsonError;
213240

214241
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
215242
NSArray* kk = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
216-
if (jsonError == nil)
243+
if (!jsonError)
217244
[OneSignal deleteTags:kk];
218245
}
219246

@@ -225,7 +252,7 @@ void _getTags() {
225252

226253
void _idsAvailable() {
227254
[OneSignal IdsAvailable:^(NSString* userId, NSString* pushToken) {
228-
if (pushToken == nil)
255+
if (!pushToken)
229256
pushToken = @"";
230257

231258
UnitySendMessage(unityListener, "onIdsAvailable",
@@ -238,12 +265,12 @@ void _setSubscription(BOOL enable) {
238265
}
239266

240267
void _postNotification(const char* jsonData) {
241-
NSString * jsonString = CreateNSString(jsonData);
268+
NSString* jsonString = CreateNSString(jsonData);
242269
NSError* jsonError;
243270

244271
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
245272
NSDictionary* jsd = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
246-
if (jsonError == nil)
273+
if (!jsonError)
247274
[OneSignal postNotification:jsd onSuccess:^(NSDictionary* results) {
248275
UnitySendMessage(unityListener, "onPostNotificationSuccess", dictionaryToJsonChar(results));
249276
} onFailure:^(NSError* error) {
@@ -309,8 +336,6 @@ void _setOneSignalLogLevel(int logLevel, int visualLogLevel) {
309336
[OneSignal setLogLevel:logLevel visualLevel: visualLogLevel];
310337
}
311338

312-
// email
313-
314339
void _setUnauthenticatedEmail(const char*email) {
315340
[OneSignal setEmail:CreateNSString(email) withSuccess:^{
316341
UnitySendMessage(unityListener, "onSetEmailSuccess", dictionaryToJsonChar(@{@"status" : @"success"}));
@@ -359,4 +384,41 @@ void _removeExternalUserId() {
359384
[OneSignal removeExternalUserId];
360385
}
361386

387+
void _addTriggers(char *triggers) {
388+
NSString* jsonString = CreateNSString(triggers);
389+
390+
NSError* jsonError;
391+
392+
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
393+
NSDictionary* triggerKeyValuePairs = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
394+
if (!jsonError)
395+
[OneSignal addTriggers:triggerKeyValuePairs];
396+
}
397+
398+
void _removeTriggerForKey(char *key) {
399+
NSString* triggerKey = CreateNSString(key);
400+
[OneSignal removeTriggerForKey:triggerKey];
401+
}
402+
403+
void _removeTriggersForKeys(char *keys) {
404+
NSString* jsonString = CreateNSString(keys);
405+
406+
NSError* jsonError;
407+
408+
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
409+
NSArray* triggerKeys = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
410+
if (!jsonError)
411+
[OneSignal removeTriggersForKeys:triggerKeys];
412+
}
413+
414+
char* _getTriggerValueForKey(char *key) {
415+
NSString* triggerKey = CreateNSString(key);
416+
NSDictionary* triggerValue = @{ @"value" : [OneSignal getTriggerValueForKey:triggerKey] };
417+
return cStringCopy(dictionaryToJsonChar(triggerValue));
418+
}
419+
420+
void _pauseInAppMessages(bool pause) {
421+
[OneSignal pauseInAppMessages:pause];
422+
}
423+
362424
@end
Binary file not shown.

OneSignalExample/Assets/OneSignal/src/OneSignal.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,23 +648,20 @@ public static bool UserProvidedConsent() {
648648
#endif
649649
}
650650

651-
public static void SetRequiresUserPrivacyConsent(bool required)
652-
{
651+
public static void SetRequiresUserPrivacyConsent(bool required) {
653652
#if ONESIGNAL_PLATFORM
654653
OneSignal.requiresUserConsent = required;
655654
#endif
656655
}
657656

658657

659-
public static void SetExternalUserId(string externalId)
660-
{
658+
public static void SetExternalUserId(string externalId) {
661659
#if ONESIGNAL_PLATFORM
662660
oneSignalPlatform.SetExternalUserId(externalId);
663661
#endif
664662
}
665663

666-
public static void RemoveExternalUserId()
667-
{
664+
public static void RemoveExternalUserId() {
668665
#if ONESIGNAL_PLATFORM
669666
oneSignalPlatform.RemoveExternalUserId();
670667
#endif

0 commit comments

Comments
 (0)