From 268d3018c2b33aad3b225046ef8fd0faca6882cc Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 17 Dec 2019 13:56:36 +1100 Subject: [PATCH] Add function to send the device token to our server. Fetch data when receiving a silent notification. --- Signal.xcodeproj/project.pbxproj | 12 +++++ Signal/src/AppDelegate.m | 25 ++++++++++ .../PushNotificationManager.swift | 48 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Signal/src/Loki/PushNotification/PushNotificationManager.swift diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 96fb745af..acc727d87 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -519,6 +519,7 @@ 768A1A2B17FC9CD300E00ED8 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 768A1A2A17FC9CD300E00ED8 /* libz.dylib */; }; 76C87F19181EFCE600C4ACAB /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 76C87F18181EFCE600C4ACAB /* MediaPlayer.framework */; }; 76EB054018170B33006006FC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB03C318170B33006006FC /* AppDelegate.m */; }; + 7B53C4F0239E038C008DF635 /* PushNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B53C4EF239E038C008DF635 /* PushNotificationManager.swift */; }; 954AEE6A1DF33E01002E5410 /* ContactsPickerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954AEE681DF33D32002E5410 /* ContactsPickerTest.swift */; }; A10FDF79184FB4BB007FF963 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 76C87F18181EFCE600C4ACAB /* MediaPlayer.framework */; }; A11CD70D17FA230600A2D1B1 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A11CD70C17FA230600A2D1B1 /* QuartzCore.framework */; }; @@ -1309,6 +1310,7 @@ 76C87F18181EFCE600C4ACAB /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; }; 76EB03C218170B33006006FC /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 76EB03C318170B33006006FC /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + 7B53C4EF239E038C008DF635 /* PushNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushNotificationManager.swift; sourceTree = ""; }; 8981C8F64D94D3C52EB67A2C /* Pods-SignalTests.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalTests.test.xcconfig"; path = "Pods/Target Support Files/Pods-SignalTests/Pods-SignalTests.test.xcconfig"; sourceTree = ""; }; 8EEE74B0753448C085B48721 /* Pods-SignalMessaging.app store release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalMessaging.app store release.xcconfig"; path = "Pods/Target Support Files/Pods-SignalMessaging/Pods-SignalMessaging.app store release.xcconfig"; sourceTree = ""; }; 948239851C08032C842937CC /* Pods-SignalMessaging.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalMessaging.test.xcconfig"; path = "Pods/Target Support Files/Pods-SignalMessaging/Pods-SignalMessaging.test.xcconfig"; sourceTree = ""; }; @@ -2525,6 +2527,14 @@ path = views; sourceTree = ""; }; + 7B53C4EE239E0343008DF635 /* PushNotification */ = { + isa = PBXGroup; + children = ( + 7B53C4EF239E038C008DF635 /* PushNotificationManager.swift */, + ); + path = PushNotification; + sourceTree = ""; + }; 9404664EC513585B05DF1350 /* Pods */ = { isa = PBXGroup; children = ( @@ -2652,6 +2662,7 @@ B8439518228510E9000563FE /* Loki */ = { isa = PBXGroup; children = ( + 7B53C4EE239E0343008DF635 /* PushNotification */, B8BFFF392355426100102A27 /* Messaging */, B86BD0872339A1ED000F5AE3 /* Onboarding */, B86BD08223399ABF000F5AE3 /* Settings */, @@ -3899,6 +3910,7 @@ 34277A5E20751BDC006049F2 /* OWSQuotedMessageView.m in Sources */, 458DE9D61DEE3FD00071BB03 /* PeerConnectionClient.swift in Sources */, 45DDA6242090CEB500DE97F8 /* ConversationHeaderView.swift in Sources */, + 7B53C4F0239E038C008DF635 /* PushNotificationManager.swift in Sources */, 3488F9362191CC4000E524CC /* ConversationMediaView.swift in Sources */, 45F32C242057297A00A300D5 /* MessageDetailViewController.swift in Sources */, 3496955C219B605E00DCFE74 /* ImagePickerController.swift in Sources */, diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 7b8b344e6..9bf528156 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -592,6 +592,7 @@ static NSTimeInterval launchStartedAt; } OWSLogInfo(@"registered vanilla push token"); + [[LKPushNotificationManager sharedInstance] registerNotificationWithToken:deviceToken]; [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken]; } @@ -1094,6 +1095,17 @@ static NSTimeInterval launchStartedAt; OWSLogInfo(@"Ignoring remote notification; app not ready."); return; } + + //deal with remote notification + //fetch data + NSLog(@"Receive remote notification!"); + __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ + job = nil; + }).catch(^{ + job = nil; + }); + [job retainUntilComplete]; + } - (void)application:(UIApplication *)application @@ -1109,6 +1121,19 @@ static NSTimeInterval launchStartedAt; OWSLogInfo(@"Ignoring remote notification; app not ready."); return; } + + //deal with remote notification + //fetch data + NSLog(@"Receive remote notification!"); + __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ + completionHandler(UIBackgroundFetchResultNewData); + job = nil; + }).catch(^{ + completionHandler(UIBackgroundFetchResultFailed); + job = nil; + }); + [job retainUntilComplete]; + } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { diff --git a/Signal/src/Loki/PushNotification/PushNotificationManager.swift b/Signal/src/Loki/PushNotification/PushNotificationManager.swift new file mode 100644 index 000000000..085d44d93 --- /dev/null +++ b/Signal/src/Loki/PushNotification/PushNotificationManager.swift @@ -0,0 +1,48 @@ +// +// Copyright (c) 2018 Loki Messenger. All rights reserved. +// This file is for silent push notification +// Created by Ryan Zhao +// + +import UIKit + +@objc(LKPushNotificationManager) +class PushNotificationManager: NSObject { + + static let shared = PushNotificationManager() + + private override init() { + super.init() + } + + @objc + class func sharedInstance() -> PushNotificationManager { + return PushNotificationManager.shared + } + + @objc + func registerNotification(token: Data) { + let deviceToken = token.map { String(format: "%02.2hhx", $0) }.joined() + print("Device Token: (\(deviceToken))") + /** send token to Loki centralized server **/ + let parameters = [ "token" : deviceToken ] + let url = URL(string: "http://88.99.14.72:5000/register")! + let request = TSRequest(url: url, method: "POST", parameters: parameters) + request.allHTTPHeaderFields = [ "Content-Type" : "application/json"] + TSNetworkManager.shared().makeRequest( + request, + success: { (_, response: Any?) -> Void in + if let responseDictionary = response as? [String: Any] { + if responseDictionary["code"] as? Int == 0 { + print("[Loki] error occured during sending device token \(String(describing: responseDictionary["message"] as? String))") + } + } + }, + failure: { (_, error: Error?) -> Void in + print("[Loki] Couldn't send the device token to the centralized server") + }) + } + + // TODO: Move the fetch message fucntion here? + +}