按需修改

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,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/ <br/>
Twitter :- https://twitter.com/Neutrinosco <br/>
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/)

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">
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-apns-push"
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

@ -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