From 0549a4203253d60056364e54255c4f4659b41a54 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 5 Jan 2021 00:09:34 +0100 Subject: [PATCH] Make it backwards compatible - for now --- nymea-app/pushnotifications.cpp | 34 ++++++++++++++----- nymea-app/pushnotifications.h | 17 +++++++--- nymea-app/ui/RootItem.qml | 14 ++++---- .../ui/thingconfiguration/SetupWizard.qml | 2 +- packaging/ios/pushnotifications.mm | 1 - 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/nymea-app/pushnotifications.cpp b/nymea-app/pushnotifications.cpp index f96b0d24..069d7fb9 100644 --- a/nymea-app/pushnotifications.cpp +++ b/nymea-app/pushnotifications.cpp @@ -62,8 +62,11 @@ PushNotifications::PushNotifications(QObject *parent) : QObject(parent) m_pushClient = new PushClient(this); m_pushClient->setAppId("io.guh.nymeaapp_nymea-app"); connect(m_pushClient, &PushClient::tokenChanged, this, [this](const QString &token) { - m_token = token; - emit tokenChanged(); + // On UBPorts, core and cloud use the same token + m_coreToken = token; + emit coreTokenChanged(); + m_cloudToken = m_coreToken; + emit cloudTokenChanged(); }); #endif } @@ -109,16 +112,28 @@ QString PushNotifications::clientId() const return PlatformHelper::instance()->deviceSerial() + "+io.guh.nymeaapp" + branding; } -QString PushNotifications::token() const +QString PushNotifications::coreToken() const { - return m_token; + return m_coreToken; +} + +QString PushNotifications::cloudToken() const +{ + return m_cloudToken; } void PushNotifications::setAPNSRegistrationToken(const QString &apnsRegistrationToken) { qDebug() << "Received APNS push notification token:" << apnsRegistrationToken; - m_token = apnsRegistrationToken; - emit tokenChanged(); + m_cloudToken = apnsRegistrationToken; + emit cloudTokenChanged(); +} + +void PushNotifications::setFirebaseRegistrationToken(const QString &firebaseRegistrationToken) +{ + qDebug() << "Received Firebase/APNS push notification token:" << firebaseRegistrationToken; + m_coreToken = firebaseRegistrationToken; + emit coreTokenChanged(); } #if defined Q_OS_ANDROID && defined WITH_FIREBASE @@ -130,7 +145,10 @@ void PushNotifications::OnMessage(const firebase::messaging::Message &message) void PushNotifications::OnTokenReceived(const char *token) { qDebug() << "Firebase token received:" << token; - m_token = QString(token); - emit tokenChanged(); + // On Android, both, core and cloud use the same token + m_coreToken = QString(token); + emit coreTokenChanged(); + m_cloudToken = m_cloudToken; + emit cloudTokenChanged(); } #endif diff --git a/nymea-app/pushnotifications.h b/nymea-app/pushnotifications.h index 1456bee6..a7916b7e 100644 --- a/nymea-app/pushnotifications.h +++ b/nymea-app/pushnotifications.h @@ -53,7 +53,8 @@ class PushNotifications : public QObject Q_OBJECT Q_PROPERTY(QString service READ service CONSTANT) Q_PROPERTY(QString clientId READ clientId CONSTANT) - Q_PROPERTY(QString token READ token NOTIFY tokenChanged) + Q_PROPERTY(QString cloudToken READ cloudToken NOTIFY cloudTokenChanged) + Q_PROPERTY(QString coreToken READ coreToken NOTIFY coreTokenChanged) public: explicit PushNotifications(QObject *parent = nullptr); @@ -64,13 +65,16 @@ public: QString service() const; QString clientId() const; - QString token() const; + QString coreToken() const; + QString cloudToken() const; - // Called by Objective-C++ + // Called by Objective-C++ on iOS void setAPNSRegistrationToken(const QString &apnsRegistrationToken); + void setFirebaseRegistrationToken(const QString &firebaseRegistrationToken); signals: - void tokenChanged(); + void coreTokenChanged(); + void cloudTokenChanged(); protected: @@ -88,7 +92,10 @@ private: #endif private: - QString m_token; + // For nymea:core plugin based push notifications + QString m_coreToken; + // for nymea:cloud based push notifications (deprecated) + QString m_cloudToken; }; #endif // PUSHNOTIFICATIONS_H diff --git a/nymea-app/ui/RootItem.qml b/nymea-app/ui/RootItem.qml index baf640d1..a8fb5a0b 100644 --- a/nymea-app/ui/RootItem.qml +++ b/nymea-app/ui/RootItem.qml @@ -273,7 +273,7 @@ Item { return; } - if (PushNotifications.token.length === 0) { + if (PushNotifications.cloudToken.length === 0) { print("Don't have a token yet. Cannot register for push"); return; } @@ -289,7 +289,7 @@ Item { } AWSClient.registerPushNotificationEndpoint( - PushNotifications.token, + PushNotifications.cloudToken, PlatformHelper.machineHostname, clientId, PlatformHelper.deviceManufacturer, @@ -303,7 +303,7 @@ Item { print("This platform does not support push notifications") return; } - if (!PushNotifications.token) { + if (!PushNotifications.coreToken) { print("No push notification token available at this time. Not updating..."); return; } @@ -311,7 +311,7 @@ Item { print("Updating push notifications") print("Own push service:", PushNotifications.service); print("Own client ID:", PushNotifications.clientId); - print("Current token:", PushNotifications.token); + print("Current token:", PushNotifications.coreToken); for (var i = 0; i < engine.thingManager.things.count; i++) { var thing = engine.thingManager.things.get(i); @@ -321,11 +321,11 @@ Item { var tokenParam = thing.paramByName("token") print("Found a push notification thing for client id:", clientIdParam.value) if (clientIdParam.value === PushNotifications.clientId) { - if (tokenParam.value !== PushNotifications.token) { + if (tokenParam.value !== PushNotifications.coreToken) { var params = [ { "paramTypeId": serviceParam.paramTypeId, "value": PushNotifications.service }, { "paramTypeId": clientIdParam.paramTypeId, "value": PushNotifications.clientId }, - { "paramTypeId": tokenParam.paramTypeId, "value": PushNotifications.token } + { "paramTypeId": tokenParam.paramTypeId, "value": PushNotifications.coreToken } ]; print("Reconfiguring PushNotifications for", thing.name) engine.thingManager.reconfigureDevice(thing.id, params); @@ -399,7 +399,7 @@ Item { Connections { target: PushNotifications - onTokenChanged: { + onCloudTokenChanged: { setupPushNotifications(); } } diff --git a/nymea-app/ui/thingconfiguration/SetupWizard.qml b/nymea-app/ui/thingconfiguration/SetupWizard.qml index 1fdbdd96..a5cbef5c 100644 --- a/nymea-app/ui/thingconfiguration/SetupWizard.qml +++ b/nymea-app/ui/thingconfiguration/SetupWizard.qml @@ -404,7 +404,7 @@ Page { return PushNotifications.service; } if (paramType.id.toString().match(/\{?12ec06b2-44e7-486a-9169-31c684b91c8f\}?/)) { - return PushNotifications.token; + return PushNotifications.coreToken; } if (paramType.id.toString().match(/\{?d76da367-64e3-4b7d-aa84-c96b3acfb65e\}?/)) { return PushNotifications.clientId; diff --git a/packaging/ios/pushnotifications.mm b/packaging/ios/pushnotifications.mm index 5c4b3315..794595ad 100644 --- a/packaging/ios/pushnotifications.mm +++ b/packaging/ios/pushnotifications.mm @@ -92,7 +92,6 @@ NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"]; [[NSNotificationCenter defaultCenter] postNotificationName: @"FCMToken" object:nil userInfo:dataDict]; - // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated. qDebug() << "Firebase token received:" << QString::fromNSString(fcmToken); PushNotifications::instance()->setAPNSRegistrationToken(QString::fromNSString(fcmToken));