• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS 10/9 推送通知

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:34:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要更新我们的应用程序以支持 iOS 9 和 iOS 10,所以我的问题是使用 UNUserNotificationCenter 进行推送通知。

因此,在 iOS 9 中,我们有一个方法可以返回 UIUserNotificationSettings 的结果,例如

- (BOOL)alertEnabled {
    UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    return [theSettings types] & UIUserNotificationTypeAlert;
}

在 iOS 10 中,我做了类似的事情

- (void)userNotificationsAuthorization void (^)(BOOL alertIsActive))completion {
    [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        completion(settings.alertSetting == UNNotificationSettingEnabled);
    }];
}

调用它并通过完成处理程序获取它。

我的问题:是否有可能使用 getNotificationSettingsWithCompletionHandlerreturn 值而不是完成处理程序,我可以将它用于我的 alertEnabled 方法?

非常感谢。

更新:

通过这种方法,它正在工作

- (BOOL)alertIsEnabled {
    __block BOOL alertIsActive = NO;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);

        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            alertIsActive = settings.alertSetting == UNNotificationSettingEnabled;
            dispatch_semaphore_signal(sem);
        }];

        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    }
    else {
        UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        alertIsActive = [theSettings types] & UIUserNotificationTypeAlert;    
    }
    return alertIsActive;
}

但也许有更好的解决方案



Best Answer-推荐答案


所以,经过一周的测试,它对我来说效果很好。

针对我的具体问题,我创建了一个自定义类。

SpecicPush.h

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger , PushNotificationType) {
    PushNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    PushNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received
    PushNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received
    PushNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being received
};

@interface SpecificPush : NSObject

@property (nonatomic, readonly) PushNotificationType currentNotificationSettings;

+ (SpecificPush *)sharedInstance;
- (PushNotificationType)types;

@end

SpecificPush.m

#import "SpecificPush.h"
#import <UserNotifications/UserNotifications.h>

@interface SpecificPush()
@property (nonatomic) PushNotificationType currentNotificationSettings;
@end

@implementation SpecificPush

#pragma mark - Init

static SpecificPush *instance = nil;
+ (SpecificPush *)sharedInstance
{
    @synchronized (self)
    {
        if (instance == nil)
        {
            [SpecificPush new];
        }
    }
    return instance;
}

- (instancetype)init
{
    NSAssert(!instance, @"WARNING - Instance of SpecifishPush already exists");
    self = [super init];
    if (self)
    {
        self.currentNotificationSettings = PushNotificationTypeNone;
    }

    instance = self;
    return self;
}

- (PushNotificationType)types
{
    if (IS_IOS10)
    {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
            if ((settings.soundSetting == UNNotificationSettingDisabled) && (settings.alertSetting == UNNotificationSettingDisabled) && (settings.soundSetting == UNNotificationSettingDisabled))
            {
                self.currentNotificationSettings = PushNotificationTypeNone;
            }
            if (settings.badgeSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeBadge;
            }
            if (settings.soundSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeSound;
            }

            if (settings.alertStyle == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeAlert;
            }

            dispatch_semaphore_signal(semaphore);

        }];

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_release(semaphore);
    }
    else
    {
        UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (settings.types == UIUserNotificationTypeNone)
        {
            self.currentNotificationSettings = PushNotificationTypeNone;
        }
        if (settings.types & UIUserNotificationTypeBadge)
        {
            self.currentNotificationSettings = PushNotificationTypeBadge;
        }
        if (settings.types & UIUserNotificationTypeSound)
        {
            self.currentNotificationSettings = PushNotificationTypeSound;
        }
        if (settings.types & UIUserNotificationTypeAlert)
        {
            self.currentNotificationSettings = PushNotificationTypeAlert;
        }
    }

    return self.currentNotificationSettings;
}
@end

我使用了与 UIUserNotificationType 相同的 NS_ENUM。现在我可以轻松使用旧的实现了。

- (BOOL)alertEnabled {
    UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    return [theSettings types] & UIUserNotificationTypeAlert;
}

我用

- (BOOL)alertEnabled {
    return [[SpecificPush sharedInstance] types] & PushNotificationTypeAlert;
}

关于iOS 10/9 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41564543/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap