Skip to content
Merged

Email #336

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9e687b5
Email SDK Changes
Nightsd01 Jan 30, 2018
ee30145
Multi-Requests
Nightsd01 Jan 30, 2018
086b32e
Email Logout
Nightsd01 Jan 30, 2018
992566e
Faster Tests
Nightsd01 Jan 30, 2018
a617ed8
Splitting/Refactoring Tests
Nightsd01 Jan 30, 2018
0082c5b
Re-Attempt Failed Network Requests
Nightsd01 Jan 31, 2018
c0b4325
Fix HTTP Request Re-Attempt Time Definitions
Nightsd01 Jan 31, 2018
2f1ff33
Multiple Re-Attempts
Nightsd01 Feb 1, 2018
fc9b0b1
OneSignal Email SDK Feature
Nightsd01 Feb 2, 2018
3705df9
Fix Tests
Nightsd01 Feb 2, 2018
8388a04
OneSignal SDK + Email
Nightsd01 Feb 3, 2018
9b47208
Email Fixes
Nightsd01 Feb 5, 2018
5964fec
Add Carrier Name
Nightsd01 Feb 6, 2018
04084c6
Fixed Tests
Nightsd01 Feb 6, 2018
e8ca60e
Email Callbacks on Main Thread
Nightsd01 Feb 6, 2018
5661dc4
Add Email to Swift Demo
Nightsd01 Feb 6, 2018
f57bf9f
Adds Invalid Email Test
Nightsd01 Feb 6, 2018
3ca87a7
Add require_email_auth tests
Nightsd01 Feb 7, 2018
945b425
Streamline Email Tests
Nightsd01 Feb 7, 2018
3db2484
Remove Hardcoded URL's
Nightsd01 Feb 7, 2018
dcd0feb
Split Email & Subscription Observer
Nightsd01 Feb 8, 2018
7f9b16e
Track In-App Purchase Safety Check
Nightsd01 Feb 9, 2018
3bb0d49
Update Server URL
Nightsd01 Feb 9, 2018
8218834
Add Email to OneSignal.getPermissionSubscriptionState()
Nightsd01 Feb 9, 2018
395a5e2
Avoid Duplicate Requests
Nightsd01 Feb 9, 2018
7ceb8a0
Fix iOS Params Request
Nightsd01 Feb 12, 2018
1ae3c37
Change setUnauthenticatedEmail
Nightsd01 Feb 13, 2018
8de0d74
Null Email Auth Token
Nightsd01 Feb 13, 2018
8f4595b
Add (unfinished) test for on_focus
Nightsd01 Feb 13, 2018
61136ae
Add Email Tests
Nightsd01 Feb 13, 2018
def6523
Fix Failing Test Build
Nightsd01 Feb 13, 2018
8d402b8
Remove Extraneous Log Statements
Nightsd01 Feb 13, 2018
4ccca07
Update Travis Config
Nightsd01 Feb 14, 2018
9261ae7
Fix Test
Nightsd01 Feb 14, 2018
47936a9
Change Travis Build Scheme
Nightsd01 Feb 14, 2018
d55a4ac
Fix Test Build Issue
Nightsd01 Feb 14, 2018
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ before_install:
script:
- xcodebuild -list
- xcodebuild build -scheme OneSignal-Dynamic
- xcodebuild -scheme UnitTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Air,OS=11.0' test
- xcodebuild -scheme UnitTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=11.0' test
10 changes: 5 additions & 5 deletions Examples/SwiftExample/OneSignalDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, OSPermissionObserver, OSS
let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in

print("Received Notification: \(notification!.payload.notificationID)")
print("launchURL = \(notification?.payload.launchURL)")
print("content_available = \(notification?.payload.contentAvailable)")
print("launchURL = \(notification?.payload.launchURL ?? "None")")
print("content_available = \(notification?.payload.contentAvailable ?? false)")
}

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
// This block gets called when the user reacts to a notification received
let payload: OSNotificationPayload? = result?.notification.payload

print("Message = \(payload!.body)")
print("badge number = \(payload?.badge)")
print("notification sound = \(payload?.sound)")
print("badge number = \(payload?.badge ?? 0)")
print("notification sound = \(payload?.sound ?? "None")")

if let additionalData = result!.notification.payload!.additionalData {
print("additionalData = \(additionalData)")
Expand Down Expand Up @@ -104,7 +104,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, OSPermissionObserver, OSS

let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppLaunchURL: true, ]

OneSignal.initWithLaunchOptions(launchOptions, appId: "<REPLACE_WITH_YOUR_ONESIGNAL_APP_ID", handleNotificationReceived: notificationReceivedBlock, handleNotificationAction: notificationOpenedBlock, settings: onesignalInitSettings)
OneSignal.initWithLaunchOptions(launchOptions, appId: "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", handleNotificationReceived: notificationReceivedBlock, handleNotificationAction: notificationOpenedBlock, settings: onesignalInitSettings)

OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification

Expand Down
81 changes: 65 additions & 16 deletions Examples/SwiftExample/OneSignalDemo/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

122 changes: 104 additions & 18 deletions Examples/SwiftExample/OneSignalDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
@IBOutlet weak var allowNotificationsSwitch: UISwitch!
@IBOutlet weak var setSubscriptionLabel: UILabel!
@IBOutlet weak var registerForPushNotificationsButton: UIButton!
@IBOutlet weak var setEmailButton: UIButton!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var setEmailActivityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var logoutEmailButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE);

let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let isSubscribed = status.subscriptionStatus.subscribed

Expand All @@ -50,20 +56,64 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
}
OneSignal.add(self as OSPermissionObserver)
OneSignal.add(self as OSSubscriptionObserver)

self.emailTextField.delegate = self;
}

func displayMessageToUser(_ title : String, _ message : String, actions : [UIAlertAction]?=nil) {

let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

for action in actions ?? [UIAlertAction(title: NSLocalizedString("OK", comment: "Alert OK button"), style: .cancel, handler: nil)] {
alertController.addAction(action);
}

self.present(alertController, animated: true, completion: nil)
}

func displaySettingsNotification() {
let message = NSLocalizedString("Please turn on notifications by going to Settings > Notifications > Allow Notifications", comment: "Alert message when the user has denied access to the notifications")
let alertController = UIAlertController(title: "OneSignal Example", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Alert OK button"), style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: NSLocalizedString("Settings", comment: "Alert button to open Settings"), style: .`default`, handler: { action in
var actions = [UIAlertAction]();

actions.append(UIAlertAction(title: NSLocalizedString("OK", comment: "Alert OK button"), style: .cancel, handler: nil));
actions.append(UIAlertAction(title: NSLocalizedString("Settings", comment: "Alert button to open Settings"), style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
}
}))
self.present(alertController, animated: true, completion: nil)
}));

self.displayMessageToUser(NSLocalizedString("OneSignal Example", comment: "title for the alert"), NSLocalizedString("Please turn on notifications by going to Settings > Notifications > Allow Notifications", comment: "Alert message when the user has denied access to the notifications"), actions: actions);
}

func changeEmailAnimationState(_ animating : Bool) {
UIView.animate(withDuration: 0.14) {
self.setEmailButton.alpha = animating ? 0.0 : 1.0;
};

self.setEmailButton.isEnabled = !animating;
self.setEmailButton.adjustsImageWhenDisabled = false;
animating ? self.setEmailActivityIndicatorView.startAnimating() : self.setEmailActivityIndicatorView.stopAnimating();
}

func setEmail() {
self.emailTextField.resignFirstResponder();

guard let emailAddress = self.emailTextField.text else { return };

self.changeEmailAnimationState(true);

OneSignal.setUnauthenticatedEmail(emailAddress, withSuccess: {
self.changeEmailAnimationState(false);

print("Successfully set email");

self.logoutEmailButton.isEnabled = true;
}) { (error) in
self.changeEmailAnimationState(false);

self.displayMessageToUser(NSLocalizedString("An Error Occurred", comment: "Title alerting the user that an error occurred"), NSLocalizedString("Encountered the following error while attempting to set unauthenticated email:\n\n\(error.debugDescription)", comment: "Shows the error to the user"));
};
}

func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
Expand All @@ -82,6 +132,8 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
}

func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {


if stateChanges.from.subscribed && !stateChanges.to.subscribed { // NOT SUBSCRIBED != DENIED
allowNotificationsSwitch.isOn = false
setSubscriptionLabel.text = "Set Subscription OFF"
Expand All @@ -93,6 +145,8 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
registerForPushNotificationsButton.backgroundColor = UIColor.green
registerForPushNotificationsButton.isUserInteractionEnabled = false
}

self.logoutEmailButton.isEnabled = stateChanges.to.emailUserId != nil;
}

@IBAction func onRegisterForPushNotificationsButton(_ sender: UIButton) {
Expand All @@ -115,6 +169,7 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse

@IBAction func onSendTagsButton(_ sender: UIButton) {


let tags: [AnyHashable : Any] = [
"some_key" : "some_value",
"users_name" : "Jon",
Expand All @@ -126,28 +181,41 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
OneSignal.sendTags(tags, onSuccess: { result in
print("Tags sent - \(result!)")
}) { error in
print("Error sending tags: \(error?.localizedDescription)")
print("Error sending tags: \(error?.localizedDescription ?? "None")")
}
}

@IBAction func onGetTagsButton(_ sender: UIButton) {
OneSignal.getTags({ tags in
print("tags - \(tags!)")
}, onFailure: { error in
print("Error getting tags - \(error?.localizedDescription)")
print("Error getting tags - \(error?.localizedDescription ?? "None")")
// errorWithDomain - OneSignalError
// code - HTTP error code from the OneSignal server
// userInfo - JSON OneSignal responded with
})
}

@IBAction func onDeleteOrUpdateTagsButton(_ sender: UIButton) {
//OneSignal.deleteTag("some_key")
OneSignal.deleteTags(["some_key", "users_name", "has_followers", "added_review"])
// To update tags simply add new ones
OneSignal.sendTags(["finished_level" : "60"])

@IBAction func setEmailButtonPressed(_ sender : UIButton) {
// NOTE: Because of implementation details, a call to setEmail() or setUnauthenticatedEmail() can sometimes take 30 seconds or more.

// OneSignal now has the ability to send emails to your users
// There are two methods to do so. Authenticated and Unauthenticated
// We heavily recommend taking the time to use authenticated setEmail with your backend

self.setEmail();
}

@IBAction func logoutEmailButtonPressed(_ sender: UIButton) {
OneSignal.logoutEmail(success: {
print("Successfully logged out of email");
}) { (error) in
self.displayMessageToUser(NSLocalizedString("An Error Occurred", comment: "Title to show that an error occurred"), NSLocalizedString("An error occurred while attempting to log out of the email for this device:\n\n\(error.debugDescription)", comment: "Displays the error that occurred"));
};
}


// User IDs
@IBAction func onGetIDsButton(_ sender: UIButton) {
//getPermissionSubscriptionState
Expand All @@ -161,16 +229,22 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
let userSubscriptionSetting = status.subscriptionStatus.userSubscriptionSetting
print("userSubscriptionSetting = \(userSubscriptionSetting)")
let userID = status.subscriptionStatus.userId
print("userID = \(userID)")
print("userID = \(userID ?? "None")")
let pushToken = status.subscriptionStatus.pushToken
print("pushToken = \(pushToken)")
print("pushToken = \(pushToken ?? "None")")
let emailId = status.subscriptionStatus.emailUserId;
print("Email userID = \(emailId ?? "None")");
}

@IBAction func onSyncEmailButton(_ sender: UIButton) {
// Optional method that sends us the user's email as an anonymized hash so that we can better target and personalize notifications sent to that user across their devices.
let testEmail = "test@test.test"
OneSignal.syncHashedEmail(testEmail)
print("sync hashedEmail successful")

if let email = self.emailTextField.text, email.isEmpty == false {
OneSignal.syncHashedEmail(email)
print("sync hashedEmail successful")
} else {
self.displayMessageToUser(NSLocalizedString("No Email Exists", comment: "Explains that no email is currently set"), NSLocalizedString("You must fill out the email address in the text field before syncing it.", comment: "Explains that no email is currently set in the text field"));
}
}

@IBAction func onPromptLocationButton(_ sender: UIButton) {
Expand Down Expand Up @@ -247,3 +321,15 @@ class ViewController: UIViewController, OSPermissionObserver, OSSubscriptionObse
}
}
}

extension ViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder();

if (textField.text?.count ?? 0 > 0) {
self.setEmail();
}

return true;
}
}
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import <UIKit/UIKit.h>
#import <OneSignal/OneSignal.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, OSPermissionObserver, OSSubscriptionObserver>
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSPermissionObserver, OSSubscriptionObserver, OSEmailSubscriptionObserver>

@property (strong, nonatomic) UIWindow *window;

Expand Down
13 changes: 9 additions & 4 deletions iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

id notificationReceiverBlock = ^(OSNotification *notification) {
NSLog(@"Received Notification - %@", notification.payload.notificationID);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:11];
};

[OneSignal initWithLaunchOptions:launchOptions
appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba"
appId:@"5dc0b8c7-335a-4c4c-9ed4-266cbf2158ac"
handleNotificationReceived:notificationReceiverBlock
handleNotificationAction:openNotificationHandler
settings:@{kOSSettingsKeyAutoPrompt: @false,
Expand All @@ -76,6 +78,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[OneSignal addPermissionObserver:self];
[OneSignal addSubscriptionObserver:self];
[OneSignal addEmailSubscriptionObserver:self];

NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate);

Expand All @@ -92,6 +95,11 @@ - (void) onOSPermissionChanged:(OSPermissionStateChanges*)stateChanges {
NSLog(@"HERE");
}

-(void)onOSEmailSubscriptionChanged:(OSEmailSubscriptionStateChanges *)stateChanges {
NSLog(@"onOSEmailSubscriptionChanged: %@", stateChanges);

}


- (void)applicationWillResignActive:(UIApplication *)application {
}
Expand All @@ -115,9 +123,6 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
// kFIRParameterCampaign: @"some title"
// }];

NSString* test = @"{ \
\"user_list\": [] \
}";
}


Expand Down
Loading