Skip to content

Commit ad07ec9

Browse files
authored
Merge pull request #1189 from OneSignal/live-activity-support-5.0.0
Live activity support 5.0.0
2 parents 0fe3a1a + e93b359 commit ad07ec9

30 files changed

+1337
-62
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
105105
return YES;
106106
}
107107

108-
#define ONESIGNAL_APP_ID_DEFAULT @"YOUR_APP_ID_HERE"
108+
#define ONESIGNAL_APP_ID_DEFAULT @"77e32082-ea27-42e3-a898-c72e141824ef"
109109
#define ONESIGNAL_APP_ID_KEY_FOR_TESTING @"YOUR_APP_ID_HERE"
110110

111111
+ (NSString*)getOneSignalAppId {

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Base.lproj/Main.storyboard

Lines changed: 95 additions & 54 deletions
Large diffs are not rendered by default.

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>NSSupportsLiveActivities</key>
6+
<true/>
57
<key>ITSAppUsesNonExemptEncryption</key>
68
<false/>
79
<key>UIUserInterfaceStyle</key>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2023 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
import Foundation
29+
import ActivityKit
30+
import UserNotifications
31+
32+
struct OneSignalWidgetAttributes: ActivityAttributes {
33+
public struct ContentState: Codable, Hashable {
34+
// Dynamic stateful properties about your activity go here!
35+
var message: String
36+
}
37+
38+
// Fixed non-changing properties about your activity go here!
39+
var title: String
40+
}
41+
@objc
42+
class LiveActivityController: NSObject {
43+
// To aid in testing
44+
static var counter = 0
45+
@available(iOS 13.0, *)
46+
@objc
47+
static func createActivity() async -> String? {
48+
if #available(iOS 16.1, *) {
49+
counter += 1;
50+
let attributes = OneSignalWidgetAttributes(title: "#" + String(counter) + " OneSignal Dev App Live Activity")
51+
let contentState = OneSignalWidgetAttributes.ContentState(message: "Update this message through push or with Activity Kit")
52+
do {
53+
let activity = try Activity<OneSignalWidgetAttributes>.request(
54+
attributes: attributes,
55+
contentState: contentState,
56+
pushType: .token)
57+
for await data in activity.pushTokenUpdates {
58+
let myToken = data.map {String(format: "%02x", $0)}.joined()
59+
return myToken
60+
}
61+
} catch (let error) {
62+
print(error.localizedDescription)
63+
return nil
64+
}
65+
}
66+
return nil
67+
}
68+
}

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
@property (weak, nonatomic) IBOutlet UITextView *result;
6565
@property (weak, nonatomic) IBOutlet UITextField *tagKey;
6666
@property (weak, nonatomic) IBOutlet UITextField *tagValue;
67+
@property (weak, nonatomic) IBOutlet UITextField *activityId;
6768

6869
@end
6970

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#import "ViewController.h"
3232
#import "AppDelegate.h"
33+
#import "OneSignalExample-Swift.h"
3334

3435
@implementation ViewController
3536

@@ -201,4 +202,25 @@ - (IBAction)sendUniqueOutcomeEvent:(id)sender {
201202
[OneSignal.Session addUniqueOutcome:[_outcomeUniqueName text]];
202203
}
203204

205+
- (IBAction)startAndEnterLiveActivity:(id)sender {
206+
if (@available(iOS 13.0, *)) {
207+
NSString *activityId = [self.activityId text];
208+
// Will not make a live activity if activityId is empty
209+
if (activityId && activityId.length) {
210+
[LiveActivityController createActivityWithCompletionHandler:^(NSString * token) {
211+
if(token){
212+
[OneSignal enterLiveActivity:activityId withToken:token];
213+
}
214+
}];
215+
}
216+
} else {
217+
NSLog(@"Must use iOS 13 or later for swift concurrency which is required for [LiveActivityController createActivityWithCompletionHandler...");
218+
}
219+
}
220+
- (IBAction)exitLiveActivity:(id)sender {
221+
if (self.activityId.text && self.activityId.text.length) {
222+
[OneSignal exitLiveActivity:self.activityId.text];
223+
}
224+
}
225+
204226
@end

0 commit comments

Comments
 (0)