Make it backwards compatible - for now
This commit is contained in:
parent
82454fc554
commit
0549a42032
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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));
|
||||
|
||||
Reference in New Issue
Block a user