Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-4234] Updates Push Example to include Channels #1879

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Changes from 1 commit
Commits
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
Next Next commit
[DRAFT] adds code for channels push & fixes push example to work with…
… simulators
umair-ably committed Feb 20, 2024
commit df72aa02317b663af438107427a49c3b1f4278ed
14 changes: 8 additions & 6 deletions Examples/AblyPush/AblyPushExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -458,10 +458,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = AblyPushExample/AblyPushExample.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"AblyPushExample/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyPushExample/Info.plist;
@@ -491,10 +492,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = AblyPushExample/AblyPushExample.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"AblyPushExample/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyPushExample/Info.plist;
@@ -526,7 +528,7 @@
CODE_SIGN_ENTITLEMENTS = AblyLocationPush/AblyLocationPush.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyLocationPush/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = AblyLocationPush;
@@ -554,7 +556,7 @@
CODE_SIGN_ENTITLEMENTS = AblyLocationPush/AblyLocationPush.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyLocationPush/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = AblyLocationPush;
@@ -585,7 +587,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"AblyPushExample/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyPushExample/InfoLP.plist;
@@ -621,7 +623,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"AblyPushExample/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XXY98AVDR6;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AblyPushExample/InfoLP.plist;
40 changes: 40 additions & 0 deletions Examples/AblyPush/AblyPushExample/AblyHelper.swift
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ class AblyHelper: NSObject {

var activatePushCallback: ((String?, String?, ARTErrorInfo?) -> ())?


private override init() {
super.init()
guard key != "" else {
@@ -87,6 +88,40 @@ extension AblyHelper {
print("Publish result: \(error?.localizedDescription ?? "Success")")
}
}

func sendPushToChannel(_ channel: Channel) {
let message = ARTMessage(name: "example", data: "rest data")
message.extras = [
"push": [
"notification": [
"title": "Channel Push",
"body": "Sent push to \(channel.rawValue)"
],
"data": [
"foo": "bar",
"baz": "qux"
]
]
] as any ARTJsonCompatible

realtime.channels.get(channel.rawValue).publish([message]) { error in
if let error {
print("Error sending push to \(channel.rawValue) with error: \(error.localizedDescription)")
} else {
print("Sent push to \(channel.rawValue)")
}
}
}

func subscribeToChannel(_ channel: Channel) {
realtime.channels.get(channel.rawValue).push.subscribeDevice { error in
if let error {
print("Error subscribing to \(channel.rawValue) with error: \(error.localizedDescription)")
} else {
print("Succesfully subscribed to \(channel.rawValue)")
}
}
}
}

extension AblyHelper: ARTPushRegistererDelegate {
@@ -141,3 +176,8 @@ extension AblyHelper : CLLocationManagerDelegate {
}
}
}

enum Channel: String {
case exampleChannel1
case exampleChannel2
}
45 changes: 44 additions & 1 deletion Examples/AblyPush/AblyPushExample/ContentView.swift
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ struct ContentView: View {
@State var defaultDeviceToken: String?
@State var locationDeviceToken: String?
@State var deviceTokensError: ARTErrorInfo?
@Binding var subscribedToExampleChannel1: Bool

var body: some View {
NavigationView {
@@ -42,6 +43,16 @@ struct ContentView: View {
AblyHelper.shared.deactivatePush()
}
.padding()
PushButton(title: "Remember Me", isOn: $subscribedToExampleChannel1)

Button("Subscribe to \(Channel.exampleChannel1.rawValue)") {
AblyHelper.shared.subscribeToChannel(.exampleChannel1)
}
.padding()
Button("Subscribe to \(Channel.exampleChannel2.rawValue)") {
AblyHelper.shared.subscribeToChannel(.exampleChannel2)
}
.padding()
Button("Print Token") {
AblyHelper.shared.printIdentityToken()
}
@@ -63,10 +74,18 @@ struct ContentView: View {
return Alert(title: Text("Device Details Error"), message: Text("Unknown result."))
}
.padding()
Button("Send Push") {
Button("Send Push to deviceId") {
AblyHelper.shared.sendAdminPush(title: "Hello", body: "This push was sent with deviceId")
}
.padding()
Button("Send Push to \(Channel.exampleChannel1.rawValue)") {
AblyHelper.shared.sendPushToChannel(.exampleChannel1)
}
.padding()
Button("Send Push to \(Channel.exampleChannel2.rawValue)") {
AblyHelper.shared.sendPushToChannel(.exampleChannel2)
}
.padding()
#if USE_LOCATION_PUSH
NavigationLink {
LocationPushEventsView()
@@ -87,3 +106,27 @@ struct ContentView_Previews: PreviewProvider {
ContentView()
}
}


struct PushButton: View {
let title: String
@Binding var isOn: Bool

var onColors = [Color.red, Color.yellow]
var offColors = [Color(white: 0.6), Color(white: 0.4)]

var body: some View {
Button(title) {
isOn.toggle()
}
.padding()
.background(LinearGradient(
colors: isOn ? onColors : offColors,
startPoint: .top, endPoint: .bottom
)
)
.foregroundStyle(.white)
.clipShape(.capsule)
.shadow(radius: isOn ? 0 : 5)
}
}
2 changes: 0 additions & 2 deletions Source/ARTPushActivationStateMachine.m
Original file line number Diff line number Diff line change
@@ -400,11 +400,9 @@ - (void)callUpdatedCallback:(nullable ARTErrorInfo *)error {
}

- (void)registerForAPNS {
#if !TARGET_OS_SIMULATOR
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
#endif
}

@end