Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(fcm): Deprecate sendToTopic and sendToCondition #2683

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions etc/firebase-admin.messaging.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ export class Messaging {
sendEachForMulticast(message: MulticastMessage, dryRun?: boolean): Promise<BatchResponse>;
// @deprecated
sendMulticast(message: MulticastMessage, dryRun?: boolean): Promise<BatchResponse>;
// @deprecated
sendToCondition(condition: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingConditionResponse>;
// @deprecated
sendToDevice(registrationTokenOrTokens: string | string[], payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDevicesResponse>;
// @deprecated
sendToDeviceGroup(notificationKey: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDeviceGroupResponse>;
// @deprecated
sendToTopic(topic: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingTopicResponse>;
subscribeToTopic(registrationTokenOrTokens: string | string[], topic: string): Promise<MessagingTopicManagementResponse>;
unsubscribeFromTopic(registrationTokenOrTokens: string | string[], topic: string): Promise<MessagingTopicManagementResponse>;
Expand Down
4 changes: 4 additions & 0 deletions src/messaging/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ export class Messaging {
*
* @returns A promise fulfilled with the server's response after the message
* has been sent.
*
* @deprecated Use {@link Messaging.send} instead.
*/
public sendToTopic(
topic: string,
Expand Down Expand Up @@ -737,6 +739,8 @@ export class Messaging {
*
* @returns A promise fulfilled with the server's response after the message
* has been sent.
*
* @deprecated Use {@link Messaging.send} instead.
*/
public sendToCondition(
condition: string,
Expand Down
192 changes: 2 additions & 190 deletions test/integration/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { Message, MulticastMessage, getMessaging } from '../../lib/messaging/index';
import { legacyTransportApp } from './setup';

chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

// The registration token and notification key have the proper format, but are not guaranteed to
// The registration token have the proper format, but are not guaranteed to
// work. The intention of these integration tests is that the endpoints returns the proper payload,
// but it is hard to ensure these tokens will always be valid. The tests below should still pass
// even if they are rotated or invalid.
const registrationToken = 'fGw0qy4TGgk:APA91bGtWGjuhp4WRhHXgbabIYp1jxEKI08ofj_v1bKhWAGJQ4e3arRCW' +
'zeTfHaLz83mBnDh0aPWB1AykXAVUUGl2h1wT4XI6XazWpvY7RBUSYfoxtqSWGIm2nvWh2BOP1YG501SsRoE';
const notificationKey = 'APA91bFYr4cWCkDs_H9VY2Ai6Erw1ABup1NEYqBjz70O8SzxjpALp_bN913XJMlOepaVv9e' +
'Qs2QrtqX_RZ6cVVv4czgTQXg62qicITR6tQDizaFilDnlVf0';

const registrationTokens = [registrationToken + '0', registrationToken + '1', registrationToken + '2'];
const topic = 'mock-topic';
const condition = '"test0" in topics || ("test1" in topics && "test2" in topics)';

const invalidTopic = 'topic-$%#^';

Expand Down Expand Up @@ -83,30 +78,7 @@ const message: Message = {
topic: 'foo-bar',
};

const payload = {
data: {
foo: 'bar',
},
notification: {
title: 'Message title',
body: 'Message body',
},
};

const invalidPayload: any = {
foo: 'bar',
};

const options = {
timeToLive: 60,
};

describe('admin.messaging', () => {

before(() => {
getMessaging(legacyTransportApp).enableLegacyHttpTransport()
})

it('send(message, dryRun) returns a message ID', () => {
return getMessaging().send(message, true)
.then((name) => {
Expand All @@ -115,37 +87,6 @@ describe('admin.messaging', () => {
});

it('sendEach()', () => {
const messages: Message[] = [message, message, message];
return getMessaging(legacyTransportApp).sendEach(messages, true)
.then((response) => {
expect(response.responses.length).to.equal(messages.length);
expect(response.successCount).to.equal(messages.length);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp) => {
expect(resp.success).to.be.true;
expect(resp.messageId).matches(/^projects\/.*\/messages\/.*$/);
});
});
});

it('sendEach(500)', () => {
const messages: Message[] = [];
for (let i = 0; i < 500; i++) {
messages.push({ topic: `foo-bar-${i % 10}` });
}
return getMessaging(legacyTransportApp).sendEach(messages, true)
.then((response) => {
expect(response.responses.length).to.equal(messages.length);
expect(response.successCount).to.equal(messages.length);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp) => {
expect(resp.success).to.be.true;
expect(resp.messageId).matches(/^projects\/.*\/messages\/.*$/);
});
});
});

it('sendEach() using HTTP2', () => {
const messages: Message[] = [message, message, message];
return getMessaging().sendEach(messages, true)
.then((response) => {
Expand All @@ -159,7 +100,7 @@ describe('admin.messaging', () => {
});
});

it('sendEach(500) using HTTP2', () => {
it('sendEach(500)', () => {
const messages: Message[] = [];
for (let i = 0; i < 500; i++) {
messages.push({ topic: `foo-bar-${i % 10}` });
Expand All @@ -176,57 +117,7 @@ describe('admin.messaging', () => {
});
});

it('sendAll()', () => {
const messages: Message[] = [message, message, message];
return getMessaging().sendAll(messages, true)
.then((response) => {
expect(response.responses.length).to.equal(messages.length);
expect(response.successCount).to.equal(messages.length);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp) => {
expect(resp.success).to.be.true;
expect(resp.messageId).matches(/^projects\/.*\/messages\/.*$/);
});
});
});

it('sendAll(500)', () => {
const messages: Message[] = [];
for (let i = 0; i < 500; i++) {
messages.push({ topic: `foo-bar-${i % 10}` });
}
return getMessaging().sendAll(messages, true)
.then((response) => {
expect(response.responses.length).to.equal(messages.length);
expect(response.successCount).to.equal(messages.length);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp) => {
expect(resp.success).to.be.true;
expect(resp.messageId).matches(/^projects\/.*\/messages\/.*$/);
});
});
});

it('sendEachForMulticast()', () => {
const multicastMessage: MulticastMessage = {
data: message.data,
android: message.android,
tokens: ['not-a-token', 'also-not-a-token'],
};
return getMessaging(legacyTransportApp).sendEachForMulticast(multicastMessage, true)
.then((response) => {
expect(response.responses.length).to.equal(2);
expect(response.successCount).to.equal(0);
expect(response.failureCount).to.equal(2);
response.responses.forEach((resp) => {
expect(resp.success).to.be.false;
expect(resp.messageId).to.be.undefined;
expect(resp.error).to.have.property('code', 'messaging/invalid-argument');
});
});
});

it('sendEachForMulticast() using HTTP2', () => {
const multicastMessage: MulticastMessage = {
data: message.data,
android: message.android,
Expand All @@ -245,85 +136,6 @@ describe('admin.messaging', () => {
});
});

it('sendMulticast()', () => {
const multicastMessage: MulticastMessage = {
data: message.data,
android: message.android,
tokens: ['not-a-token', 'also-not-a-token'],
};
return getMessaging().sendMulticast(multicastMessage, true)
.then((response) => {
expect(response.responses.length).to.equal(2);
expect(response.successCount).to.equal(0);
expect(response.failureCount).to.equal(2);
response.responses.forEach((resp) => {
expect(resp.success).to.be.false;
expect(resp.messageId).to.be.undefined;
expect(resp.error).to.have.property('code', 'messaging/invalid-argument');
});
});
});

it('sendToDevice(token) returns a response with multicast ID', () => {
return getMessaging().sendToDevice(registrationToken, payload, options)
.then((response) => {
expect(typeof response.multicastId).to.equal('number');
});
});

it('sendToDevice(token-list) returns a response with multicat ID', () => {
return getMessaging().sendToDevice(registrationTokens, payload, options)
.then((response) => {
expect(typeof response.multicastId).to.equal('number');
});
});

it.skip('sendToDeviceGroup() returns a response with success count', () => {
return getMessaging().sendToDeviceGroup(notificationKey, payload, options)
.then((response) => {
expect(typeof response.successCount).to.equal('number');
});
});

it('sendToTopic() returns a response with message ID', () => {
return getMessaging().sendToTopic(topic, payload, options)
.then((response) => {
expect(typeof response.messageId).to.equal('number');
});
});

it('sendToCondition() returns a response with message ID', () => {
return getMessaging().sendToCondition(condition, payload, options)
.then((response) => {
expect(typeof response.messageId).to.equal('number');
});
});

it('sendToDevice(token) fails when called with invalid payload', () => {
return getMessaging().sendToDevice(registrationToken, invalidPayload, options)
.should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-payload');
});

it('sendToDevice(token-list) fails when called with invalid payload', () => {
return getMessaging().sendToDevice(registrationTokens, invalidPayload, options)
.should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-payload');
});

it('sendToDeviceGroup() fails when called with invalid payload', () => {
return getMessaging().sendToDeviceGroup(notificationKey, invalidPayload, options)
.should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-payload');
});

it('sendToTopic() fails when called with invalid payload', () => {
return getMessaging().sendToTopic(topic, invalidPayload, options)
.should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-payload');
});

it('sendToCondition() fails when called with invalid payload', () => {
return getMessaging().sendToCondition(condition, invalidPayload, options)
.should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-payload');
});

it('subscribeToTopic() returns a response with success count', () => {
return getMessaging().subscribeToTopic(registrationToken, topic)
.then((response) => {
Expand Down