NMADeviceInformationChange

@objc
public protocol NMADeviceInformationChange : NMAHttpOperation

The object that updates a device information. The device information change can be used

  • to modify the name of the device and/or
  • to change the Firebase registration token or to disable push notifications.

    Warning

    If neither name(_:) or fcmRegistrationToken(_:) are provided, the provided onSuccess(_:) object will be called when NMADeviceInformationChange.execute() is invoked.

Usage example:

id<NMADeviceInformationChange> deviceInformationChange = [[client operations] deviceInformationChange];
[deviceInformationChange name:newName];
[deviceInformationChange fcmRegistrationToken:fcmToken];
[deviceInformationChange onSuccess:^{...}];
[deviceInformationChange onError:^(NMADeviceInformationChangeError * _Nonnull error) {...}];
[deviceInformationChange execute];

[deviceInformationChange name:newName];
[deviceInformationChange disablePushNotifications];
[deviceInformationChange onSuccess:^{...}];
[deviceInformationChange onError:^(NMADeviceInformationChangeError * _Nonnull error) {...}];
[deviceInformationChange execute];

Synchronization with retry strategy

The retry strategy can be useful when updating the Firebase Cloud Messaging token. But since it is not known when the token will be updated, synchronization is necessary and should be done by the caller. Below a sample code is provided that solves the synchronization using dispatch queues and groups.

@import Firebase;
@import FirebaseMessaging;

@interface SampleAppMessagingService : NSObject<FIRMessagingDelegate>

@property(strong, nonatomic, nonnull) dispatch_queue_t queue;

@end

@implementation SampleAppMessagingService

- (instancetype)init {
    self = [super init];
    self.queue = dispatch_queue_create("com.sample.fcm.update.serial", DISPATCH_QUEUE_SERIAL);
    return self;
}

- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveRegistrationToken:(nullable NSString *)fcmToken {
    if (fcmToken != nil) {
        [self process:fcmToken client:self.client];
    }
}

- (void)process:(nonnull NSString *)token client:(nonnull id<NMAMobileAuthenticationClient>)client {
    dispatch_async(self.queue, ^{
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            id<NMADeviceInformationChange> deviceInformationChange = [[client operations] deviceInformationChange];
            [deviceInformationChange fcmRegistrationToken:token];

            NMAConstantRetryPolicy* policy = [[NMAConstantRetryPolicy alloc] initWithMaxRetries:3 delayInSeconds:30.0];
            [deviceInformationChange retryPolicy:policy];
            [deviceInformationChange onSuccess:^{
                // Firebase registration token successfully updated
                dispatch_group_leave(group);
            }];
            [deviceInformationChange onError:^(NMADeviceInformationChangeError * _Nonnull error) {
                // handle unsuccessful update
                dispatch_group_leave(group);
            }];
            [deviceInformationChange execute];
        });
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    });
}
  • Specifies the new name of the device information. This is typically a user-friendly string describing the mobile device where the application is running.

    Warning

    Do not invoke this method if the name does not need to be updated.

    Declaration

    Swift

    @discardableResult
    func name(_ name: String) -> NMADeviceInformationChange

    Parameters

    name

    the new device information name.

    Return Value

    the NMADeviceInformationChange builder.

  • Specifies the new Firebase Cloud Messaging registration token.

    Warning

    Do not invoke this method if the Firebase Cloud Messaging registration token does not need to be updated.

    Declaration

    Swift

    @discardableResult
    func fcmRegistrationToken(_ fcmRegistrationToken: String) -> NMADeviceInformationChange

    Parameters

    fcmRegistrationToken

    the new Firebase Cloud Messaging registration token

    Return Value

    the NMADeviceInformationChange builder.

  • Disables the push notifications on the server side (i.e. the server will not send authentication push notifications).

    To re-enable the sending of push notifications, execute a NMADeviceInformationChange and provide the Firebase Cloud Messaging registration token through the method fcmRegistrationToken(_:).

    Declaration

    Swift

    @discardableResult
    func disablePushNotifications() -> NMADeviceInformationChange

    Return Value

    the NMADeviceInformationChange builder.

  • Retries the operation in case of network failure using the provided retry policy.

    Declaration

    Swift

    @discardableResult
    func retryPolicy(_ retryPolicy: NMARetryPolicy) -> NMADeviceInformationChange

    Parameters

    retryPolicy

    the object defining the retry policy.

    Return Value

    the NMADeviceInformationChange builder.

  • Specifies the block to execute if the device information for the user was updated successfully.

    Important

    Providing the onSuccess block is required.

    Declaration

    Swift

    @discardableResult
    func onSuccess(_ onSuccess: @escaping () -> ()) -> NMADeviceInformationChange

    Parameters

    onSuccess

    the block to execute on successful update.

    Return Value

    the NMADeviceInformationChange builder.

  • Specifies the block to execute if the device information update failed.

    Important

    Providing the onError block is required.

    Declaration

    Swift

    @discardableResult
    func onError(_ onError: @escaping (NMADeviceInformationChangeError) -> ()) -> NMADeviceInformationChange

    Parameters

    onError

    the block to execute on failed device information update, receives an NMADeviceInformationChangeError.

    Return Value

    the NMADeviceInformationChange builder.