OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
625 stars 214 forks source link

这个库挺坑的 #904

Closed RichDaddyCashMany closed 3 months ago

RichDaddyCashMany commented 5 months ago

How can we help?

iOS app后台进入前台的这个库居然会自动清除角标,真是无语,还好我是老iOS

WX20240619-161351@2x

Code of Conduct

RichDaddyCashMany commented 5 months ago
#import "OneSignalHook.h"
#import <objc/runtime.h>

@implementation OneSignalHook

+ (void)load {
    [OneSignalHook swizzleOneSignalLifecycleObserverMethod];
}

+ (void)swizzleOneSignalLifecycleObserverMethod {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = NSClassFromString(@"OneSignalLifecycleObserver");

        if (class) {
            SEL originalSelector = NSSelectorFromString(@"didBecomeActive");
            SEL swizzledSelector = @selector(swizzled_didBecomeActive);

            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);

            BOOL didAddMethod = class_addMethod(class,
                                                originalSelector,
                                                method_getImplementation(swizzledMethod),
                                                method_getTypeEncoding(swizzledMethod));

            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        } else {
            NSLog(@"OneSignalLifecycleObserver class not found");
        }
    });
}

- (void)swizzled_didBecomeActive {
    NSLog(@"-[OneSignalLifecycleObserver didBecomeActive] deny");

    SEL originalSelector = NSSelectorFromString(@"swizzled_didBecomeActive");
    if ([self respondsToSelector:originalSelector]) {
        ((void (*)(id, SEL))[self methodForSelector:originalSelector])(self, originalSelector);
    }
}

@end
nan-li commented 3 months ago

Hi @RichDaddyCashMany thank you for reaching out about this behavior.

I see you are asking about badge count being cleared and reset to zero when the app is foregrounded.

Actually, this is the default behavior. Please see our docs on iOS Badge Behavior.

If you want to prevent our SDK from automatically clearing badges set OneSignal_disable_badge_clearing to YES as a Boolean type in your .plist in Xcode.