/**
 * Google FIrebase를 이용한 Notification을 돕는 모듈
 * Firebase 설정 값은  config 에서 불러 옴
 *
 *  author ryeolj5911 gmail import request = require ( 'request-promise-native' ) ; import property = require ( '@app/core/config/property' ) ; import FcmError from './error/FcmError' ; import FcmLogServie = require ( '@app/core/fcm/FcmLogService' ) ; let apiKey = property . get ( 'core.fcm.apiKey' ) ; let projectId = property . get ( 'core.fcm.projectId' ) ; const device_api_header = { "Content-Type" : "application/json" , "Authorization" : "key=" + apiKey , "project_id" : projectId } ; export function createNotiKey ( key_name , registration_ids ) { return request ( { method : 'POST' , uri : 'https://android.googleapis.com/gcm/notification' , headers : device_api_header , body : { "operation" : "create" , "notification_key_name" : key_name , "registration_ids" : registration_ids } , json : true } ) . then ( function ( body ) { return Promise . resolve ( body . notification_key ) ; } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export function getNotiKey ( key_name ) { return request ( { method : 'GET' , uri : 'https://android.googleapis.com/gcm/notification' , headers : device_api_header , qs : { "notification_key_name" : key_name } , json : true } ) . then ( function ( body ) { return Promise . resolve ( body . notification_key ) ; } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export function addDevice ( key_name , key , registration_ids ) { return request ( { method : 'POST' , uri : 'https://android.googleapis.com/gcm/notification' , headers : device_api_header , body : { "operation" : "add" , "notification_key_name" : key_name , "notification_key" : key , "registration_ids" : registration_ids } , json : true } ) . then ( function ( body ) { return Promise . resolve ( body . notification_key ) ; } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export function removeDevice ( key_name , key , registration_ids ) { return request ( { method : 'POST' , uri : 'https://android.googleapis.com/gcm/notification' , headers : device_api_header , body : { "operation" : "remove" , "notification_key_name" : key_name , "notification_key" : key , "registration_ids" : registration_ids } , json : true } ) . then ( function ( body ) { return Promise . resolve ( body . notification_key ) ; } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export function addTopic ( registration_id ) { return request ( { method : 'POST' , uri : 'https://iid.googleapis.com/iid/v1/' + registration_id + '/rel/topics/global' , headers : { "Content-Type" : "application/json" , "Authorization" : "key=" + apiKey } } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export function removeTopicBatch ( registration_tokens ) { return request ( { method : 'POST' , uri : 'https://iid.googleapis.com/iid/v1:batchRemove' , headers : { "Content-Type" : "application/json" , "Authorization" : "key=" + apiKey } , body : { "to" : "/topics/global" , "registration_tokens" : registration_tokens } , json : true } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; } export async function sendMsg ( to , title , body , author , cause ) { let promise = request ( { method : 'POST' , uri : 'https://fcm.googleapis.com/fcm/send' , headers : { "Content-Type" : "application/json" , "Authorization" : "key=" + apiKey } , body : { "to" : to , "notification" : { "body" : body , "title" : title , "sound" : "default" } , "priority" : "high" , "content_available" : true } , json : true , } ) . catch ( function ( err ) { return Promise . reject ( new FcmError ( err . response . statusMessage ) ) ; } ) ; let response = await promise ; await FcmLogServie . addFcmLog ( to , author , title + '\n' + body , cause , response ) ; return response ; } export function sendGlobalMsg ( title , body , author , cause ) { return sendMsg ( "/topics/global" , title , body , author , cause ) ; }