Skip to content

Commit

Permalink
Channels-1034 Set google Api version for Push (#1883)
Browse files Browse the repository at this point in the history
* feat: set googleApiVersion

* fix: set legacy as default version

* fix: defaults

---------

Co-authored-by: suppalapati <sneha.uppalapati@segment.com>
Co-authored-by: alfrimpong <afrimpong@twilio.com>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent a77f4a7 commit b8439ca
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ export interface Payload {
* Time of when the actual event happened.
*/
eventOccurredTS?: string
/**
* Controls the notification payload format
*/
googleApiVersion?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,128 @@ describe('sendMobilePush action', () => {
expect(responses[1].data).toMatchObject(externalIds[0])
})
})

describe('Google Api Formatting', () => {
const externalId = {
type: 'android.push_token',
id: 'android-token-1',
channelType: 'ANDROID_PUSH',
subscriptionStatus: 'subscribed'
}

const androidLegacyReq = new URLSearchParams({
Body: defaultTemplate.types['twilio/text'].body,
Title: customizationTitle,
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: 1
}
}),
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
badge: 1
}
}),
Recipients: JSON.stringify({
fcm: [{ addr: externalId.id }]
}),
CustomData: JSON.stringify({
space_id: spaceId,
badgeAmount: 1,
badgeStrategy: 'inc',
__segment_internal_external_id_key__: externalId.type,
__segment_internal_external_id_value__: externalId.id
})
})

const androidV1Req = new URLSearchParams({
Body: defaultTemplate.types['twilio/text'].body,
Title: customizationTitle,
FcmPayload: JSON.stringify({
android: {
mutable_content: true,
notification: {
badge: 1
}
}
}),
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
badge: 1
}
}),
Recipients: JSON.stringify({
fcm: [{ addr: externalId.id }]
}),
CustomData: JSON.stringify({
space_id: spaceId,
badgeAmount: 1,
badgeStrategy: 'inc',
__segment_internal_external_id_key__: externalId.type,
__segment_internal_external_id_value__: externalId.id
})
})

it('should format FCM overrides with legacy format when googleApiVersion field is not provided', async () => {
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidLegacyReq

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})

it('should format FCM overrides with legacy format when googleApiVersion field is set to legacy', async () => {
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidLegacyReq

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
googleApiVersion: 'legacy',
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})

it('should format FCM overrides with v1 format when googleApiVersion field is v1', async () => {
const externalId = {
type: 'android.push_token',
id: 'android-token-1',
channelType: 'ANDROID_PUSH',
subscriptionStatus: 'subscribed'
}
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidV1Req

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
googleApiVersion: 'v1',
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@ export class PushSender extends TwilioMessageSender<PushPayload> {
Sound: this.payload.customizations?.sound,
Priority: this.payload.customizations?.priority,
TimeToLive: this.payload.customizations?.ttl,
FcmPayload: {
mutable_content: true,
notification: {
badge: badgeAmount
}
},
FcmPayload: this.getFcmNotificationOverrides(badgeAmount),
ApnPayload: {
aps: {
'mutable-content': 1,
Expand All @@ -172,6 +167,27 @@ export class PushSender extends TwilioMessageSender<PushPayload> {
}
}

private getFcmNotificationOverrides(badgeAmount: number) {
// FCM V1 format
if (this.payload.googleApiVersion === 'v1') {
return {
android: {
mutable_content: true,
notification: {
badge: badgeAmount
}
}
}
}
// FCM legacy format
return {
mutable_content: true,
notification: {
badge: badgeAmount
}
}
}

// transforms open_app + url, to deep_link tap action preset
// when the tap action is open_app and there is a link, it is supposed to be "deep_link"
// any other conditions return the tap action as is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ export const actionDefinition: ActionDefinition<Settings, Payload> = {
default: {
'@path': '$.timestamp'
}
},
googleApiVersion: {
label: 'Google Api Version',
description: 'Controls the notification payload format',
type: 'string',
required: false,
choices: [
{ label: 'legacy', value: 'legacy' },
{ label: 'v1', value: 'v1' }
],
default: 'legacy'
}
},
perform: async (request, data) => {
Expand Down

0 comments on commit b8439ca

Please sign in to comment.