Utility Methods API for AngularJS 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
1Pubnub.stop();
Decrypt
This function allows to decrypt the data.
Method(s)
To decrypt the data you can use the following method(s) in AngularJS SDK.
-
1decrypt(data: string, customCipherKey: ?string)* requiredParameter Description data*Type: StringThe datatodecrypt.customCipherKeyType: StringIf it's not provided, the cipher keyfrom config will be used.
Sample code
1var decrypted = Pubnub.decrypt(encrypted, 'myCipherKey');
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 AngularJS SDK.
-
1encrypt(data: string, customCipherKey: ?string)* requiredParameter Description data*Type: StringThe datatoencrypt.customCipherKeyType: StringIf it's not provided, the cipher keyfrom config will be used.
Sample code
1var sCypher = "testCypher";
2var msgContent = "This is the data I wish to encrypt.";
3console.log('msgContent: ' + msgContent);
4
5// Encrypt with pubnub
6var encryptedMessage = Pubnub.encrypt(JSON.stringify(msgContent), sCypher);
7console.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 AngularJS SDK.
-
1reconnect()This method doesn't take any arguments.
Sample code
1Pubnub.reconnect();
Time
This function will return a 17 digit precision Unix epoch.
The timetoken is constructed using the following algorithm:
1timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time and date
108/19/2013 @ 9:20pm in UTC = 1376961606
2timetoken = 1376961606 * 10000000
3timetoken = 13769616060000000
Method(s)
To fetch Time you can use the following method(s) in AngularJS SDK:
-
1time(Function callback)* requiredParameter Description callback*Type: FunctionCallbackis called after a successful return.
Sample code
1// At the tone Pubnub time will be...
2Pubnub.time(function(status, response) {
3 if (status.error) {
4 // handle error if something went wrong based on the status object
5 } else {
6 console.log(response.timetoken);
7 }
8});
Response
1//Example of status
2{
3 error: false,
4 operation: 'PNTimeOperation',
5 statusCode: 200
6}
7
8//Example of response
9{
10 timetoken: 15031768233407550
11}
Push notification configuration
APNS2Configuration
APNS2 configuration type.
Method(s)
-
1type APNS2Configuration = { collapseId?: string, expirationDate?: Date, targets: Array<APNS2Target>}* requiredParameter Description collapseIdType: StringNotification group / collapse identifier. Value will be used in APNs POST request as apns-collapse-idheader value.expirationDateType: DateDate till which APNs will try to deliver notification to target device. Value will be used in APNs POST request as apns-expirationheader value.targets*Type: Array< 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 configurationsType: Array< APNS2NotificationConfiguration *> *List of HTTP/2-based APNs delivery configurations. notificationType: HashHash with parameters which specify user-visible key-value pairs. payloadType: HashPlatform 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. silentType: BooleanWhether operation system should handle notification layout by default or not. alert,soundandbadgewill be removed from resulting payload if set totrue.
APNS2Target
APNS2 configuration target type.
Method(s)
-
1type APNS2Target = { topic: string, environment?: 'development' | 'production', excludedDevices?: Array<string>}* requiredParameter Description topic*Type: StringDefault:n/aNotifications topic name (usually it is bundle identifier of applicationfor Apple platform). Required only if pushGatewayset toapns2.environmentType: StringDefault:developmentEnvironment within which registered devices to which notifications should be delivered. Available: developmentproduction
excludedDevices*Type: ArrayDefault:n/aList 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 notificationType: HashHash with parameters which specify user-visible key-value pairs. dataType: HashCustom 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_typeor start withgoogleorgcm. Also as key can't be used any word defined in this tablesilentType: BooleanWhether operation system should handle notification layout by default or not. notificationkey with it's content will be moved from root level underdatakey.iconType: StringIcon which should be shown on the left from notification title instead of application icon. tagType: StringUnique notification identifier which can be used to publish update notifications (they will previous notification with same tag).payloadType: HashPlatform 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 subtitleType: StringAdditional information which may explain reason why this notification has been delivered. badgeType: NumberNumber which should be shown in space designated by platform (for example atop of application icon). soundType: StringPath to file with sound or name of system sound which should be played upon notification receive. debuggingType: BooleanWhether PubNub service should provide debug information about devices which received created notifications payload. apnsType: APNSNotificationPayloadAccess to APNs specific notification builder. fcmType: FCMNotificationPayloadAccess to FCM specific notification builder. -
1PubNub.notificationPayload(String title, String body)* requiredParameter Description titleType: StringShort text which should be shown at the top of notification instead of application name. bodyType: StringMessage which should be shown in notification body (under title line). -
1buildPayload(Array<String> platforms)* requiredParameter Description platforms*Type: ArrayList of platforms for which payload should be added to final dictionary. Available: apnsapns2fcm
Sample code
Create notification payload builder with pre-defined notification title and body:
-
1let builder = PubNub.notificationPayload('Chat invitation',
2 'You have been invited to \'quiz\' chat');
3let messagePayload = builder.buildPayload(['apns2', 'fcm']);
4messagePayload.message = 'Max invited you to \'quiz\' chat room';
5messagePayload.roomID = 'ewuiogw9vewg0';
6
7pubnub.publish(
8 {
9 message: messagePayload,
10 channel: 'chat-bot',
11 },
12 function (status, response) {
13 // Handle publish results
14 }
15); -
1let builder = PubNub.notificationPayload('Chat invitation',
2 '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:
1let builder = PubNub.notificationPayload('Chat invitation',
2 'You have been invited to \'quiz\' chat');
3builder.sound = 'default';
4
5console.log(JSON.stringify(builder.buildPayload(['apns', 'fcm']), null, 2));Output
show all 22 lines{
"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"
}, -
Generate simple notification payload for FCM and HTTP/2-based APNs (default configuration):
1let builder = PubNub.notificationPayload('Chat invitation',
2 'You have been invited to \'quiz\' chat');
3builder.apns.configurations = [{ targets: [{ topic: 'com.meetings.chat.app' }] }];
4builder.sound = 'default';
5
6console.log(JSON.stringify(builder.buildPayload(['apns2', 'fcm']), null, 2));Output
show all 33 lines{
"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" -
Generate simple notification payload for FCM and HTTP/2-based APNs (custom configuration):
1let configuration = [
2 {
3 collapseId: 'invitations',
4 expirationDate: new Date(Date.now() + 10000),
5 targets: [{ topic: 'com.meetings.chat.app' }]
6 }
7];
8let builder = PubNub.notificationPayload('Chat invitation',
9 'You have been invited to \'quiz\' chat');
10builder.apns.configurations = [configuration];
11
12console.log(JSON.stringify(builder.buildPayload(['apns2', 'fcm']), null, 2));Output
show all 29 lines{
"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",
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 instance.