From 5639b78c62fe96c8594e07388fabead07738869b Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Thu, 23 Nov 2017 06:26:22 +0900
Subject: [PATCH] Fix bug

---
 src/web/app/common/mios.ts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts
index 7f9aacc46c..e73a65bff8 100644
--- a/src/web/app/common/mios.ts
+++ b/src/web/app/common/mios.ts
@@ -257,8 +257,14 @@ export default class MiOS extends EventEmitter {
 				});
 			}).then(() => {
 				this.logInfo('[sw] Server Stored Subscription.');
-			}).catch(err => {
+			}).catch(async (err) => {
 				this.logError('[sw] Subscribe Error:', err);
+
+				// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
+				// 既に存在していることが原因でエラーになった可能性があるので、
+				// そのサブスクリプションを解除しておく
+				const subscription = await this.swRegistration.pushManager.getSubscription();
+				if (subscription) subscription.unsubscribe();
 			});
 		});