PushNotifications
public struct PushNotifications : JWTTokenGenerable
PushNotifications struct implements publish method that is used to publish push notifications to specified interests.
Precondition
instanceId should not be an empty string.
Precondition
secretKey should not be an empty string.
-
Creates a new
PushNotificationsinstance.Declaration
Swift
public init(instanceId: String, secretKey: String)Parameters
instanceIdPusher Beams Instance Id.
secretKeyPusher Beams Secret Key.
-
Publish the given
publishRequestto the specified interests.// Pusher Beams Instance Id. let instanceId = "1b880590-6301-4bb5-b34f-45db1c5f5644" // Pusher Beams Secret Key. let secretKey = "F8AC0B756E50DF235F642D6F0DC2CDE0328CD9184B3874C5E91AB2189BB722FE" // PushNotifications instance. let pushNotifications = PushNotifications(instanceId: instanceId, secretKey: secretKey) let interests = ["pizza", "donuts"] // Publish request: APNs, FCM. let publishRequest = [ "apns": [ "aps": [ "alert": "Hello" ] ], "fcm": [ "notification": [ "title": "Hello", "body": "Hello, world", ] ] ] pushNotifications.publishToInterests(interests, publishRequest) { result in switch result { case .success(let publishId): print("Publish id: \(publishId)") case .failure(let error): print("Error: \(error)") } }Declaration
Swift
public func publishToInterests(_ interests: [String], _ publishRequest: [String: Any], completion: @escaping (Result<String, PushNotificationsError>) -> Void)Parameters
interestsArray of strings that contains interests.
publishRequestDictionary containing the body of the push notification publish request.
completionThe block to execute when the
publishToInterestsoperation is complete.Return Value
A non-empty device id string if successful; or a non-nil
PushNotificationsErrorerror otherwise. Example usage: -
Publish the given
publishRequestto the specified users.// Pusher Beams Instance Id. let instanceId = "1b880590-6301-4bb5-b34f-45db1c5f5644" // Pusher Beams Secret Key. let secretKey = "F8AC0B756E50DF235F642D6F0DC2CDE0328CD9184B3874C5E91AB2189BB722FE" // PushNotifications instance. let pushNotifications = PushNotifications(instanceId: instanceId, secretKey: secretKey) let users = ["user1", "user2", "user3"] // Publish request: APNs, FCM. let publishRequest = [ "apns": [ "aps": [ "alert": "Hello" ] ], "fcm": [ "notification": [ "title": "Hello", "body": "Hello, world", ] ] ] pushNotifications.publishToUsers(users, publishRequest) { result in switch result { case .success(let publishId): print("Publish id: \(publishId)") case .failure(let error): print("Error: \(error)") } }Declaration
Swift
public func publishToUsers(_ users: [String], _ publishRequest: [String: Any], completion: @escaping (Result<String, PushNotificationsError>) -> Void)Parameters
usersArray of strings that contains user ids.
publishRequestDictionary containing the body of the push notification publish request.
completionThe block to execute when the
publishToUsersoperation is complete.Return Value
A non-empty publish id string if successful; or a non-nil
PushNotificationsErrorerror otherwise. Example usage: -
Creates a signed JWT for a user id.
// Pusher Beams Instance Id. let instanceId = "1b880590-6301-4bb5-b34f-45db1c5f5644" // Pusher Beams Secret Key. let secretKey = "F8AC0B756E50DF235F642D6F0DC2CDE0328CD9184B3874C5E91AB2189BB722FE" // PushNotifications instance. let pushNotifications = PushNotifications(instanceId: instanceId, secretKey: secretKey) pushNotifications.generateToken("Al Pacino", completion: { result in switch result { case .success(let jwtToken): print("\(jwtToken)") case .failure(let error): print("\(error)") } })Declaration
Swift
public func generateToken(_ userId: String, completion: @escaping (Result<[String: String], PushNotificationsError>) -> Void)Parameters
userIdId of a user for which we want to generate the JWT token.
completionThe block to execute when the
generateTokenoperation is complete.Return Value
A signed JWT if successful, or a non-nil
PushNotificationsErrorerror otherwise. Example usage: -
Contacts the Beams service to remove all the devices of the given user.
// Pusher Beams Instance Id. let instanceId = "1b880590-6301-4bb5-b34f-45db1c5f5644" // Pusher Beams Secret Key. let secretKey = "F8AC0B756E50DF235F642D6F0DC2CDE0328CD9184B3874C5E91AB2189BB722FE" // PushNotifications instance. let pushNotifications = PushNotifications(instanceId: instanceId, secretKey: secretKey) pushNotifications.deleteUser("Al Pacino", completion: { result in switch result { case .success: print("User deleted 👌") case .failure(let error): print("\(error)") } })Declaration
Swift
public func deleteUser(_ userId: String, completion: @escaping (Result<Void, PushNotificationsError>) -> Void)Parameters
userIdId of a user for which we want to remove all the devices.
completionThe block to execute when the
deleteUseroperation is complete.Return Value
Void if successful, or a non-nil
PushNotificationsErrorerror otherwise. Example usage:
View on GitHub
PushNotifications Structure Reference