some work on push notifications for ios

This commit is contained in:
nymea 2018-09-19 00:26:57 +02:00 committed by Michael Zanetti
parent 9279db9608
commit 21e8814fb3
6 changed files with 125 additions and 2 deletions

View File

@ -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<PushNotifications>("Nymea", 1, 0, "PushNotifications", PushNotifications::pushNotificationsProvider);
#ifdef BRANDING
engine->rootContext()->setContextProperty("appBranding", BRANDING);
#else

View File

@ -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

View File

@ -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
}

View File

@ -0,0 +1,29 @@
#ifndef PUSHNOTIFICATIONS_H
#define PUSHNOTIFICATIONS_H
#include <QObject>
#include <QQmlEngine>
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

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>

View File

@ -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 were 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