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 PushNotifications instance.

    Declaration

    Swift

    public init(instanceId: String, secretKey: String)

    Parameters

    instanceId

    Pusher Beams Instance Id.

    secretKey

    Pusher Beams Secret Key.

  • Publish the given publishRequest to 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

    interests

    Array of strings that contains interests.

    publishRequest

    Dictionary containing the body of the push notification publish request.

    completion

    The block to execute when the publishToInterests operation is complete.

    Return Value

    A non-empty device id string if successful; or a non-nil PushNotificationsError error otherwise. Example usage:

  • Publish the given publishRequest to 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

    users

    Array of strings that contains user ids.

    publishRequest

    Dictionary containing the body of the push notification publish request.

    completion

    The block to execute when the publishToUsers operation is complete.

    Return Value

    A non-empty publish id string if successful; or a non-nil PushNotificationsError error 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

    userId

    Id of a user for which we want to generate the JWT token.

    completion

    The block to execute when the generateToken operation is complete.

    Return Value

    A signed JWT if successful, or a non-nil PushNotificationsError error 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

    userId

    Id of a user for which we want to remove all the devices.

    completion

    The block to execute when the deleteUser operation is complete.

    Return Value

    Void if successful, or a non-nil PushNotificationsError error otherwise. Example usage: