Add typings for Infobip Mobile Messaging Cordova SDK (#43038)

* Add typings for Infobip Mobile Messaging Cordova SDK

* Remove unused import

Co-authored-by: Konstantin Latypov <Konstantin.Latypov@infobip.com>
This commit is contained in:
Konstantin 2020-03-27 19:43:18 +03:00 committed by GitHub
parent 04e95f9b8a
commit cc362a05d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 485 additions and 0 deletions

View File

@ -0,0 +1,293 @@
// Type definitions for non-npm package mobile-messaging-cordova-plugin 1.2
// Project: https://github.com/infobip/mobile-messaging-cordova-plugin
// Definitions by: kostap13 <https://github.com/kostap13>,
// tjuric <https://github.com/tjuric>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
declare namespace MobileMessagingCordova {
type OS = 'Android' | 'iOS';
type Gender = 'Male' | 'Female';
type Event = 'messageReceived' |
'notificationTapped' |
'tokenReceived' |
'registrationUpdated' |
'geofenceEntered' |
'actionTapped' |
'installationUpdated' |
'userUpdated' |
'personalized' |
'depersonalized';
interface Configuration {
/**
* The application code of your Application from Push Portal website
*/
applicationCode: string;
geofencingEnabled?: boolean;
/**
* Message storage save callback
*/
messageStorage?: string;
defaultMessageStorage?: boolean;
ios?: {
notificationTypes?: string[];
forceCleanup?: boolean;
logging?: boolean
};
android?: {
notificationIcon: string; // a resource name for a status bar icon (without extension), located in '/platforms/android/app/src/main/res/mipmap'
multipleNotifications: boolean;
notificationAccentColor: string;
};
privacySettings?: {
applicationCodePersistingDisabled?: boolean;
userDataPersistingDisabled?: boolean;
carrierInfoSendingDisabled?: boolean;
systemInfoSendingDisabled?: boolean
};
notificationCategories?: [
{
identifier?: string;
actions?: [
{
identifier?: string;
title?: string;
foreground?: boolean;
authenticationRequired?: boolean;
moRequired?: boolean;
destructive?: boolean;
icon?: string;
textInputActionButtonTitle?: string;
textInputPlaceholder?: string
}]
}];
}
interface UserData {
externalUserId: string;
firstName?: string;
lastName?: string;
middleName?: string;
gender?: Gender;
birthday?: Date;
phones?: string[];
emails?: string[];
tags?: string[];
customAttributes?: Record<string, string>;
}
interface Installation {
isPrimaryDevice?: boolean;
isPushRegistrationEnabled?: boolean;
notificationsEnabled?: boolean;
geoEnabled?: boolean;
sdkVersion?: string;
appVersion?: string;
os?: OS;
osVersion: string;
deviceManufacturer?: string;
deviceModel?: string;
deviceSecure?: boolean;
language?: string;
deviceTimezoneId?: string;
applicationUserId?: string;
deviceName?: string;
customAttributes?: Record<string, string>;
}
interface UserIdentity {
phones?: string[];
emails?: string[];
externalUserId: string;
}
interface PersonalizeContext {
userIdentity: UserIdentity;
userAttributes?: Record<string, string>;
forceDepersonalize?: boolean;
}
interface Message {
messageId: string;
title?: string;
body?: string;
sound?: string;
vibrate?: string;
silent?: string;
category?: string;
customPayload?: Record<string, string>;
internalData?: string;
}
interface MobileMessagingError {
code: string;
message: string;
}
interface DefaultMessageStorage {
find(messageId: string, callback: (message: Message) => void): void;
findAll(callback: (messages: Message[]) => void): void;
delete(messageId: string, callback: () => void): void;
deleteAll(callback: () => void): void;
}
interface Api {
/**
* Starts a new Mobile Messaging session.
*
* @param config. Configuration for Mobile Messaging
* @param onInitError. Error callback
*/
init(config: Configuration, onInitError?: (error: MobileMessagingError) => void): void;
/**
* Register to event coming from MobileMessaging library.
* The following events are supported:
*
* @param event
* @param handler will be called when event occurs
*/
register(event: Event, handler: (message: Message) => void): void;
on(event: Event, handler: (message: Message) => void): void;
/**
* Un register from MobileMessaging library event.
*
* @param event
* @param handler will be unregistered from event
*/
unregister(event: Event, handler: (message: Message) => void): void;
off(event: Event, handler: (message: Message) => void): void;
/**
* Saves user data to the server.
*
* @param userData. An object containing user data
* @param callback will be called on success
* @param errorCallback will be called on error
*/
saveUser(userData: UserData,
callback: (userData: UserData) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Fetch user data from the server.
*
* @param callback will be called with fetched user data on success
* @param errorCallback will be called on error
*/
fetchUser(callback: (userData: UserData) => void, errorCallback: (error: MobileMessagingError) => void): void;
/**
* Gets user data from the locally stored cache.
*
* @param callback will be called with fetched user data on success
* @param errorCallback will be called on error
*/
getUser(callback: (userData: UserData) => void, errorCallback: (error: MobileMessagingError) => void): void;
/**
* Saves installation to the server.
*
* @param installation. An object containing installation data
* @param callback will be called on success
* @param errorCallback will be called on error
*/
saveInstallation(installation: Installation,
callback: (data: Installation) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Fetches installation from the server.
*
* @param callback will be called on success
* @param errorCallback will be called on error
*/
fetchInstallation(callback: (installation: Installation) => void, errorCallback: (error: MobileMessagingError) => void): void;
/**
* Gets locally cached installation.
*
* @param callback will be called on success
* @param errorCallback will be called on error
*/
getInstallation(callback: (installation: Installation) => void, errorCallback: (error: MobileMessagingError) => void): void;
/**
* Sets any installation as primary for this user.
*
* @param pushRegistrationId of an installation
* @param primary or not
* @param callback will be called on success
* @param errorCallback will be called on error
*/
setInstallationAsPrimary(pushRegistrationId: string,
primary: boolean,
callback: (installation: Installation) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Performs personalization of the current installation on the platform.
*
* @param context. An object containing user identity information as well as additional user attributes.
* @param callback will be called on success
* @param errorCallback will be called on error
*/
personalize(context: PersonalizeContext,
callback: (personalizeContext: PersonalizeContext) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Performs depersonalization of the current installation on the platform.
*
* @param callback will be called on success
* @param errorCallback will be called on error
*/
depersonalize(callback: (personalizeContext: PersonalizeContext) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Performs depersonalization of the installation referenced by pushRegistrationId.
*
* @param pushRegistrationId of the remote installation to depersonalize
* @param callback will be called on success
* @param errorCallback will be called on error
*/
depersonalizeInstallation(pushRegistrationId: string,
callback: (installation: Installation) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Mark messages as seen
*
* @param messageIds of identifiers of message to mark as seen
* @param callback will be called upon completion
* @param errorCallback will be called on error
*/
markMessagesSeen(messageIds: string[],
callback: (messages: Message[]) => void,
errorCallback: (error: MobileMessagingError) => void): void;
/**
* Displays built-in error dialog so that user can resolve errors during sdk initialization.
*
* @param errorCode to display dialog for
* @param callback will be called upon completion
* @param errorCallback will be called on error
*/
showDialogForError(errorCode: number,
callback: () => void,
errorCallback: (error: MobileMessagingError) => void): void;
defaultMessageStorage(): DefaultMessageStorage | undefined;
}
}
declare var MobileMessaging: MobileMessagingCordova.Api;

View File

@ -0,0 +1,167 @@
import Message = MobileMessagingCordova.Message;
import UserData = MobileMessagingCordova.UserData;
import MobileMessagingError = MobileMessagingCordova.MobileMessagingError;
import Installation = MobileMessagingCordova.Installation;
import PersonalizeContext = MobileMessagingCordova.PersonalizeContext;
MobileMessaging.init({
applicationCode: 'some-code',
geofencingEnabled: false,
ios: {
notificationTypes: ['alert', 'badge', 'sound']
}
},
(error) => {
console.log('Init error: ' + error.message);
}
);
MobileMessaging.register('messageReceived',
(message: MobileMessagingCordova.Message) => {
console.log('Message Received: ' + message.body);
}
);
const tappedHandler = (message: Message) => {
console.log(message);
};
MobileMessaging.register("notificationTapped", tappedHandler);
MobileMessaging.unregister("notificationTapped", tappedHandler);
MobileMessaging.saveUser({
externalUserId: 'some-user-123',
firstName: 'Name',
lastName: 'Lastname',
middleName: 'Middle',
gender: 'Male',
birthday: new Date(1987, 6, 5),
phones: ['+12345677890'],
emails: ['some@ema.il'],
tags: ['TestIonic'],
customAttributes: {
firstAttr: 'firstValue'
}
}, (userData: UserData) => {
console.log(userData);
}, (error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.fetchUser((userData: UserData) => {
console.log(userData);
}, (error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.getUser((userData: UserData) => {
console.log(userData);
}, (error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.saveInstallation({
isPrimaryDevice: true,
isPushRegistrationEnabled: true,
notificationsEnabled: true,
geoEnabled: true,
sdkVersion: '1.2.9',
os: 'Android',
osVersion: '9.0',
deviceManufacturer: 'Motorolla',
customAttributes: {
installationAttr: 'installationValue'
}
}, (result: Installation) => {
console.log('saveInstallation result:');
console.log(result);
MobileMessaging.getInstallation((installation: Installation) => {
console.log('getInstallation result:');
console.log(installation);
}, (error) => {
console.log('getInstallation error:');
console.error(error);
});
MobileMessaging.fetchInstallation((installation: Installation) => {
console.log(installation);
}, (error: MobileMessagingError) => {
console.error(error);
});
}, (error: MobileMessagingError) => {
console.log('saveInstallation error:');
console.error(error);
});
MobileMessaging.setInstallationAsPrimary(
'some-reg-id',
true,
(installation: Installation) => {
console.log(installation);
},
(error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.personalize({
userIdentity: {
externalUserId: 'some-user-123',
phones: ['+1234567890'],
emails: ['some@ema.il'],
},
userAttributes: {
personalizeAttr: 'personalizeValue'
}
}, (result) => {
console.log('Personalise result:');
console.log(result);
}, (error) => {
console.log('Personalise error:');
console.error(error);
});
MobileMessaging.depersonalize((personalizeContext: PersonalizeContext) => {
console.log(personalizeContext);
}, (error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.depersonalizeInstallation('pushRegistrationId',
(installation: Installation) => {
console.log(installation);
},
(error: MobileMessagingError) => {
console.error(error);
});
MobileMessaging.markMessagesSeen(['someMessageId'],
(messages: Message[]) => {
console.log(messages);
},
(error: MobileMessagingError) => {
console.error(error);
});
const defaultMessageStorage = MobileMessaging.defaultMessageStorage();
if (defaultMessageStorage !== undefined) {
defaultMessageStorage.find("messageId", (message: Message) => {
console.log(message);
});
defaultMessageStorage.findAll((messages: Message[]) => {
console.log(messages);
});
defaultMessageStorage.delete("messageId", () => {
console.log("Deleted");
});
defaultMessageStorage.deleteAll(() => {
console.log("Deleted all");
});
}
MobileMessaging.showDialogForError(1, () => {
console.log("Dialog opened");
}, () => {
console.error("Error opening dialog");
});

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"mobile-messaging-cordova-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }