Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #362 from danielpassos/subscribe-alias-topic
Browse files Browse the repository at this point in the history
Subscribe alias topic
  • Loading branch information
danielpassos committed May 30, 2019
2 parents e6afd51 + a95f30c commit 1bd3196
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
56 changes: 50 additions & 6 deletions packages/push/src/PushRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { isCordovaAndroid, isCordovaIOS, ServiceConfiguration, ConfigurationService } from "@aerogear/core";
import axios from "axios";

Expand All @@ -18,7 +17,8 @@ export class PushRegistration {
public static readonly TYPE: string = "push";
public static readonly API_PATH: string = "rest/registry/device";

private pushConfig?: ServiceConfiguration;
private readonly pushConfig?: ServiceConfiguration;
private _objectInstance: any;

constructor(config: ConfigurationService) {
const configuration = config.getConfigByType(PushRegistration.TYPE);
Expand All @@ -37,16 +37,19 @@ export class PushRegistration {
* @param categories array list of categories that device should be register to.
*/
public register(deviceToken: string, alias: string = "", categories: string[] = []): Promise<void> {
if (!window || !window.device) {
if (!window || !window.device || !window.PushNotification) {
return Promise.reject(new Error("Registration requires cordova plugin. Verify the " +
"@aerogear/cordova-plugin-aerogear-push plugin is installed."));
}

if (!this.pushConfig || !this.pushConfig.config || !this.pushConfig.url) {
return Promise.reject(new Error("UPS registration: configuration is invalid"));
}

if (!deviceToken) {
return Promise.reject(new Error("Device token should not be empty"));
}

let platformConfig;
const url = this.pushConfig.url;
if (isCordovaAndroid()) {
Expand All @@ -61,14 +64,28 @@ export class PushRegistration {
return Promise.reject(new Error("UPS registration: Platform is configured." +
"Please add UPS variant and generate mobile - config.json again"));
}

const variantId = platformConfig.variantId || platformConfig.variantID;
const variantSecret = platformConfig.variantSecret;
if (!variantId) {
return Promise.reject(new Error("UPS registration: variantId is not defined."));
}

const variantSecret = platformConfig.variantSecret;
if (!variantSecret) {
return Promise.reject(new Error("UPS registration: variantSecret is not defined."));
}

this._objectInstance = window.PushNotification.init(
{
android: {},
ios: {
alert: true,
badge: true,
sound: true
}
}
);

const authToken = window.btoa(`${variantId}:${variantSecret}`);
const postData = {
"deviceToken": deviceToken,
Expand All @@ -78,13 +95,27 @@ export class PushRegistration {
"alias": alias,
"categories": categories
};

const instance = axios.create({
baseURL: url,
timeout: 5000,
headers: { "Authorization": `Basic ${authToken}` }
headers: {"Authorization": `Basic ${authToken}`}
});

return new Promise((resolve, reject) => {
return instance.post(PushRegistration.API_PATH, postData).then(() => resolve()).catch(reject);
return instance.post(PushRegistration.API_PATH, postData)
.then(
() => {
if (isCordovaAndroid()) {
this.subscribeToFirebaseTopic(variantId);
for (const category of categories) {
this.subscribeToFirebaseTopic(category);
}
}
resolve();
}
)
.catch(reject);
});
}

Expand All @@ -101,4 +132,17 @@ export class PushRegistration {
public hasConfig(): boolean {
return !!this.pushConfig;
}

private subscribeToFirebaseTopic(topic: string) {
this._objectInstance.subscribe(
topic,
() => {
console.warn("FCM topic: " + topic + " subscribed");
},
(e: any) => {
console.warn("error:", e);
}
);
}

}
5 changes: 3 additions & 2 deletions packages/push/test/PushRegistrationTest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { assert, expect } from "chai";
import { PushRegistration } from "../src/PushRegistration";
import { assert } from "chai";
import { PushRegistration } from "../src";
import { ConfigurationService } from "@aerogear/core";

declare var window: any;
declare var global: any;

global.window = { btoa: () => "dGVzdA==" };
global.window.PushNotification = { init: () => "" };
window.device = { platform: "iOS" };

describe("Registration tests", () => {
Expand Down
6 changes: 0 additions & 6 deletions packages/push/test/indexTest.ts

This file was deleted.

0 comments on commit 1bd3196

Please sign in to comment.