Ver código fonte

原生推送

apple 6 anos atrás
pai
commit
fb02a1a745

+ 8 - 0
ios/JianYuIOS/JianYuIOS.xcodeproj/project.pbxproj

@@ -18,6 +18,8 @@
 		1880371F2193EB25006F9D7B /* JYMineViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 188037172193EB25006F9D7B /* JYMineViewController.m */; };
 		188037202193EB25006F9D7B /* JYSubViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1880371B2193EB25006F9D7B /* JYSubViewController.m */; };
 		188037212193EB25006F9D7B /* JYExpViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1880371C2193EB25006F9D7B /* JYExpViewController.m */; };
+		188A144723273CAD00D39E7E /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 188A144623273CAD00D39E7E /* UserNotifications.framework */; };
+		188A144923273CB400D39E7E /* UserNotificationsUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 188A144823273CB300D39E7E /* UserNotificationsUI.framework */; };
 		18C9D8E7219A5F9F001AC829 /* UITabBar+badge.m in Sources */ = {isa = PBXBuildFile; fileRef = 18C9D8E6219A5F9F001AC829 /* UITabBar+badge.m */; };
 		18E05CF922F11CDE001E5B44 /* README.txt in Resources */ = {isa = PBXBuildFile; fileRef = 18E05CF322F11CDD001E5B44 /* README.txt */; };
 		18E05CFA22F11CDE001E5B44 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 18E05CF522F11CDD001E5B44 /* libWeChatSDK.a */; };
@@ -98,6 +100,8 @@
 		1880371B2193EB25006F9D7B /* JYSubViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JYSubViewController.m; sourceTree = "<group>"; };
 		1880371C2193EB25006F9D7B /* JYExpViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JYExpViewController.m; sourceTree = "<group>"; };
 		1880371D2193EB25006F9D7B /* JYSearchViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JYSearchViewController.h; sourceTree = "<group>"; };
+		188A144623273CAD00D39E7E /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = System/Library/Frameworks/UserNotifications.framework; sourceTree = SDKROOT; };
+		188A144823273CB300D39E7E /* UserNotificationsUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotificationsUI.framework; path = System/Library/Frameworks/UserNotificationsUI.framework; sourceTree = SDKROOT; };
 		18B2DF4021ACDA5D00B5C93F /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
 		18C9D8E5219A5F9F001AC829 /* UITabBar+badge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UITabBar+badge.h"; sourceTree = "<group>"; };
 		18C9D8E6219A5F9F001AC829 /* UITabBar+badge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UITabBar+badge.m"; sourceTree = "<group>"; };
@@ -167,6 +171,8 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				188A144923273CB400D39E7E /* UserNotificationsUI.framework in Frameworks */,
+				188A144723273CAD00D39E7E /* UserNotifications.framework in Frameworks */,
 				18E05D1022F11D97001E5B44 /* Foundation.framework in Frameworks */,
 				18E05D0E22F11D8D001E5B44 /* CFNetwork.framework in Frameworks */,
 				18E05D0C22F11D86001E5B44 /* UIKit.framework in Frameworks */,
@@ -261,6 +267,8 @@
 		954256EB2ECEF2507E4350ED /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				188A144823273CB300D39E7E /* UserNotificationsUI.framework */,
+				188A144623273CAD00D39E7E /* UserNotifications.framework */,
 				18E05D0F22F11D97001E5B44 /* Foundation.framework */,
 				18E05D0D22F11D8D001E5B44 /* CFNetwork.framework */,
 				18E05D0B22F11D86001E5B44 /* UIKit.framework */,

BIN
ios/JianYuIOS/JianYuIOS.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate


+ 82 - 21
ios/JianYuIOS/JianYuIOS/AppDelegate.m

@@ -20,7 +20,7 @@
 #import "JYTabBarController.h"
 
 
-@interface AppDelegate ()<JPUSHRegisterDelegate,WXApiDelegate>
+@interface AppDelegate ()<JPUSHRegisterDelegate,WXApiDelegate,UNUserNotificationCenterDelegate>
 
 @property(nonatomic, strong)UIImageView *imgView;
 @property(nonatomic, assign)BOOL wakeUpBool;
@@ -42,8 +42,11 @@
     [LXDataBaseManager shareDataBaseManager];//初始化单例
     [LXViewControllerManager shareViewControllerManager];
     
-    [self initJPush:launchOptions];
+//    [self initJPush:launchOptions];
     [self initUM];
+    [self registeredPush]; //注册原生推送
+    
+    
     
     
     
@@ -62,27 +65,15 @@
     return YES;
 }
 
+
 -(void)initJPush:(NSDictionary *)launchOptions{
     JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
     entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
     if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
-        // 可以添加自定义categories
-        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
-        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
-        //        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
-        //        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
+    
     }
     [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
-    
-    // Optional
-    // 获取IDFA
-    // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
-    //    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
-    
-    // Required
-    // init Push
-    // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
-    // 如需继续使用pushConfig.pinfo文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
+  
     [JPUSHService setupWithOption:launchOptions appKey:@"5efa1257867cf5d77d007ce6"
                           channel:@"APP Store"
                  apsForProduction:1
@@ -126,6 +117,68 @@
 
 
 
+#pragma mark 原生推送
+-(void)registeredPush
+{
+    if (@available(iOS 10.0, *)) {
+        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+        center.delegate = self;
+        // 小角标、声音、弹窗
+        if (@available(iOS 10.0, *)) {
+            [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
+                if (!error) {
+                    NSLog(@"request authorization succeeded!");
+                }
+            }];
+        } else {
+            // Fallback on earlier versions
+        }
+        
+        [[UIApplication sharedApplication] registerForRemoteNotifications];
+        
+    }  else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
+        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
+        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
+        [[UIApplication sharedApplication] registerForRemoteNotifications];
+        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
+    }else {
+        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeBadge);
+        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
+    }
+}
+
+
+//  iOS 10: App在前台获取到通知
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
+    
+    NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);
+    
+    // 处理 推送数据
+    NSDictionary *contentDic=notification.request.content.userInfo;
+//    [self handleDic:contentDic];
+    
+    // 根据APP需要,判断是否要提示用户Badge、Sound、Alert
+    if (@available(iOS 10.0, *)) {
+        completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
+    } else {
+        // Fallback on earlier versions
+    }
+}
+//  iOS 10: 点击通知进入App时触发
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
+    NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
+    // 处理 推送数据
+    NSDictionary *contentDic=response.notification.request.content.userInfo;
+//    [self handleDic:contentDic];
+    completionHandler();
+}
+
+
+
+
+
+
+
 
 
 
@@ -171,12 +224,20 @@
 didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
     
     /// Required - 注册 DeviceToken
-    [JPUSHService registerDeviceToken:deviceToken];
+//    [JPUSHService registerDeviceToken:deviceToken];
+    
+    // 收到token
+    NSString *token =
+    [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
+    // 保存到后台-用于推送
+    NSLog(@"%@",token)
+    
+
 }
 
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
     //Optional
-    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
+   NSLog(@"注册APNS失败:%@",error);
 }
 
 // iOS 10 Support
@@ -268,7 +329,7 @@ didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
 }
 
 
-
+// 收到推送时调用(iOS7-iOS9)
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
     NSLog(@"111");
     NSLog(@"%@",userInfo);
@@ -332,7 +393,7 @@ didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
     completionHandler(UIBackgroundFetchResultNewData);
 }
 
-
+// 收到推送时调用(iOS6及以下)
 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
     // App 收到推送的通知
     NSLog(@"********** ios7.0之前 **********");