From 7380d4099e8cd491837874b2049c7bb41492d4e2 Mon Sep 17 00:00:00 2001 From: REIS Benjamin Date: Mon, 12 Sep 2016 13:51:26 +0200 Subject: [PATCH] Implementation PushKit, pb when touching Pushkit notif --- Classes/LaunchScreen.xib | 36 ++++++----- Classes/LinphoneAppDelegate.h | 5 +- Classes/LinphoneAppDelegate.m | 96 +++++++++++++++++++++++------- Classes/LinphoneManager.m | 27 ++++++--- linphone-Info.plist | 1 + linphone.entitlements | 8 +++ linphone.xcodeproj/project.pbxproj | 12 +++- 7 files changed, 137 insertions(+), 48 deletions(-) create mode 100644 linphone.entitlements diff --git a/Classes/LaunchScreen.xib b/Classes/LaunchScreen.xib index a5c5107e4..21717df44 100644 --- a/Classes/LaunchScreen.xib +++ b/Classes/LaunchScreen.xib @@ -1,8 +1,9 @@ - + - + + @@ -12,39 +13,39 @@ - + - + - + - + - + - + @@ -75,6 +76,11 @@ - + + + + + + diff --git a/Classes/LinphoneAppDelegate.h b/Classes/LinphoneAppDelegate.h index edda21b1b..9446192c1 100644 --- a/Classes/LinphoneAppDelegate.h +++ b/Classes/LinphoneAppDelegate.h @@ -18,12 +18,14 @@ */ #import +#import #import #import "LinphoneCoreSettingsStore.h" -@interface LinphoneAppDelegate : NSObject { +//@interface LinphoneAppDelegate : NSObject { +@interface LinphoneAppDelegate : NSObject { @private UIBackgroundTaskIdentifier bgStartId; BOOL startedInBackground; @@ -35,6 +37,7 @@ @property (nonatomic, retain) UIAlertView *waitingIndicator; @property (nonatomic, retain) NSString *configURL; @property (nonatomic, strong) UIWindow* window; +@property PKPushRegistry* voipRegistry; @end diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index aa5e15211..282e28d0c 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -21,6 +21,7 @@ #import "ShopView.h" #import "linphoneAppDelegate.h" #import "AddressBook/ABPerson.h" +#import #import "CoreTelephony/CTCallCenter.h" #import "CoreTelephony/CTCall.h" @@ -182,23 +183,17 @@ - (void)registerForNotifications:(UIApplication *)app { LinphoneManager *instance = [LinphoneManager instance]; - - if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { - /* iOS8 notifications can be actioned! Awesome: */ - UIUserNotificationType notifTypes = - UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; - - NSSet *categories = - [NSSet setWithObjects:[self getCallNotificationCategory], [self getMessageNotificationCategory], [self getAccountExpiryNotificationCategory], nil]; - UIUserNotificationSettings *userSettings = - [UIUserNotificationSettings settingsForTypes:notifTypes categories:categories]; - [app registerUserNotificationSettings:userSettings]; - - if (!instance.isTesting) { - [app registerForRemoteNotifications]; - } + if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) { + //[app unregisterForRemoteNotifications]; + // iOS8 and more : PushKit + self.voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()]; + self.voipRegistry.delegate = self; + + // Initiate registration. + self.voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; } else { /* iOS7 and below */ + self.voipRegistry.delegate = NULL; if (!instance.isTesting) { NSUInteger notifTypes = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge; @@ -209,21 +204,19 @@ #pragma deploymate pop - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - UIApplication *app = [UIApplication sharedApplication]; UIApplicationState state = app.applicationState; LinphoneManager *instance = [LinphoneManager instance]; BOOL background_mode = [instance lpConfigBoolForKey:@"backgroundmode_preference"]; BOOL start_at_boot = [instance lpConfigBoolForKey:@"start_at_boot_preference"]; - [self registerForNotifications:app]; if (state == UIApplicationStateBackground) { // we've been woken up directly to background; if (!start_at_boot || !background_mode) { // autoboot disabled or no background, and no push: do nothing and wait for a real launch - /*output a log with NSLog, because the ortp logging system isn't activated yet at this time*/ + //output a log with NSLog, because the ortp logging system isn't activated yet at this time NSLog(@"Linphone launch doing nothing because start_at_boot or background_mode are not activated.", NULL); return YES; } @@ -248,7 +241,16 @@ } if (bgStartId != UIBackgroundTaskInvalid) [[UIApplication sharedApplication] endBackgroundTask:bgStartId]; - + + //Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later + UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert| UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; + + //register the notification settings + [application registerUserNotificationSettings:notificationSettings]; + + //output what state the app is in. This will be used to see when the app is started in the background + NSLog(@"app launched with state : %li", (long)application.applicationState); + return YES; } @@ -324,8 +326,10 @@ As a result, break it and refresh registers in order to make sure to receive incoming INVITE or MESSAGE*/ if (linphone_core_get_calls(LC) == NULL) { // if there are calls, obviously our TCP socket shall be working linphone_core_set_network_reachable(LC, FALSE); - LinphoneManager.instance.connectivity = none; /*force connectivity to be discovered again*/ + LinphoneManager.instance.connectivity = none; /*force connectivity to be discovered again*/ + LOGI(@"Registers refreshing"); [LinphoneManager.instance refreshRegisters]; + LOGI(@"Registers refreshed"); if (loc_key != nil) { NSString *callId = [userInfo objectForKey:@"call-id"]; @@ -344,6 +348,7 @@ } } } + LOGI(@"Notification %@ processed", userInfo.description); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { @@ -442,13 +447,60 @@ [LinphoneManager.instance setPushNotificationToken:nil]; } +#pragma mark - PushKit Functions + +- (void)pushRegistry:(PKPushRegistry *)registry +didInvalidatePushTokenForType:(NSString *)type { + LOGI(@"PushKit Token invalidated"); + [LinphoneManager.instance setPushNotificationToken:nil]; +} + +- (void)pushRegistry:(PKPushRegistry *)registry + didReceiveIncomingPushWithPayload:(PKPushPayload *)payload + forType:(NSString *)type { + LOGI(@"PushKit received with payload : %@", payload.description); + + /*NSDictionary *payloadDict = payload.dictionaryPayload[@"aps"]; + NSString *message = payloadDict[@"alert"]; + + //present a local notifcation to visually see when we are recieving a VoIP Notification + if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) { + + UILocalNotification *localNotification = [UILocalNotification new]; + localNotification.alertBody = message; + localNotification.applicationIconBadgeNumber = 1; + localNotification.soundName = UILocalNotificationDefaultSoundName; + + [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; + }*/ + + LOGI(@"incoming voip notfication: %@ ", payload.dictionaryPayload); + dispatch_async(dispatch_get_main_queue(), ^{[self processRemoteNotification:payload.dictionaryPayload];}); +} + +- (void)pushRegistry:(PKPushRegistry *)registry + didUpdatePushCredentials:(PKPushCredentials *)credentials + forType:(NSString *)type { + LOGI(@"PushKit credentials updated"); + LOGI(@"voip token: %@", (credentials.token)); + LOGI(@"%@ : %@", NSStringFromSelector(_cmd), credentials.token); + [LinphoneManager.instance setPushNotificationToken:credentials.token]; +} + #pragma mark - User notifications -#pragma deploymate push "ignored-api-availability" +/*#pragma deploymate push "ignored-api-availability" - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { + //register for voip notifiactions + self.voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()]; + + // Initiate registration. + NSLog(@"Initiating PushKit registration"); + self.voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; + self.voipRegistry.delegate = self; LOGI(@"%@", NSStringFromSelector(_cmd)); -} +}*/ - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index ec4b186e0..0987476b0 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -616,7 +616,7 @@ static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char } - (void)onCall:(LinphoneCall *)call StateChanged:(LinphoneCallState)state withMessage:(const char *)message { - + // Handling wrapper LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call); if (!data) { @@ -638,7 +638,6 @@ static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char NSString *address = [FastAddressBook displayNameForAddress:addr]; if (state == LinphoneCallIncomingReceived) { - /*first step is to re-enable ctcall center*/ CTCallCenter *lCTCallCenter = [[CTCallCenter alloc] init]; @@ -653,12 +652,12 @@ static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char return; } - if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { + if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { LinphoneCallLog *callLog = linphone_call_get_call_log(call); NSString *callId = [NSString stringWithUTF8String:linphone_call_log_get_call_id(callLog)]; - if (![LinphoneManager.instance popPushCallID:callId]) { + //if (![LinphoneManager.instance popPushCallID:callId]) { // case where a remote notification is not already received // Create a new local notification data->notification = [[UILocalNotification alloc] init]; @@ -704,7 +703,7 @@ static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char } } } - } + //} } } @@ -1925,9 +1924,12 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { if (connectivity == none) { // don't trust ios when he says there is no network. Create a new reachability context, the previous one might // be mis-functionning. + LOGI(@"None connectivity"); [self setupNetworkReachabilityCallback]; } - linphone_core_refresh_registers(theLinphoneCore); // just to make sure REGISTRATION is up to date + LOGI(@"Network reachability callback setup"); + linphone_core_refresh_registers(theLinphoneCore); // just to make sure REGISTRATION is up to date + LOGI(@"Out of refreshRegisters"); } - (void)renameDefaultSettings { @@ -2216,10 +2218,19 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"local_ring" inSection:@"sound"].lastPathComponent] ?: [LinphoneManager bundleFile:@"notes_of_the_optimistic.caf"]) .lastPathComponent; + NSString * notif_type; + if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) { + //IOS 8 and more + notif_type = @".voip"; + } else { + // IOS 7 and below + notif_type = @""; + } + NSString *params = [NSString - stringWithFormat:@"app-id=%@.%@;pn-type=apple;pn-tok=%@;pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-" + stringWithFormat:@"app-id=%@%@.%@;pn-type=apple;pn-tok=%@;pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-" @"call-snd=%@;pn-msg-snd=msg.caf", - [[NSBundle mainBundle] bundleIdentifier], APPMODE_SUFFIX, tokenString, ring]; + [[NSBundle mainBundle] bundleIdentifier], notif_type, APPMODE_SUFFIX, tokenString, ring]; LOGI(@"Proxy config %s configured for push notifications with contact: %@", linphone_proxy_config_get_identity(proxyCfg), params); diff --git a/linphone-Info.plist b/linphone-Info.plist index 5fdb08ae3..859f1cd16 100644 --- a/linphone-Info.plist +++ b/linphone-Info.plist @@ -77,6 +77,7 @@ UIBackgroundModes audio + fetch remote-notification voip diff --git a/linphone.entitlements b/linphone.entitlements new file mode 100644 index 000000000..903def2af --- /dev/null +++ b/linphone.entitlements @@ -0,0 +1,8 @@ + + + + + aps-environment + development + + diff --git a/linphone.xcodeproj/project.pbxproj b/linphone.xcodeproj/project.pbxproj index 69eaaefbc..9b940bdc4 100755 --- a/linphone.xcodeproj/project.pbxproj +++ b/linphone.xcodeproj/project.pbxproj @@ -666,6 +666,7 @@ 63FD3F121CA17FA400E9AECC /* libmbedx509.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 63FD3F081CA17F9100E9AECC /* libmbedx509.a */; }; 70E542F313E147E3002BA2C0 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 70E542F213E147E3002BA2C0 /* OpenGLES.framework */; }; 70E542F513E147EB002BA2C0 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 70E542F413E147EB002BA2C0 /* QuartzCore.framework */; }; + 8CA2004C1D8158440095F859 /* PushKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8CA2004B1D8158440095F859 /* PushKit.framework */; }; C90FAA7915AF54E6002091CB /* HistoryDetailsView.m in Sources */ = {isa = PBXBuildFile; fileRef = C90FAA7715AF54E6002091CB /* HistoryDetailsView.m */; }; D306459E1611EC2A00BB571E /* UILoadingImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = D306459D1611EC2900BB571E /* UILoadingImageView.m */; }; D3128FE115AABC7E00A2147A /* ContactDetailsView.m in Sources */ = {isa = PBXBuildFile; fileRef = D3128FDF15AABC7E00A2147A /* ContactDetailsView.m */; }; @@ -1580,6 +1581,8 @@ 7066FC0B13E830E400EFC6DC /* libvpx.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libvpx.a; path = "liblinphone-sdk/apple-darwin/lib/libvpx.a"; sourceTree = ""; }; 70E542F213E147E3002BA2C0 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; 70E542F413E147EB002BA2C0 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; + 8C23BCB71D82AAC3005F19BB /* linphone.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = linphone.entitlements; sourceTree = ""; }; + 8CA2004B1D8158440095F859 /* PushKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PushKit.framework; path = System/Library/Frameworks/PushKit.framework; sourceTree = SDKROOT; }; C90FAA7615AF54E6002091CB /* HistoryDetailsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryDetailsView.h; sourceTree = ""; }; C90FAA7715AF54E6002091CB /* HistoryDetailsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryDetailsView.m; sourceTree = ""; }; C9B3A6FD15B485DB006F52EE /* Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Utils.h; path = Utils/Utils.h; sourceTree = ""; }; @@ -1784,6 +1787,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 8CA2004C1D8158440095F859 /* PushKit.framework in Frameworks */, 340751971506459A00B89C47 /* CoreTelephony.framework in Frameworks */, 637835A51CFD971D00599382 /* libbv16.a in Frameworks */, 63FD3F091CA17F9100E9AECC /* libmbedcrypto.a in Frameworks */, @@ -2153,6 +2157,7 @@ 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( + 8C23BCB71D82AAC3005F19BB /* linphone.entitlements */, 080E96DDFE201D6D7F000001 /* Classes */, 29B97323FDCFA39411CA2CEA /* Frameworks */, F0938158188E629800A55DFA /* iTunesArtwork */, @@ -2180,6 +2185,7 @@ 29B97323FDCFA39411CA2CEA /* Frameworks */ = { isa = PBXGroup; children = ( + 8CA2004B1D8158440095F859 /* PushKit.framework */, 570742661D5A63DB004B9C84 /* StoreKit.framework */, 636920661D253DF700D44CAC /* libbcunit.a */, 637835A41CFD971D00599382 /* libbv16.a */, @@ -4415,6 +4421,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_UNREACHABLE_CODE = NO; + CODE_SIGN_ENTITLEMENTS = linphone.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COMPRESS_PNG_FILES = NO; @@ -4503,7 +4510,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_UNREACHABLE_CODE = NO; - CODE_SIGN_ENTITLEMENTS = ""; + CODE_SIGN_ENTITLEMENTS = linphone.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COMPRESS_PNG_FILES = NO; @@ -4591,6 +4598,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_UNREACHABLE_CODE = NO; + CODE_SIGN_ENTITLEMENTS = linphone.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COMPRESS_PNG_FILES = NO; @@ -4678,7 +4686,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_UNREACHABLE_CODE = NO; - CODE_SIGN_ENTITLEMENTS = ""; + CODE_SIGN_ENTITLEMENTS = linphone.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COMPRESS_PNG_FILES = NO;