diff --git a/nymea-app/main.cpp b/nymea-app/main.cpp index 860b8221..7e46a4e2 100644 --- a/nymea-app/main.cpp +++ b/nymea-app/main.cpp @@ -34,6 +34,7 @@ #include "libnymea-app-core.h" #include "stylecontroller.h" +#include "pushnotifications.h" int main(int argc, char *argv[]) { @@ -87,6 +88,8 @@ int main(int argc, char *argv[]) engine->rootContext()->setContextProperty("pushServices", pushServices); #endif + qmlRegisterSingletonType("Nymea", 1, 0, "PushNotifications", PushNotifications::pushNotificationsProvider); + #ifdef BRANDING engine->rootContext()->setContextProperty("appBranding", BRANDING); #else diff --git a/nymea-app/nymea-app.pro b/nymea-app/nymea-app.pro index 53049f53..edd7f0f8 100644 --- a/nymea-app/nymea-app.pro +++ b/nymea-app/nymea-app.pro @@ -17,10 +17,12 @@ linux:!android:LIBS += -lavahi-client -lavahi-common PRE_TARGETDEPS += ../libnymea-app-core ../libnymea-common HEADERS += \ - stylecontroller.h + stylecontroller.h \ + pushnotifications.h SOURCES += main.cpp \ - stylecontroller.cpp + stylecontroller.cpp \ + pushnotifications.cpp OTHER_FILES += $$files(*.qml, true) @@ -88,6 +90,16 @@ ios: { ios_icon_files.files += $$files(../packaging/ios/AppIcon*.png) ios_launch_images.files += $$files(../packaging/ios/LaunchImage*.png) ../packaging/ios/LaunchScreen1.xib QMAKE_BUNDLE_DATA += ios_icon_files ios_launch_images + + IOS_DEVELOPMENT_TEAM.name = DEVELOPMENT_TEAM + IOS_DEVELOPMENT_TEAM.value = Z45PLKLTHM + QMAKE_MAC_XCODE_SETTINGS += IOS_DEVELOPMENT_TEAM + + IOS_ENTITLEMENTS.name = CODE_SIGN_ENTITLEMENTS + IOS_ENTITLEMENTS.value = $$files($$PWD/../packaging/ios/pushnotifications.entitlements) + QMAKE_MAC_XCODE_SETTINGS += IOS_ENTITLEMENTS + + OBJECTIVE_SOURCES += $$PWD/../packaging/ios/pushnotifications.mm } BR=$$BRANDING diff --git a/nymea-app/pushnotifications.cpp b/nymea-app/pushnotifications.cpp new file mode 100644 index 00000000..d7977c0b --- /dev/null +++ b/nymea-app/pushnotifications.cpp @@ -0,0 +1,28 @@ +#include "pushnotifications.h" + +PushNotifications::PushNotifications(QObject *parent) : QObject(parent) +{ + +} + +QObject *PushNotifications::pushNotificationsProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + return instance(); +} + +PushNotifications *PushNotifications::instance() +{ + static PushNotifications* pushNotifications = new PushNotifications(); + return pushNotifications; +} + +QString PushNotifications::apnsRegistrationToken() const +{ + return m_apnsToken; +} + +void PushNotifications::setAPNSRegistrationToken(const QString &apnsRegistrationToken) +{ + m_apnsToken = apnsRegistrationToken; + apnsRegistrationTokenChanged(); //emit signal +} diff --git a/nymea-app/pushnotifications.h b/nymea-app/pushnotifications.h new file mode 100644 index 00000000..afe9571e --- /dev/null +++ b/nymea-app/pushnotifications.h @@ -0,0 +1,29 @@ +#ifndef PUSHNOTIFICATIONS_H +#define PUSHNOTIFICATIONS_H + +#include +#include + +class PushNotifications : public QObject +{ + Q_OBJECT +public: + explicit PushNotifications(QObject *parent = nullptr); + + static QObject* pushNotificationsProvider(QQmlEngine *engine, QJSEngine *scriptEngine); + static PushNotifications* instance(); + + QString apnsRegistrationToken() const; + void setAPNSRegistrationToken(const QString &apnsRegistrationToken); + +signals: + void gcmRegistrationTokenChanged(); + void apnsRegistrationTokenChanged(); + void registeredChanged(); + +private: + QString m_gcmToken; + QString m_apnsToken; +}; + +#endif // PUSHNOTIFICATIONS_H diff --git a/packaging/ios/pushnotifications.entitlements b/packaging/ios/pushnotifications.entitlements new file mode 100644 index 00000000..26a6c367 --- /dev/null +++ b/packaging/ios/pushnotifications.entitlements @@ -0,0 +1,8 @@ + + + + + aps-environment + development + + diff --git a/packaging/ios/pushnotifications.mm b/packaging/ios/pushnotifications.mm new file mode 100644 index 00000000..b4688bf9 --- /dev/null +++ b/packaging/ios/pushnotifications.mm @@ -0,0 +1,43 @@ +#import "UIKit/UIKit.h" + +// Include our C++ class +#include "pushnotifications.h" + +// This is hidden, so we declare it here to hook into it +@interface QIOSApplicationDelegate +@end + +//add a category to QIOSApplicationDelegate +@interface QIOSApplicationDelegate (APNSApplicationDelegate) +// No need to declare the methods here, since we’re overriding existing ones +@end + +@implementation QIOSApplicationDelegate (APNSApplicationDelegate) + +- (BOOL)application:(UIApplication *)application + didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Register to receive notifications from the system + [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; + + [application registerForRemoteNotifications]; + NSLog(@"registered for remote notifications"); + return YES; +} + +- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { + NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken); + const unsigned *tokenBytes = (const unsigned*)[deviceToken bytes]; + NSString *tokenStr = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", + ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]), + ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]), + ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; + + PushNotifications::instance()->setAPNSRegistrationToken(QString::fromNSString(tokenStr)); +} + +- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + NSLog(@"Did Fail to Register for Remote Notifications"); + NSLog(@"%@, %@", error, error.localizedDescription); +} + +@end