فهرست منبع

Support APNs push format

Danilo Bargen 7 سال پیش
والد
کامیت
1e2ce6b239
3فایلهای تغییر یافته به همراه45 افزوده شده و 12 حذف شده
  1. 0 1
      src/partials/welcome.ts
  2. 45 10
      src/services/push.ts
  3. 0 1
      src/services/webclient.ts

+ 0 - 1
src/partials/welcome.ts

@@ -274,7 +274,6 @@ class WelcomeController {
         if (decrypted.pushToken !== null && decrypted.pushTokenType !== null) {
             this.webClientService.updatePushToken(decrypted.pushToken, decrypted.pushTokenType);
             this.pushService.init(decrypted.pushToken, decrypted.pushTokenType);
-            this.$log.debug(this.logTag, 'Initialize push service');
         }
 
         // Reconnect

+ 45 - 10
src/services/push.ts

@@ -20,8 +20,13 @@ export class PushService {
     private static ARG_TOKEN = 'token';
     private static ARG_SESSION = 'session';
     private static ARG_VERSION = 'version';
+    private static ARG_ENDPOINT = 'endpoint';
+    private static ARG_BUNDLE_ID = 'bundleid';
+
+    private logTag: string = '[PushService]';
 
     private $http: ng.IHttpService;
+    private $log: ng.ILogService;
     private $httpParamSerializerJQLike;
 
     private url: string;
@@ -29,10 +34,11 @@ export class PushService {
     private pushType = threema.PushTokenType.Gcm;
     private version: number = null;
 
-    public static $inject = ['$http', '$httpParamSerializerJQLike', 'CONFIG', 'PROTOCOL_VERSION'];
-    constructor($http: ng.IHttpService, $httpParamSerializerJQLike,
+    public static $inject = ['$http', '$log', '$httpParamSerializerJQLike', 'CONFIG', 'PROTOCOL_VERSION'];
+    constructor($http: ng.IHttpService, $log: ng.ILogService, $httpParamSerializerJQLike,
                 CONFIG: threema.Config, PROTOCOL_VERSION: number) {
         this.$http = $http;
+        this.$log = $log;
         this.$httpParamSerializerJQLike = $httpParamSerializerJQLike;
         this.url = CONFIG.PUSH_URL;
         this.version = PROTOCOL_VERSION;
@@ -42,6 +48,7 @@ export class PushService {
      * Initiate the push service with a push token.
      */
     public init(pushToken: string, pushTokenType: threema.PushTokenType): void {
+        this.$log.info(this.logTag, 'Initialized with', pushTokenType, 'token');
         this.pushToken = pushToken;
         this.pushType = pushTokenType;
     }
@@ -70,25 +77,53 @@ export class PushService {
         }
 
         // Prepare request
+        const data = {
+            [PushService.ARG_TYPE]: this.pushType,
+            [PushService.ARG_SESSION]: sha256(session),
+            [PushService.ARG_VERSION]: this.version,
+        };
+        if (this.pushType === threema.PushTokenType.Apns) {
+            // APNS token format: "<hex-deviceid>;<endpoint>;<bundle-id>"
+            const parts = this.pushToken.split(';');
+            if (parts.length < 3) {
+                this.$log.warn(this.logTag, 'APNS push token contains', parts.length, 'parts, at least 3 are required');
+                return Promise.resolve(false);
+            }
+            data[PushService.ARG_TOKEN] = parts[0];
+            data[PushService.ARG_ENDPOINT] = parts[1];
+            data[PushService.ARG_BUNDLE_ID] = parts[2];
+        } else if (this.pushType === threema.PushTokenType.Gcm) {
+            data[PushService.ARG_TOKEN] = this.pushToken;
+        } else {
+            this.$log.warn(this.logTag, 'Invalid push type');
+            return Promise.resolve(false);
+        }
+
         const request = {
             method: 'POST',
             url: this.url,
             headers: {
                 'Content-Type': 'application/x-www-form-urlencoded',
             },
-            data: this.$httpParamSerializerJQLike({
-                [PushService.ARG_TYPE]: this.pushType,
-                [PushService.ARG_SESSION]: sha256(session),
-                [PushService.ARG_TOKEN]: this.pushToken,
-                [PushService.ARG_VERSION]: this.version,
-            }),
+            data: this.$httpParamSerializerJQLike(data),
         };
 
         // Send push
         return new Promise((resolve) => {
             this.$http(request).then(
-                (successResponse) => resolve(successResponse.status === 204),
-                (errorResponse) => resolve(false),
+                (successResponse) => {
+                    if (successResponse.status === 204) {
+                        this.$log.debug(this.logTag, 'Sent push');
+                        resolve(true);
+                    } else {
+                        this.$log.warn(this.logTag, 'Sending push failed: HTTP ' + successResponse.status);
+                        resolve(false);
+                    }
+                },
+                (errorResponse) => {
+                    this.$log.warn(this.logTag, 'Sending push failed:', errorResponse);
+                    resolve(false);
+                },
             );
         });
     }

+ 0 - 1
src/services/webclient.ts

@@ -2360,7 +2360,6 @@ export class WebClientService {
     }
 
     public updatePushToken(token: string, tokenType: threema.PushTokenType): void {
-        this.$log.debug(this.logTag, 'Updating', tokenType, 'push token');
         this.pushToken = token;
         this.pushTokenType = tokenType;
     }