Skip to content

Instantly share code, notes, and snippets.

@gsusmonzon
Last active December 16, 2021 19:16
Show Gist options
  • Save gsusmonzon/8d235c7fd93d1bd005eebb2b2d1ef2a5 to your computer and use it in GitHub Desktop.
Save gsusmonzon/8d235c7fd93d1bd005eebb2b2d1ef2a5 to your computer and use it in GitHub Desktop.
How to send different payloads per platform with node-gcm
import { IResponseBody as FcmResponse, Message as FcmMessage, Sender as FcmSender } from 'node-gcm';
class FcmMessage2 extends Message {
private apns: Record<string, unknown>;
private android: Record<string, unknown>;
addApnsData(data: Record<string, unknown>): void {
this.apns = data;
}
addAndroidData(data: Record<string, unknown>): void {
this.android = data;
}
toJson(): string {
const json = (FcmMessage.prototype as any).toJson.call(this);
if (this.apns) json['apns'] = this.apns;
if (this.android) json['android'] = this.android;
return json;
}
// [1] for iOS devices we want visible notifications, for Android devices we want 'data message' notifications
const message = new FcmMessage2({
priority: 'normal', // vs 'high'
collapseKey: options.collapseKey,
contentAvailable: !silent,
data,
});
message.addApnsData({ notification, 'apns-push-type': silent ? 'background' : 'alert' });
// fcmSender = new Sender(FcmKey, FcmRequestOptions);
// fcmSender.send(message, { registrationTokens }, (err, response: FcmResponse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment