按需修改

This commit is contained in:
巫翔 2023-07-15 20:04:18 +08:00
parent 69312b2402
commit a2dfda62b3
5 changed files with 64 additions and 68 deletions

View File

@ -1,10 +1,16 @@
# 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

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-apns-push"
version="1.0.1">
version="1.1.1">
<name>APNSPushPlugin</name>
<description> This plugin allows your application to receive apple push notifications (APNS) on iOS devices. </description>

View File

@ -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"]) {

View File

@ -11,10 +11,7 @@
extern NSString *const pushPluginApplicationDidBecomeActiveNotification;
@interface AppDelegate (notification) <UNUserNotificationCenterDelegate>
- (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) <UNUserNotificationCenterDelegate, UIApplicationDelegate>
- (void)pushPluginOnApplicationDidBecomeActive:(UIApplication *)application;
- (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler;
- (id) getCommandInstance:(NSString*)className;

View File

@ -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,21 +83,13 @@ 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];
}
if (silent == 1) {
NSLog(@"this should be a silent push");
void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){
dispatch_async(dispatch_get_main_queue(), ^{
@ -120,17 +113,13 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA
}
pushHandler.notificationMessage = userInfo;
pushHandler.isInline = NO;
pushHandler.isInline = YES;
[pushHandler notificationReceived];
completionHandler(UIBackgroundFetchResultNoData);
} else {
NSLog(@"just put it in the shade");
//save it for later
self.launchNotification = userInfo;
completionHandler(UIBackgroundFetchResultNewData);
}
} else {
completionHandler(UIBackgroundFetchResultNoData);
// completionHandler(UIBackgroundFetchResultNoData);
}
}
@ -147,6 +136,10 @@ NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginA
case UNAuthorizationStatusAuthorized:
completionHandler(YES);
break;
case UNAuthorizationStatusProvisional:
break;
case UNAuthorizationStatusEphemeral:
break;
}
}];
}
@ -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