Browse Source

Push Notification

Marino Faggiana 6 năm trước cách đây
mục cha
commit
8202e0aff9

+ 16 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -407,6 +407,8 @@
 		F7B2DEF11F976859007CF4D2 /* english.txt in Resources */ = {isa = PBXBuildFile; fileRef = F7B2DEED1F976785007CF4D2 /* english.txt */; };
 		F7B3A4EE1E97818A000DACE8 /* CCLoginWeb.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B3A4ED1E97818A000DACE8 /* CCLoginWeb.swift */; };
 		F7B4F1CB1F44356F00B53B42 /* NCUchardet.m in Sources */ = {isa = PBXBuildFile; fileRef = F7B4F1C81F44356F00B53B42 /* NCUchardet.m */; };
+		F7B7ED3F211340EB00C89FFA /* NCPushNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = F7B7ED3E211340EB00C89FFA /* NCPushNotification.m */; };
+		F7B7ED40211340EB00C89FFA /* NCPushNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = F7B7ED3E211340EB00C89FFA /* NCPushNotification.m */; };
 		F7BAADC81ED5A87C00B7EAD4 /* NCDatabase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7BAADB41ED5A87C00B7EAD4 /* NCDatabase.swift */; };
 		F7BAADC91ED5A87C00B7EAD4 /* NCDatabase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7BAADB41ED5A87C00B7EAD4 /* NCDatabase.swift */; };
 		F7BAADCB1ED5A87C00B7EAD4 /* NCManageDatabase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7BAADB51ED5A87C00B7EAD4 /* NCManageDatabase.swift */; };
@@ -1356,6 +1358,8 @@
 		F7B4F1C81F44356F00B53B42 /* NCUchardet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCUchardet.m; sourceTree = "<group>"; };
 		F7B7ED3A21133A4300C89FFA /* Notification_Service_Extension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Notification_Service_Extension.entitlements; sourceTree = "<group>"; };
 		F7B7ED3B21133A6000C89FFA /* Notification_Service_Extension.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Notification_Service_Extension.plist; sourceTree = "<group>"; };
+		F7B7ED3D211340EB00C89FFA /* NCPushNotification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NCPushNotification.h; sourceTree = "<group>"; };
+		F7B7ED3E211340EB00C89FFA /* NCPushNotification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCPushNotification.m; sourceTree = "<group>"; };
 		F7BAADB41ED5A87C00B7EAD4 /* NCDatabase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCDatabase.swift; sourceTree = "<group>"; };
 		F7BAADB51ED5A87C00B7EAD4 /* NCManageDatabase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCManageDatabase.swift; sourceTree = "<group>"; };
 		F7BB04821FD58ACA00BBFD2A /* cs-CZ */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "cs-CZ"; path = "cs-CZ.lproj/CTAssetsPicker.strings"; sourceTree = "<group>"; };
@@ -2708,6 +2712,15 @@
 			path = NCUchardet;
 			sourceTree = "<group>";
 		};
+		F7B7ED3C211340EB00C89FFA /* PushNotification */ = {
+			isa = PBXGroup;
+			children = (
+				F7B7ED3D211340EB00C89FFA /* NCPushNotification.h */,
+				F7B7ED3E211340EB00C89FFA /* NCPushNotification.m */,
+			);
+			path = PushNotification;
+			sourceTree = "<group>";
+		};
 		F7BAAD951ED5A63D00B7EAD4 /* Database */ = {
 			isa = PBXGroup;
 			children = (
@@ -3179,6 +3192,7 @@
 				F74D3DB81BAC1941000BAE4B /* Networking */,
 				F7C5259A1E3B441D00FFE02C /* Notification */,
 				F7FCFFD51D70798C000E6E29 /* PeekPop */,
+				F7B7ED3C211340EB00C89FFA /* PushNotification */,
 				F7BE6E2A1D2D5C3B00106933 /* QuickActions */,
 				F7FE125B1BAC03FB0041924B /* Security */,
 				F7ACE4281BAC0268006C0017 /* Settings */,
@@ -3739,6 +3753,7 @@
 				F72D101B210B74CA009C96B7 /* UICKeyChainStore.m in Sources */,
 				F72D103F210B77B8009C96B7 /* NCPushNotificationEncryption.m in Sources */,
 				F72D103E210B77A5009C96B7 /* OCActivity.m in Sources */,
+				F7B7ED40211340EB00C89FFA /* NCPushNotification.m in Sources */,
 				F72D1042210B783D009C96B7 /* NCEndToEndEncryption.m in Sources */,
 				F72D103B210B76A1009C96B7 /* NCManageDatabase.swift in Sources */,
 			);
@@ -4081,6 +4096,7 @@
 				F73CC06C1E813DFF006E3047 /* BKPasscodeField.m in Sources */,
 				F77B0ED51D118A16002130FE /* PHAsset+Utility.m in Sources */,
 				F73B4EFD1F470D9100BBEE4B /* LangHebrewModel.cpp in Sources */,
+				F7B7ED3F211340EB00C89FFA /* NCPushNotification.m in Sources */,
 				F70022CE1EC4C9100080073F /* OCShareUser.m in Sources */,
 				F77B0ED91D118A16002130FE /* main.m in Sources */,
 			);

+ 6 - 1
Notification Service Extension/NotificationService.swift

@@ -41,11 +41,16 @@ class NotificationService: UNNotificationServiceExtension {
             let message = bestAttemptContent.userInfo["subject"] as! String
             
             guard let decryptedMessage = NCPushNotificationEncryption.sharedInstance().decryptPushNotification(message, withDevicePrivateKey: CCUtility.getPushNotificationPrivateKey()) else {
+                
                 contentHandler(bestAttemptContent)
                 return
             }
             
-            bestAttemptContent.body = decryptedMessage
+            let pushNotification = NCPushNotification.init(fromDecryptedString: decryptedMessage)
+            if (pushNotification != nil) {
+                bestAttemptContent.title = ""
+                bestAttemptContent.body = pushNotification!.bodyForRemoteAlerts()
+            }
             
             contentHandler(bestAttemptContent)
         }

+ 1 - 0
Notification Service Extension/NotificationServiceExtension-Bridging-Header.h

@@ -23,6 +23,7 @@
 
 #import "CCUtility.h"
 #import "NCPushNotificationEncryption.h"
+#import "NCPushNotification.h"
 
 #import "OCActivity.h"
 #import "OCUserProfile.h"

+ 16 - 0
iOSClient/AppDelegate.m

@@ -38,6 +38,7 @@
 #import "NCAutoUpload.h"
 #import "Firebase.h"
 #import "NCPushNotificationEncryption.h"
+#import "NCPushNotification.h"
 
 @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
 
@@ -435,6 +436,21 @@
         NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:[NCPushNotificationEncryption sharedInstance].ncPNPrivateKey];
         if (decryptedMessage) {
            
+            NCPushNotification *pushNotification = [NCPushNotification pushNotificationFromDecryptedString:decryptedMessage];
+            if (pushNotification) {
+                switch (pushNotification.type) {
+                    case NCPushNotificationTypeComment:
+                    {
+                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[pushNotification bodyForRemoteAlerts] message:nil preferredStyle:UIAlertControllerStyleAlert];
+                        UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
+                        
+                        [alertController addAction:okAction];
+                        [self.activeMain presentViewController:alertController animated:YES completion:nil];
+                    }
+                    default:
+                        break;
+                }
+            }
         }
     }
     completionHandler();

+ 41 - 0
iOSClient/PushNotification/NCPushNotification.h

@@ -0,0 +1,41 @@
+//
+//  NCPushNotification.h
+//  VideoCalls
+//
+//  Created by Ivan Sein on 24.11.17.
+//  Copyright © 2017 struktur AG. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+typedef NS_ENUM(NSInteger, NCPushNotificationType) {
+    NCPushNotificationTypeUnknown,
+    NCPushNotificationTypeCall,
+    NCPushNotificationTypeRoom,
+    NCPushNotificationTypeChat,
+    NCPushNotificationTypeComment
+};
+
+extern NSString * const kNCPNAppKey;
+extern NSString * const kNCPNTypeKey;
+extern NSString * const kNCPNSubjectKey;
+extern NSString * const kNCPNIdKey;
+extern NSString * const kNCPNTypeCallKey;
+extern NSString * const kNCPNTypeRoomKey;
+extern NSString * const kNCPNTypeChatKey;
+
+extern NSString * const NCPushNotificationJoinChatNotification;
+extern NSString * const NCPushNotificationJoinAudioCallAcceptedNotification;
+extern NSString * const NCPushNotificationJoinVideoCallAcceptedNotification;
+
+@interface NCPushNotification : NSObject
+
+@property (nonatomic, copy) NSString *app;
+@property (nonatomic, assign) NCPushNotificationType type;
+@property (nonatomic, copy) NSString *subject;
+@property (nonatomic, assign) NSInteger pnId;
+
++ (instancetype)pushNotificationFromDecryptedString:(NSString *)decryptedString;
+- (NSString *)bodyForRemoteAlerts;
+
+@end

+ 84 - 0
iOSClient/PushNotification/NCPushNotification.m

@@ -0,0 +1,84 @@
+//
+//  NCPushNotification.m
+//  VideoCalls
+//
+//  Created by Ivan Sein on 24.11.17.
+//  Copyright © 2017 struktur AG. All rights reserved.
+//
+
+#import "NCPushNotification.h"
+
+@implementation NCPushNotification
+
+NSString * const kNCPNAppKey            = @"app";
+NSString * const kNCPNAppIdComments     = @"comments";
+NSString * const kNCPNTypeKey           = @"type";
+NSString * const kNCPNSubjectKey        = @"subject";
+NSString * const kNCPNIdKey             = @"id";
+NSString * const kNCPNTypeCallKey       = @"call";
+NSString * const kNCPNTypeRoomKey       = @"room";
+NSString * const kNCPNTypeChatKey       = @"chat";
+NSString * const kNCPNTypeCommentKey    = @"comment";
+
+NSString * const NCPushNotificationJoinChatNotification                 = @"NCPushNotificationJoinChatNotification";
+NSString * const NCPushNotificationJoinAudioCallAcceptedNotification    = @"NCPushNotificationJoinAudioCallAcceptedNotification";
+NSString * const NCPushNotificationJoinVideoCallAcceptedNotification    = @"NCPushNotificationJoinVideoCallAcceptedNotification";
+
+
++ (instancetype)pushNotificationFromDecryptedString:(NSString *)decryptedString
+{
+    if (!decryptedString) {
+        return nil;
+    }
+    
+    NSData *data = [decryptedString dataUsingEncoding:NSUTF8StringEncoding];
+    id jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
+    
+    NSString *app = [jsonDict objectForKey:kNCPNAppKey];
+    if (![app isEqualToString:kNCPNAppIdComments]) {
+        return nil;
+    }
+    
+    NCPushNotification *pushNotification = [[NCPushNotification alloc] init];
+    pushNotification.app = app;
+    pushNotification.subject = [jsonDict objectForKey:kNCPNSubjectKey];
+    pushNotification.pnId = [[jsonDict objectForKey:kNCPNIdKey] integerValue];
+    
+    NSString *type = [jsonDict objectForKey:kNCPNTypeKey];
+    pushNotification.type = NCPushNotificationTypeUnknown;
+    
+    if ([type isEqualToString:kNCPNTypeCallKey]) {
+        pushNotification.type = NCPushNotificationTypeCall;
+    } else if ([type isEqualToString:kNCPNTypeRoomKey]) {
+        pushNotification.type = NCPushNotificationTypeRoom;
+    } else if ([type isEqualToString:kNCPNTypeChatKey]) {
+        pushNotification.type = NCPushNotificationTypeChat;
+    } else if ([type isEqualToString:kNCPNTypeCommentKey]) {
+        pushNotification.type = NCPushNotificationTypeComment;
+    }
+    
+    return pushNotification;
+}
+
+- (NSString *)bodyForRemoteAlerts
+{
+    switch (_type) {
+        case NCPushNotificationTypeCall:
+            return [NSString stringWithFormat:@"📞 %@", _subject];
+            break;
+        case NCPushNotificationTypeRoom:
+            return [NSString stringWithFormat:@"🔔 %@", _subject];
+            break;
+        case NCPushNotificationTypeChat:
+            return [NSString stringWithFormat:@"💬 %@", _subject];
+            break;
+        case NCPushNotificationTypeComment:
+            return [NSString stringWithFormat:@"💬 %@", _subject];
+            break;
+        case NCPushNotificationTypeUnknown:
+            return _subject;
+            break;
+    }
+}
+
+@end