---
source_url: https://www.pubnub.com/docs/sdks/angular2/api-reference/misc
title: Utility Methods API for Angular2 SDK
updated_at: 2026-06-05T11:11:19.273Z
---

> 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 Angular2 SDK

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

## Close PubNub

End all open requests and `close` the PubNub instance.

### Sample code

#### Close pubnub

```javascript
pubnub.stop();
```

## Decrypt

This function allows to `decrypt` the data.

### Method(s)

To `decrypt` the data you can use the following method(s) in Angular2 SDK.

```javascript
decrypt(data: string, customCipherKey: ?string)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| data | String | Yes |  | The `data` to `decrypt`. |
| customCipherKey | String | Optional |  | If it's not provided, the `cipher key` from config will be used. |

### Sample code

```javascript
var decrypted = pubnub.decrypt(encrypted, 'myCipherKey'); // Pass the encrypted data as the first parameter in decrypt Method
```

### Returns

It returns the decrypted `data` as an object.

## Encrypt

This function allows to `encrypt` the data.

### Method(s)

To `encrypt` the data you can use the following method(s) in Angular2 SDK.

```javascript
encrypt(data: string, customCipherKey: ?string)
```

| Parameter | Description |
| --- | --- |
| `data` *Type: String | The `data` to `encrypt`. |
| `customCipherKey`Type: String | If it's not provided, the `cipher key` from config will be used. |

### Sample code

```javascript
var sCypher = "testCypher";
var msgContent = "This is the data I wish to encrypt.";
console.log('msgContent: ' + msgContent);

// Encrypt with pubnub
var encryptedMessage = pubnub.encrypt(JSON.stringify(msgContent), sCypher);
console.log('encryptedMessage PN: ' + encryptedMessage);
```

### Returns

It returns the encrypted `data` as string.

## Reconnect

Call the `reconnect` method to force the SDK to try and reach out PubNub.

### Method(s)

To `reconnect` the data you can use the following method(s) in Angular2 SDK.

```javascript
reconnect()
```

This method doesn't take any arguments.

### Sample code

```javascript
pubnub.reconnect();
```

## Time

This function will return a 17 digit precision Unix epoch.

:::note The timetoken is constructed using the following algorithm:
```javascript
timetoken = (Unix epoch time in seconds) * 10000000
```
#### Example of creating a timetoken for a specific time and date​
```javascript
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
```
:::

### Method(s)

To fetch `Time` you can use the following method(s) in Angular2 SDK:

```javascript
time(Function callback)
```

| Parameter | Description |
| --- | --- |
| `callback` *Type: Function | `Callback` is called after a successful return. |

### Sample code

```javascript
pubnub.time(function(status, response) {
    if (status.error) {
        console.log(status.error);
    } else {
        console.log(response.timetoken);
    }
});
```

### Response

```javascript
//Example of status
{
    error: false,
    operation: 'PNTimeOperation',
    statusCode: 200
}

//Example of response
{
    timetoken: 15031768233407550
}
```

### Other examples

#### Sample code using promises

```javascript
pubnub.time().then((timetoken) => {
    console.log(timetoken);
}).catch((error) => {
    console.log(error)
});
```

## Push notification configuration

### APNS2Configuration

`APNS2` configuration type.

#### Method(s)

```javascript
type APNS2Configuration = {  collapseId?: string,  expirationDate?: Date,  targets: Array<APNS2Target>}
```

| Parameter | Description |
| --- | --- |
| `collapseId`Type: String | Notification group / collapse identifier. Value will be used in APNS POST request as `apns-collapse-id` header value. |
| `expirationDate`Type: Date | 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: Array< [APNS2Target](#apns2target) > | List of topics which should receive this notification. |

### APNSNotificationPayload

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

#### Properties

| Parameter | Description |
| --- | --- |
| `configurations`Type: Array< [APNS2NotificationConfiguration](#apns2configuration) *> * | List of HTTP/2-based APNs delivery configurations. |
| `notification`Type: Hash | Hash with parameters which specify user-visible key-value pairs. |
| `payload`Type: Hash | 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: Boolean | Whether operation system should handle notification layout by default or not. `alert`, `sound` and `badge` will be removed from resulting payload if set to `true`. |

### APNS2Target

`APNS2` configuration target type.

#### Method(s)

```javascript
type APNS2Target = {  topic: string,  environment?: 'development' | 'production',  excludedDevices?: Array<string>}
```

| Parameter | Description |
| --- | --- |
| `topic` *Type: StringDefault: n/a | Notifications topic name (usually it is bundle identifier of applicationfor Apple platform). *Required only if pushGateway set to apns2.* |
| `environment`Type: StringDefault: `development` | Environment within which registered devices to which notifications should be delivered. Available: development, production |
| `excludedDevices` *Type: ArrayDefault: n/a | List of devices (their push tokens) to which this notification shouldn't be delivered. |

### FCMNotificationPayload

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

#### Properties

| Parameter | Description |
| --- | --- |
| `notification`Type: Hash | Hash with parameters which specify user-visible key-value pairs. |
| `data`Type: Hash | 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 `gcm`. 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: Boolean | 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: String | Icon which should be shown on the left from notification title instead of application icon. |
| `tag`Type: String | Unique notification identifier which can be used to publish update notifications (they will previous notification with same `tag`). |
| `payload`Type: Hash | 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. |

### NotificationsPayload

`NotificationsPayload` 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)

| Parameter | Description |
| --- | --- |
| `subtitle`Type: String | Additional information which may explain reason why this notification has been delivered. |
| `badge`Type: Number | Number which should be shown in space designated by platform (for example atop of application icon). |
| `sound`Type: String | Path to file with sound or name of system sound which should be played upon notification receive. |
| `debugging`Type: Boolean | Whether PubNub service should provide debug information about devices which received created notifications payload. |
| `apns`Type: [APNSNotificationPayload](#apnsnotificationpayload) | Access to APNS specific notification builder. |
| `fcm`Type: [FCMNotificationPayload](#fcmnotificationpayload) | Access to FCM specific notification builder. |

```javascript
PubNub.notificationPayload(String title, String body)
```

| Parameter | Description |
| --- | --- |
| `title`Type: String | Short text which should be shown at the top of notification instead of application name. |
| `body`Type: String | Message which should be shown in notification body (under title line). |

```javascript
buildPayload(Array<String> platforms)
```

| Parameter | Description |
| --- | --- |
| `platforms` *Type: Array | List of platforms for which payload should be added to final dictionary. Available: apns, apns2, fcm |

#### Sample code

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

```javascript
let builder = PubNub.notificationPayload('Chat invitation',
                                            'You have been invited to \'quiz\' chat');
let messagePayload = builder.buildPayload(['apns2', 'fcm']);
messagePayload.message = 'Max invited you to \'quiz\' chat room';
messagePayload.roomID = 'ewuiogw9vewg0';

pubnub.publish(
    {
        message: messagePayload,
        channel: 'chat-bot',
    },
    function (status, response) {
        // Handle publish results
    }
);
```

```javascript
let builder = PubNub.notificationPayload('Chat invitation',
                                            'You have been invited to \'quiz\' chat');
```

#### Response

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

#### Other examples

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

```javascript
let builder = PubNub.notificationPayload('Chat invitation',
                                            'You have been invited to \'quiz\' chat');
builder.sound = 'default';

console.log(JSON.stringify(builder.buildPayload(['apns', 'fcm']), null, 2));
```

**Output**

```json
{
    "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)

```javascript
let builder = PubNub.notificationPayload('Chat invitation',
                                            'You have been invited to \'quiz\' chat');
builder.apns.configurations = [{ targets: [{ topic: 'com.meetings.chat.app' }] }];
builder.sound = 'default';

console.log(JSON.stringify(builder.buildPayload(['apns2', 'fcm']), null, 2));
```

##### Output

```json
{
    "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)

```javascript
let configuration = [
    {
        collapseId: 'invitations',
        expirationDate: new Date(Date.now() + 10000),
        targets: [{ topic: 'com.meetings.chat.app' }]
    }
];
let builder = PubNub.notificationPayload('Chat invitation',
                                            'You have been invited to \'quiz\' chat');
builder.apns.configurations = [configuration];

console.log(JSON.stringify(builder.buildPayload(['apns2', 'fcm']), null, 2));
```

**Output**

```json
{
    "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:09.163Z",
                "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.

#### Returns

Configured and ready to use [NotificationsPayload](#notificationspayload) instance.