From a2dfda62b35dfc223d91612787b89860b528401e Mon Sep 17 00:00:00 2001 From: wuxiang Date: Sat, 15 Jul 2023 20:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E9=9C=80=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++- plugin.xml | 6 +- src/ios/APNSPushPlugin.m | 2 +- src/ios/AppDelegate+apnsnotification.h | 5 +- src/ios/AppDelegate+apnsnotification.m | 105 ++++++++++++------------- 5 files changed, 64 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index b4c1e3b..9b9c3e1 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,20 @@ # cordova-plugin-apns-push +> 从 https://github.com/NeutrinosPlatform/cordova-plugin-apns-push 克隆的。 +> 主要修改: +> 1. 调整了 app 在活动/非活动状态的逻辑,在活动状态时是不会弹出提醒的。 +> 2. 需要在发消息时指定 "content-available": 1,这样才能接收到数据。但是在试用 https://github.com/PerfectExamples/Perfect-APNS-Demo 时,并不需要这个参数。还是有很多东西没搞明白。 +> 3. 如果想在活动状态弹出提醒,需要配合 cordova-plugin-local-notifications 插件。 + > Register and receive APNS push notifications # Install -Install via npm :- `cordova plugin add cordova-plugin-apns-push` +`cordova plugin add git+https://m.shuto.cn:8681/public/cordova-plugin-apns-push` # Usage -```js +```js var push = window['APNSPushNotification'].init({ ios: { @@ -41,7 +47,7 @@ Install via npm :- `cordova plugin add cordova-plugin-apns-push` ``` # Supported Platforms - + - iOS # More about us @@ -52,5 +58,5 @@ LinkedIn :- https://www.linkedin.com/company/25057297/
Twitter :- https://twitter.com/Neutrinosco
Instagram :- https://www.instagram.com/neutrinos.co/ -[![N|Solid](https://image4.owler.com/logo/neutrinos_owler_20171023_142541_original.jpg "Neutrinos")](http://www.neutrinos.co/) +[![N|Solid](https://image4.owler.com/logo/neutrinos_owler_20171023_142541_original.jpg "Neutrinos")](http://www.neutrinos.co/) diff --git a/plugin.xml b/plugin.xml index f4c2863..f4066bb 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ - + APNSPushPlugin This plugin allows your application to receive apple push notifications (APNS) on iOS devices. diff --git a/src/ios/APNSPushPlugin.m b/src/ios/APNSPushPlugin.m index 90cb321..b1fcb7f 100644 --- a/src/ios/APNSPushPlugin.m +++ b/src/ios/APNSPushPlugin.m @@ -428,7 +428,7 @@ } else if ([key isEqualToString:@"title"]) { [message setObject:value forKey:@"title"]; } else if ([key isEqualToString:@"badge"]) { - [message setObject:value forKey:@"count"]; + [message setObject:value forKey:@"badge"]; } else if ([key isEqualToString:@"sound"]) { [message setObject:value forKey:@"sound"]; } else if ([key isEqualToString:@"image"]) { diff --git a/src/ios/AppDelegate+apnsnotification.h b/src/ios/AppDelegate+apnsnotification.h index 9479b18..f287281 100644 --- a/src/ios/AppDelegate+apnsnotification.h +++ b/src/ios/AppDelegate+apnsnotification.h @@ -11,10 +11,7 @@ extern NSString *const pushPluginApplicationDidBecomeActiveNotification; -@interface AppDelegate (notification) -- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; -- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; -- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler; +@interface AppDelegate (notification) - (void)pushPluginOnApplicationDidBecomeActive:(UIApplication *)application; - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler; - (id) getCommandInstance:(NSString*)className; diff --git a/src/ios/AppDelegate+apnsnotification.m b/src/ios/AppDelegate+apnsnotification.m index 9d34b5f..de993c7 100644 --- a/src/ios/AppDelegate+apnsnotification.m +++ b/src/ios/AppDelegate+apnsnotification.m @@ -15,7 +15,7 @@ static char coldstartKey; NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginApplicationDidBecomeActiveNotification"; -@implementation AppDelegate (notification) +@implementation AppDelegate (notification) - (id) getCommandInstance:(NSString*)className { @@ -55,8 +55,9 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA - (AppDelegate *)pushPluginSwizzledInit { - UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; - center.delegate = self; +// UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; +// center.delegate = self; +// [UIApplication sharedApplication].delegate = self; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushPluginOnApplicationDidBecomeActive:) @@ -82,55 +83,43 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA NSLog(@"didReceiveNotification with fetchCompletionHandler"); // app is in the background or inactive, so only call notification callback if this is a silent push - if (application.applicationState != UIApplicationStateActive) { + if (application.applicationState == UIApplicationStateActive) { - NSLog(@"app in-active"); + NSLog(@"app active"); // do some convoluted logic to find out if this should be a silent push. - long silent = 0; id aps = [userInfo objectForKey:@"aps"]; id contentAvailable = [aps objectForKey:@"content-available"]; - if ([contentAvailable isKindOfClass:[NSString class]] && [contentAvailable isEqualToString:@"1"]) { - silent = 1; - } else if ([contentAvailable isKindOfClass:[NSNumber class]]) { - silent = [contentAvailable integerValue]; + NSLog(@"this should be a silent push"); + void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){ + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(result); + }); + }; + + PushPlugin *pushHandler = [self getCommandInstance:@"APNSPushNotification"]; + + if (pushHandler.handlerObj == nil) { + pushHandler.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2]; } - if (silent == 1) { - NSLog(@"this should be a silent push"); - void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){ - dispatch_async(dispatch_get_main_queue(), ^{ - completionHandler(result); - }); - }; - - PushPlugin *pushHandler = [self getCommandInstance:@"APNSPushNotification"]; - - if (pushHandler.handlerObj == nil) { - pushHandler.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2]; - } - - id notId = [userInfo objectForKey:@"notId"]; - if (notId != nil) { - NSLog(@"Push Plugin notId %@", notId); - [pushHandler.handlerObj setObject:safeHandler forKey:notId]; - } else { - NSLog(@"Push Plugin notId handler"); - [pushHandler.handlerObj setObject:safeHandler forKey:@"handler"]; - } - - pushHandler.notificationMessage = userInfo; - pushHandler.isInline = NO; - [pushHandler notificationReceived]; + id notId = [userInfo objectForKey:@"notId"]; + if (notId != nil) { + NSLog(@"Push Plugin notId %@", notId); + [pushHandler.handlerObj setObject:safeHandler forKey:notId]; } else { - NSLog(@"just put it in the shade"); - //save it for later - self.launchNotification = userInfo; - completionHandler(UIBackgroundFetchResultNewData); + NSLog(@"Push Plugin notId handler"); + [pushHandler.handlerObj setObject:safeHandler forKey:@"handler"]; } - } else { + pushHandler.notificationMessage = userInfo; + pushHandler.isInline = YES; + [pushHandler notificationReceived]; completionHandler(UIBackgroundFetchResultNoData); + } else { + self.launchNotification = userInfo; + completionHandler(UIBackgroundFetchResultNewData); +// completionHandler(UIBackgroundFetchResultNoData); } } @@ -147,14 +136,18 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA case UNAuthorizationStatusAuthorized: completionHandler(YES); break; - } + case UNAuthorizationStatusProvisional: + break; + case UNAuthorizationStatusEphemeral: + break; + } }]; } - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { NSLog(@"active"); - + NSString *firstLaunchKey = @"firstLaunchKey"; NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"phonegap-plugin-push"]; if (![defaults boolForKey:firstLaunchKey]) { @@ -186,19 +179,19 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA [[NSNotificationCenter defaultCenter] postNotificationName:pushPluginApplicationDidBecomeActiveNotification object:nil]; } -- (void)userNotificationCenter:(UNUserNotificationCenter *)center - willPresentNotification:(UNNotification *)notification - withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler -{ - NSLog( @"NotificationCenter Handle push from foreground" ); - // custom code to handle push while app is in the foreground - PushPlugin *pushHandler = [self getCommandInstance:@"APNSPushNotification"]; - pushHandler.notificationMessage = notification.request.content.userInfo; - pushHandler.isInline = YES; - [pushHandler notificationReceived]; - - completionHandler(UNNotificationPresentationOptionNone); -} +//- (void)userNotificationCenter:(UNUserNotificationCenter *)center +// willPresentNotification:(UNNotification *)notification +// withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler +//{ +// NSLog( @"NotificationCenter Handle push from foreground" ); +// // custom code to handle push while app is in the foreground +// PushPlugin *pushHandler = [self getCommandInstance:@"APNSPushNotification"]; +// pushHandler.notificationMessage = notification.request.content.userInfo; +// pushHandler.isInline = YES; +// [pushHandler notificationReceived]; +// +// completionHandler(UNNotificationPresentationOptionNone); +//} - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response