---
source_url: https://www.pubnub.com/docs/sdks/objective-c/api-reference/misc
title: Utility Methods API for Objective-C SDK
updated_at: 2026-06-10T11:20:44.157Z
sdk_name: PubNub Objective-C SDK
sdk_version: 7.0.2
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Utility Methods API for Objective-C SDK

PubNub Objective-C SDK, use the latest version: 7.0.2

Install:

```bash
pod install PubNub@7.0.2
```

The methods on this page are utility methods that don't fit into other categories.

## Get size of message

This function provides a mechanism to calculate resulting message before it will be sent to the PubNub network.

### Method(s)

To run `Get size of message` you can use the following method(s) in the Objective-C SDK:

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
       withCompletion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| message | id | Yes |  | Message for which size should be calculated. |
| channel | NSString | Yes |  | Name of the `channel` to which `message` should be sent (it is part of request URI). |
| block | PNMessageSizeCalculationCompletionBlock | Yes |  | Referecnce on block which should be sent, when message size calculation will be completed. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
           compressed:(BOOL)compressMessage 
       withCompletion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | Message for which size should be calculated. |
| `channel` *Type: NSString | Name of the `channel` to which `message` should be sent (it is part of request URI). |
| `compressMessage` *Type: Bool | `YES` in case if `message` should be compressed before sending to `PubNub` network. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Referecnce on block which should be sent, when message size calculation will be completed. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
       storeInHistory:(BOOL)shouldStore 
       withCompletion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | Message for which size should be calculated. |
| `channel` *Type: NSString | Name of the `channel` to which `message` should be sent (it is part of request URI). |
| `shouldStore` *Type: Bool | `YES` in case if `message` should be stored in Message Persistence. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Referecnce on block which should be sent, when message size calculation will be completed. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
           compressed:(BOOL)compressMessage 
       storeInHistory:(BOOL)shouldStore 
       withCompletion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | Message for which size should be calculated. |
| `channel` *Type: NSString | Name of the `channel` to which `message` should be sent (it is part of request URI). |
| `compressMessage` *Type: Bool | `YES` in case if `message` should be compressed before sending to PubNub network. |
| `shouldStore` *Type: Bool | `NO` in case if `message` shouldn't be available after it has been sent via Message Persistence API methods `group`. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Referecnce on block which should be sent, when message size calculation will be completed. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
         withMetadata:(nullable NSDictionary<NSString *, id> *)metadata 
           completion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | The `message` for which the size needs be calculated. |
| `channel` *Type: NSString | The `channel` on which the `message` has to be sent (it is part of request URI). |
| `metadata`Type: NSDictionary | `NSDictionary` with values which should be used by `PubNub` service to filter messages. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Completion `block` which will be called when the `message` size calculation is complete. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
           compressed:(BOOL)compressMessage 
         withMetadata:(nullable NSDictionary<NSString *, id> *)metadata 
           completion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | The `message` for which the size needs be calculated. |
| `channel` *Type: NSString | The `channel` on which the `message` has to be sent (it is part of request URI). |
| `compressMessage` *Type: Bool | Should be `true` if the `message` is compressed before sending to `PubNub` network. |
| `metadata`Type: NSDictionary | `NSDictionary` with values which should be used by `PubNub` service to filter messages. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Completion `block` which will be called when the `message` size calculation is complete. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
       storeInHistory:(BOOL)shouldStore 
         withMetadata:(nullable NSDictionary<NSString *, id> *)metadata 
           completion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | The `message` for which the size needs be calculated. |
| `channel` *Type: NSString | The `channel` on which the `message` has to be sent (it is part of request URI). |
| `shouldStore` *Type: Bool | Should be `true` if the `message` is marked to be stored in history. |
| `metadata`Type: NSDictionary | `NSDictionary` with values which should be used by `PubNub` service to filter messages. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Completion `block` which will be called when the `message` size calculation is complete. |

```objectivec
- (void)sizeOfMessage:(id)message 
            toChannel:(NSString *)channel 
           compressed:(BOOL)compressMessage 
       storeInHistory:(BOOL)shouldStore 
         withMetadata:(nullable NSDictionary<NSString *, id> *)metadata 
           completion:(PNMessageSizeCalculationCompletionBlock)block;
```

| Parameter | Description |
| --- | --- |
| `message` *Type: id | The `message` for which the size needs be calculated. |
| `channel` *Type: NSString | The `channel` on which the `message` has to be sent (it is part of request URI). |
| `shouldStore` *Type: Bool | Should be `true` if the `message` is marked to be stored in history. |
| `compressMessage` *Type: Bool | Should be `true` if the `message` is compressed before sending to PubNub network. |
| `metadata`Type: NSDictionary | `NSDictionary` with values which should be used by `PubNub` service to filter messages. |
| `block` *Type: PNMessageSizeCalculationCompletionBlock | Completion `block` which will be called when the `message` size calculation is complete. |

### Sample code

#### Get message size

```objectivec
[self.client sizeOfMessage: @{@"Hello": @"world"} toChannel: @"announcement"
             withCompletion:^(NSInteger size) {

    // Process calculated target message size.
 }];
```

### Returns

The message size

### Other examples

#### Get size of message with metadata

```objectivec
[self.client sizeOfMessage: @{@"Hello": @"World"} toChannel: @"announcement"
              withMetadata: @{@"senderID": @"bob"} completion:^(NSInteger size) {

    // Process calculated target message size.
}];
```

## Push notification configuration

### PNAPNSNotificationConfiguration

`PNAPNSNotificationConfiguration` instance allow to configure how notification should be delivered using HTTP/2-based APNs.

#### Method(s)

```objectivec
+ (instancetype)defaultConfiguration
```

Create default configuration with single target configured against `PNAPNSDevelopment` environment `NSBundle.mainBundle.bundleIdentifier` as topic name.

```objectivec
+ (instancetype)configurationWithTargets:(NSArray<PNAPNSNotificationTarget *> *)targets
```

| Parameter | Description |
| --- | --- |
| `targets` *Type: `NSArray< [PNAPNSNotificationTarget](#pnapnsnotificationtarget)*> *` | List of topics which should receive this notification. Default target with `NSBundle.mainBundle.bundleIdentifier` topic and `PNAPNSDevelopment` environment will be used if list is empty. |

```objectivec
+ (instancetype)configurationWithCollapseID:(nullable NSString *)collapseId 
                             expirationDate:(nullable NSDate *)date 
                                    targets:(NSArray<PNAPNSNotificationTarget *> *)targets;
```

| Parameter | Description |
| --- | --- |
| `collapseId`Type: NSString | Notification group / collapse identifier. Value will be used in APNs POST request as `apns-collapse-id` header value. |
| `date`Type: NSDate | Date till which APNS will try to deliver notification to target device. Value will be used in APNs POST request as `apns-expiration` header value. |
| `targets` *Type: `NSArray< [PNAPNSNotificationTarget](#pnapnsnotificationtarget)*> *` | List of topics which should receive this notification. Default target with `NSBundle.mainBundle.bundleIdentifier` topic and `PNAPNSDevelopment` environment will be used if list is empty. |

#### Sample code

Create and configure configuration instance which will collapse invitation notifications and retry to deliver notification (APNS) for next 10 seconds if device off-line:

```objectivec
PNAPNSNotificationConfiguration *configuration = nil;
PNNotificationsPayload *builder = nil;

PNAPNSNotificationTarget *target = [PNAPNSNotificationTarget targetForTopic:@"com.meetings.chat.app"];
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:10];
configuration = [PNAPNSNotificationConfiguration configurationWithCollapseID:@"invitations"
                                                              expirationDate:expirationDate
                                                                     targets:@[target]];
```

#### Response

Configured and ready to use `PNAPNSNotificationConfiguration` instance.

### PNAPNSNotificationPayload

`PNAPNSNotificationPayload` instance provides access to options specific only to mobile push notifications sent with APNs.

#### Properties

| Parameter | Description |
| --- | --- |
| `configurations`Type: `NSArray< [PNAPNSNotificationConfiguration](#pnapnsnotificationconfiguration)*> *` | List of HTTP/2-based APNs delivery configurations. If list is empty when payload for `PNAPNS2Push` has been requested, it will create default configuration for `PNAPNSDevelopment` environment and `NSBundle.mainBundle.bundleIdentifier` as topic name. |
| `notification`Type: NSMutableDictionary | Object with parameters which specify user-visible key-value pairs. |
| `payload`Type: NSMutableDictionary | Platform specific notification payload. In addition to data required to make notification visual presentation it can be used to pass additional information which should be sent to remote device. |
| `silent`Type: BOOL | Whether operation system should handle notification layout by default or not. `alert`, `sound` and `badge` will be removed from resulting payload if set to `YES`. |

### PNAPNSNotificationTarget

`PNAPNSNotificationTarget` instance allow to configure APNS notification recipient.

#### Method(s)

```objectivec
+ (instancetype)defaultTarget
```

Create default target configured against `PNAPNSDevelopment` environment `NSBundle.mainBundle.bundleIdentifier` as topic name.

```objectivec
+ (instancetype)targetForTopic:(NSString *)topic
```

| Parameter | Description |
| --- | --- |
| `topic` *Type: NSString | Notifications topic name (usually it is application's bundle identifier). Value will be used in APNs POST request as `apns-topic` header value. |

```objectivec
+ (instancetype)targetForTopic:(NSString *)topic 
                 inEnvironment:(PNAPNSEnvironment)environment 
           withExcludedDevices:(nullable NSArray<NSData *> *)excludedDevices;
```

| Parameter | Description |
| --- | --- |
| `topic` *Type: NSString | Notifications topic name (usually it is application's bundle identifier). Value will be used in APNs POST request as `apns-topic` header value. |
| `environment` *Type: PNAPNSEnvironment | One of `PNAPNSEnvironment` fields which specify environment within which registered devices to which notifications should be delivered. `PNAPNSEnvironment` fields: PNAPNSDevelopment, PNAPNSProduction |
| `excludedDevices` *Type: NSArray`<NSDate *>` * | List of devices (their push tokens) to which this notification shouldn't be delivered. |

#### Sample code

Create and configure APNS notification target, which excludes current device from recipients list:

```objectivec
PNAPNSNotificationTarget *target = [PNAPNSNotificationTarget targetForTopic:@"com.meetings.chat.app"
                                                              inEnvironment:PNAPNSProduction
                                                        withExcludedDevices:@[self.currentDevicePushToken]];
```

#### Response

Configured and ready to use `PNAPNSNotificationTarget` instance.

### PNFCMNotificationPayload

`PNFCMNotificationPayload` instance provides access to options specific only to mobile push notifications sent with **FCM**.

#### Properties

| Parameter | Description |
| --- | --- |
| `notification`Type: NSMutableDictionary | Object with parameters which specify user-visible key-value pairs. |
| `data`Type: NSMutableDictionary | Custom key-value object with additional information which will be passed to device along with displayable notification information. All object and scalar type value should be converted to strings before passing to this object. Keys shouldn't match: `from`, `message_type` or start with `google` or `fcm`. Also as key can't be used any word defined in [this table](https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support) |
| `silent`Type: BOOL | Whether operation system should handle notification layout by default or not. `notification` key with it's content will be moved from root level under `data` key. |
| `icon`Type: NSString | Icon which should be shown on the left from notification title instead of application icon. |
| `tag`Type: NSString | Unique notification identifier which can be used to publish update notifications (they will previous notification with same `tag`). |
| `payload`Type: NSMutableDictionary | Platform specific notification payload. In addition to data required to make notification visual presentation it can be used to pass additional information which should be sent to remote device. |

### PNNotificationsPayload

`PNNotificationsPayload` instance provides convenient method and properties which allow to setup notification for multiple platforms without getting into details how they should be formatted. Builder instance contain additional set of properties which allow to fine tune payloads for particular platforms and even access to *RAW* payload dictionaries.

#### Method(s)

```objectivec
+ (instancetype)payloadsWithNotificationTitle:(nullable NSString *)title 
                                         body:(nullable NSString *)body;
```

| Parameter | Description |
| --- | --- |
| `title`Type: NSString | Short text which should be shown at the top of notification instead of application name. |
| `body`Type: NSString | Message which should be shown in notification body (under title line). |
| `subtitle`Type: NSString | Additional information which may explain reason why this notification has been delivered. |
| `badge`Type: NSNumber | Number which should be shown in space designated by platform (for example atop of application icon). |
| `sound`Type: NSString | Path to file with sound or name of system sound which should be played upon notification receive. |
| `apns` *Type: [PNAPNSNotificationPayload](#pnapnsnotificationpayload) | Access to APNS specific notification builder. |
| `fcm` *Type: [PNFCMNotificationPayload](#pnfcmnotificationpayload) | Access to FCM specific notification builder. |

#### Sample code

Create notification payload builder with pre-defined notification title and body:

```objectivec
PNNotificationsPayload *builder = nil;
builder = [PNNotificationsPayload payloadsWithNotificationTitle:@"Chat invitation"
                                                           body:@"You have been invited to 'quiz' chat"];
```

#### Response

Configured and ready to use `PNNotificationsPayload` instance.

#### Other examples

#### Generate simple notification payload for FCM and APNS

```objectivec
PNNotificationsPayload *builder = nil;
builder = [PNNotificationsPayload payloadsWithNotificationTitle:@"Chat invitation"
                                                            body:@"You have been invited to 'quiz' chat"];
builder.sound = @"default";

NSLog(@"Notifications payload: %@", [builder dictionaryRepresentationFor:PNAPNSPush|PNFCMPush]);
```

##### Output

```objectivec
{
    "pn_apns": {
        "aps": {
            "alert": {
                "body": "You have been invited to 'quiz' chat",
                "title": "Chat invitation"
            },
            "sound": "default"
        }
    },
    "pn_fcm": {
        "notification": {
            "body": "You have been invited to 'quiz' chat",
            "title": "Chat invitation"
        },
        "android": {
            "notification": {
                "sound": "default"
            }
        }
    }
}
```

#### Generate simple notification payload for FCM and HTTP/2-based APNs (default configuration)

```objectivec
PNNotificationsPayload *builder = nil;
builder = [PNNotificationsPayload payloadsWithNotificationTitle:@"Chat invitation"
                                                            body:@"You have been invited to 'quiz' chat"];
builder.sound = @"default";

NSLog(@"Notifications payload: %@", [builder dictionaryRepresentationFor:PNAPNS2Push|PNFCMPush]);
```

##### Output

```objectivec
{
    "pn_apns": {
        "aps": {
            "alert": {
                "body": "You have been invited to 'quiz' chat",
                "title": "Chat invitation"
            },
            "sound": "default"
        },
        "pn_push": [
            {
                "targets": [
                    {
                        "environment": "development",
                        "topic": "com.meetings.chat.app"
                    }
                ],
                "version": "v2"
            }
        ]
    },
    "pn_fcm": {
        "notification": {
            "body": "You have been invited to 'quiz' chat",
            "title": "Chat invitation"
        },
        "android": {
            "notification": {
                "sound": "default"
            }
        }
    }
}
```

#### Generate simple notification payload for FCM and HTTP/2-based APNs (custom configuration)

```objectivec
PNAPNSNotificationConfiguration *configuration = nil;
PNNotificationsPayload *builder = nil;

PNAPNSNotificationTarget *target = [PNAPNSNotificationTarget targetForTopic:@"com.meetings.chat.app"];
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:10];
configuration = [PNAPNSNotificationConfiguration configurationWithCollapseID:@"invitations"
                                                                expirationDate:expirationDate
                                                                        targets:@[target]];

builder = [PNNotificationsPayload payloadsWithNotificationTitle:@"Chat invitation"
                                                            body:@"You have been invited to 'quiz' chat"];
builder.apns.configurations = @[configuration];

NSLog(@"Notifications payload: %@", [builder dictionaryRepresentationFor:PNAPNS2Push|PNFCMPush]);
```

##### Output

```objectivec
{
    "pn_apns": {
        "aps": {
            "alert": {
                "body": "Chat invitation",
                "title": "You have been invited to 'quiz' chat"
            }
        },
        "pn_push": [
            {
                "collapse_id": "invitations",
                "expiration": "2019-11-28T22:06:09Z",
                "targets": [
                    {
                        "environment": "development",
                        "topic": "com.meetings.chat.app"
                    }
                ],
                "version": "v2"
            }
        ]
    },
    "pn_fcm": {
        "notification": {
            "body": "You have been invited to 'quiz' chat",
            "title": "Chat invitation"
        }
    }
}
```

Example above show how to create notification payload which APNS will try to redeliver few times (if devices not active) and give up after **10** seconds since moment when it has been scheduled.

Additionally this invitation notification will be grouped along with other invitation notifications (using provided `collapse_id` as group identifier) and shown as one in notification center.

```objectivec
- (NSDictionary *)dictionaryRepresentationFor:(PNPushType)pushTypes
```

Build notifications platform for requested platforms (`pushTypes`).

| Parameter | Description |
| --- | --- |
| `pushTypes` *Type: PNPushType | Bitfield with fields from `PNPushType` which specify platforms for which payload should be added to final dictionary. `PNPushType` fields: PNAPNSPush, PNAPNS2Push, PNFCMPush |

#### Sample code

Publish message with notification payload:

```objectivec
PNNotificationsPayload *builder = nil;
builder = [PNNotificationsPayload payloadsWithNotificationTitle:@"Chat invitation"
                                                           body:@"You have been invited to 'quiz' chat"];
NSDictionary *payload = [builder dictionaryRepresentationFor:PNAPNS2Push|PNFCMPush];
NSDictionary *message = @{
    @"message": @"Max invited you to 'quiz' chat room",
    @"roomID": @"ewuiogw9vewg0"
};

[self.client publish:message toChannel:@"chat-bot" mobilePushPayload:payload
      withCompletion:^(PNPublishStatus *status) {
   // Handle publish results
}];
```

#### Response

Dictionary with data, which can be sent with publish method call and trigger remote notifications for specified platforms.