Last active
August 24, 2019 08:06
-
-
Save PetkevichPavel/c95ec1ebd22c663caba7ef15bdde646c to your computer and use it in GitHub Desktop.
Cloud function: class with helpful functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PlatformSettingsObj, PushTopics, Platform } from './RemoteConfigObj'; | |
import { SCOPES } from '../SecretProperties'; | |
const { google } = require('googleapis'); | |
/** | |
* This method is for requesting an access token. | |
* @param service - is the service account data. | |
*/ | |
export function getAccessToken(service: any) { | |
return new Promise(function (resolve, reject) { | |
const jwtClient = new google.auth.JWT( | |
service.client_email, | |
null, | |
service.private_key, | |
SCOPES, | |
null | |
); | |
jwtClient.authorize(function (err: any, tokens: { access_token: any; }) { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(tokens.access_token); | |
}); | |
}); | |
} | |
/** | |
* This function chooses on which topic will send notifications. | |
* @param platformSettings - platform settings from remote configuration. | |
* @returns Array<string> - array of topics. | |
*/ | |
export function choosePushTopics(platformSettings: PlatformSettingsObj | null):Array<string> { | |
const topic: Array<string> = new Array(); | |
if (platformSettings!.isPushAvailable) { | |
const flavor = platformSettings!.isItDev ? PushTopics.DEV : PushTopics.PROD | |
if (platformSettings!.platform === Platform.BOTH) { | |
topic.push(Platform.AN + "_" + flavor) | |
topic.push(Platform.IOS + "_" + flavor) | |
} else topic.push(platformSettings!.platform + "_" + flavor) | |
} | |
return topic | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment