From 4008171b79916e0727f11d27c7c5627440089e0a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 14 Nov 2019 17:27:09 +0100 Subject: [PATCH] Try using the UserNotification framework --- nymea-app/nymea-app.pro | 2 ++ packaging/ios/pushnotifications.mm | 39 +++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/nymea-app/nymea-app.pro b/nymea-app/nymea-app.pro index 4505458f..fe9b360f 100644 --- a/nymea-app/nymea-app.pro +++ b/nymea-app/nymea-app.pro @@ -102,6 +102,8 @@ ios: { OBJECTIVE_SOURCES += $$PWD/../packaging/ios/pushnotifications.mm \ $$PWD/../packaging/ios/platformhelperios.mm + LIBS += -framework "UserNotifications" + QMAKE_TARGET_BUNDLE_PREFIX = io.guh QMAKE_BUNDLE = nymeaApp # Configure generated xcode project to have our bundle id diff --git a/packaging/ios/pushnotifications.mm b/packaging/ios/pushnotifications.mm index d7fa4718..e54678ca 100644 --- a/packaging/ios/pushnotifications.mm +++ b/packaging/ios/pushnotifications.mm @@ -1,10 +1,13 @@ #import "UIKit/UIKit.h" +#import // Include our C++ class #include "pushnotifications.h" +#include + // This is hidden, so we declare it here to hook into it -@interface QIOSApplicationDelegate +@interface QIOSApplicationDelegate: UIResponder @end //add a category to QIOSApplicationDelegate @@ -12,31 +15,55 @@ // No need to declare the methods here, since we’re overriding existing ones @end + @implementation QIOSApplicationDelegate (APNSApplicationDelegate) -- (BOOL)application:(UIApplication *)application - didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register to receive notifications from the system - [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + center.delegate = self; + [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ + if(!error){ + [[UIApplication sharedApplication] registerForRemoteNotifications]; + } + }]; - [application registerForRemoteNotifications]; - NSLog(@"registered for remote notifications"); + NSLog(@"registering for remote notifications"); + qDebug() << "Registering 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])]; + + qDebug() << "Registering for remote notifications"; + qDebug() << "Token description:" << QString::fromNSString(deviceToken.description); + qDebug() << "Parsed token:" << QString::fromNSString(tokenStr); 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); + qWarning() << "Failed to register for notifications:" << QString::fromNSString(error.localizedDescription); +} + +-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ + NSLog(@"User Info : %@",notification.request.content.userInfo); + qDebug() << "willPresentNotification called!"; + completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); +} + +-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ + NSLog(@"User Info : %@",response.notification.request.content.userInfo); + qDebug() << "received notification response!"; + completionHandler(); } @end