Skip to content

Commit 20085ee

Browse files
authored
Improved Swift Example (#204)
* Added examples for each OneSignal SDK function. * Including action button handling opening different view controllers in the app.
1 parent 7621488 commit 20085ee

File tree

9 files changed

+579
-104
lines changed

9 files changed

+579
-104
lines changed

Examples/SwiftExample/OneSignalDemo.xcodeproj/project.pbxproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
3E20C84D1D8787B6004ACDEA /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3EA62EFB1D877EF200E5845A /* UserNotifications.framework */; };
1313
9112E8B01E725C1E0022A1CB /* NotificationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9112E8AF1E725C1E0022A1CB /* NotificationService.swift */; };
1414
9112E8B41E725C1E0022A1CB /* OneSignalNotificationServiceExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 9112E8AD1E725C1E0022A1CB /* OneSignalNotificationServiceExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
15+
9150E7691E734F6E00C5D46A /* GreenViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9150E7671E734F6E00C5D46A /* GreenViewController.swift */; };
16+
9150E76A1E734F6E00C5D46A /* RedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9150E7681E734F6E00C5D46A /* RedViewController.swift */; };
17+
9150E76D1E73556A00C5D46A /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9150E76C1E73556A00C5D46A /* CoreLocation.framework */; };
1518
91C3CFD91E7266F600D9EA80 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91C3CFD11E7266F600D9EA80 /* AppDelegate.swift */; };
1619
91C3CFDA1E7266F600D9EA80 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 91C3CFD21E7266F600D9EA80 /* Assets.xcassets */; };
1720
91C3CFDB1E7266F600D9EA80 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 91C3CFD31E7266F600D9EA80 /* LaunchScreen.storyboard */; };
@@ -52,6 +55,10 @@
5255
9112E8AD1E725C1E0022A1CB /* OneSignalNotificationServiceExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = OneSignalNotificationServiceExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
5356
9112E8AF1E725C1E0022A1CB /* NotificationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationService.swift; sourceTree = "<group>"; };
5457
9112E8B11E725C1E0022A1CB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
58+
9150E7671E734F6E00C5D46A /* GreenViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GreenViewController.swift; sourceTree = "<group>"; };
59+
9150E7681E734F6E00C5D46A /* RedViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedViewController.swift; sourceTree = "<group>"; };
60+
9150E76B1E734FB000C5D46A /* OneSignalDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OneSignalDemo.entitlements; sourceTree = "<group>"; };
61+
9150E76C1E73556A00C5D46A /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
5562
91C3CFD11E7266F600D9EA80 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5663
91C3CFD21E7266F600D9EA80 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5764
91C3CFD41E7266F600D9EA80 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -77,6 +84,7 @@
7784
isa = PBXFrameworksBuildPhase;
7885
buildActionMask = 2147483647;
7986
files = (
87+
9150E76D1E73556A00C5D46A /* CoreLocation.framework in Frameworks */,
8088
3E20C84D1D8787B6004ACDEA /* UserNotifications.framework in Frameworks */,
8189
378310DE5E74831CBC5C833C /* Pods_OneSignalDemo.framework in Frameworks */,
8290
);
@@ -99,6 +107,7 @@
99107
3EA62EFA1D877EF200E5845A /* Frameworks */ = {
100108
isa = PBXGroup;
101109
children = (
110+
9150E76C1E73556A00C5D46A /* CoreLocation.framework */,
102111
3EA62EFB1D877EF200E5845A /* UserNotifications.framework */,
103112
6DE881DDE387B7869A75E56A /* Pods_OneSignalDemo.framework */,
104113
7D32C4978E51F05AA82FC597 /* libPods-OneSignalNotificationServiceExtension.a */,
@@ -118,6 +127,9 @@
118127
91C3CFD01E7266F600D9EA80 /* OneSignalDemo */ = {
119128
isa = PBXGroup;
120129
children = (
130+
9150E76B1E734FB000C5D46A /* OneSignalDemo.entitlements */,
131+
9150E7671E734F6E00C5D46A /* GreenViewController.swift */,
132+
9150E7681E734F6E00C5D46A /* RedViewController.swift */,
121133
91C3CFD11E7266F600D9EA80 /* AppDelegate.swift */,
122134
91C3CFD21E7266F600D9EA80 /* Assets.xcassets */,
123135
91C3CFD31E7266F600D9EA80 /* LaunchScreen.storyboard */,
@@ -215,6 +227,9 @@
215227
com.apple.BackgroundModes = {
216228
enabled = 1;
217229
};
230+
com.apple.Push = {
231+
enabled = 1;
232+
};
218233
};
219234
};
220235
};
@@ -350,6 +365,8 @@
350365
isa = PBXSourcesBuildPhase;
351366
buildActionMask = 2147483647;
352367
files = (
368+
9150E76A1E734F6E00C5D46A /* RedViewController.swift in Sources */,
369+
9150E7691E734F6E00C5D46A /* GreenViewController.swift in Sources */,
353370
91C3CFDE1E7266F600D9EA80 /* ViewController.swift in Sources */,
354371
91C3CFD91E7266F600D9EA80 /* AppDelegate.swift in Sources */,
355372
);
@@ -513,6 +530,7 @@
513530
baseConfigurationReference = FBF7B1679DAECB1F8823DFA8 /* Pods-OneSignalDemo.debug.xcconfig */;
514531
buildSettings = {
515532
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
533+
CODE_SIGN_ENTITLEMENTS = OneSignalDemo/OneSignalDemo.entitlements;
516534
DEVELOPMENT_TEAM = 99SW8E36CT;
517535
INFOPLIST_FILE = "$(SRCROOT)/OneSignalDemo/Info.plist";
518536
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -528,6 +546,7 @@
528546
baseConfigurationReference = 2F2B914C976A118DAE4F0182 /* Pods-OneSignalDemo.release.xcconfig */;
529547
buildSettings = {
530548
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
549+
CODE_SIGN_ENTITLEMENTS = OneSignalDemo/OneSignalDemo.entitlements;
531550
DEVELOPMENT_TEAM = 99SW8E36CT;
532551
INFOPLIST_FILE = "$(SRCROOT)/OneSignalDemo/Info.plist";
533552
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";

Examples/SwiftExample/OneSignalDemo/AppDelegate.swift

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Modified MIT License
33
*
4-
* Copyright 2016 OneSignal
4+
* Copyright 2017 OneSignal
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -28,34 +28,91 @@
2828
import UIKit
2929
import OneSignal
3030

31+
3132
@UIApplicationMain
3233
class AppDelegate: UIResponder, UIApplicationDelegate {
3334

3435
var window: UIWindow?
35-
36+
37+
let redViewController = RedViewController()
38+
let greenViewController = GreenViewController()
39+
3640
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
3741

42+
// For debugging
3843
OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
44+
45+
// Replace 'b2f7f966-d8cc-11e4-bed1-df8f05be55ba' with your OneSignal App ID
46+
OneSignal.initWithLaunchOptions(launchOptions, appId: "b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
47+
handleNotificationReceived: {
48+
// OSHandleNotificationRecevedBlock - Function to be called when a notification is received
49+
notification in
50+
51+
print("notificationID - \((notification?.payload.notificationID)!)")
52+
print("launchURL = \(notification?.payload.launchURL)")
53+
54+
// content_available is NOT APPLICABLE IF APP HAS BEEN SWIPED AWAY
55+
print("content_available = \(notification?.payload.contentAvailable)")
56+
},
57+
handleNotificationAction:
58+
{ // OSHandleNotificationActionBlock - Function to be called when a user reacts to a notification received
59+
result in
3960

40-
OneSignal.initWithLaunchOptions(launchOptions, appId: "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", handleNotificationReceived: { (notification) in
41-
print("Received Notification - \(notification?.payload.notificationID)")
42-
}, handleNotificationAction: { (result) in
61+
let displayType: OSNotificationDisplayType? = result?.notification.displayType
62+
print("displayType = \(displayType!.rawValue)")
63+
64+
let wasShown: Bool? = result?.notification.wasShown
65+
print("wasShown = \(wasShown!)")
66+
67+
// https://documentation.onesignal.com/docs/ios-native-sdk#section--osnotificationpayload-
68+
let payload: OSNotificationPayload? = result?.notification.payload
69+
70+
print("badge number = \(payload?.badge)")
71+
print("notification sound = \(payload?.sound)")
4372

44-
// This block gets called when the user reacts to a notification received
45-
let payload = result?.notification.payload
46-
var fullMessage = payload?.title
73+
if let additionalData: [AnyHashable : Any]? = payload?.additionalData {
74+
print("additionalData = \(additionalData)")
75+
}
4776

48-
//Try to fetch the action selected
49-
if let actionSelected = result?.action.actionID {
50-
fullMessage = fullMessage! + "\nPressed ButtonId:\(actionSelected)"
77+
if let actionSelected = payload?.actionButtons {
78+
print("actionSelected = \(actionSelected)")
5179
}
5280

53-
print(fullMessage)
81+
82+
if let actionID = result?.action.actionID {
83+
84+
// For presenting a ViewController from push notification action button
85+
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
86+
let instantiateRedViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "RedVCID") as UIViewController
87+
let instantiatedGreenViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "GreenVCID") as UIViewController
88+
self.window = UIWindow(frame: UIScreen.main.bounds)
89+
90+
print("actionID = \(actionID)")
91+
92+
if actionID == "id2" {
93+
print("do something when button 2 is pressed")
94+
self.window?.rootViewController = instantiateRedViewController
95+
self.window?.makeKeyAndVisible()
96+
97+
98+
} else if actionID == "id1" {
99+
print("do something when button 1 is pressed")
100+
self.window?.rootViewController = instantiatedGreenViewController
101+
self.window?.makeKeyAndVisible()
102+
103+
}
104+
}
105+
},
106+
settings: [
107+
kOSSettingsKeyAutoPrompt : false, // automatically prompts users to Enable Notifications
54108

55-
}, settings: [kOSSettingsKeyAutoPrompt : true,
56-
kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.notification.rawValue])
109+
kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.notification.rawValue,
110+
111+
kOSSettingsKeyInAppLaunchURL: true // true-default
112+
])
57113

58114
return true
59115
}
116+
60117
}
61118

Examples/SwiftExample/OneSignalDemo/Base.lproj/LaunchScreen.storyboard

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1217" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
510
</dependencies>
611
<scenes>
712
<!--View Controller-->
@@ -13,10 +18,9 @@
1318
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
1419
</layoutGuides>
1520
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1722
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<animations/>
19-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
23+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2024
</view>
2125
</viewController>
2226
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>

0 commit comments

Comments
 (0)