IOS 9 不请求远程通知权限

标签 ios objective-c xcode remote-notifications

我在尝试请求远程通知权限时遇到问题。

它在 iOS 10 上完美运行,但当我尝试在 iOS 9 设备上运行时,它没有显示任何警报,并且 UIApplication 委托(delegate)方法“application:didRegisterForRemoteNotificationsWithDeviceToken:”未被调用。既不是“失败”的方法。

我只在真实设备上测试,而不是模拟器。我目前用于请求许可的代码如下:

-(void)requestPushPermissions {
NSLog(@"Starting register for remote Notification");
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"Got a yes!");
        }
        else {
            NSLog(@"Got a no...");
        }
    }];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
}

有人知道吗?

最佳答案

试试这段代码

#import "AppDelegate.h"

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self registerForRemoteNotification];

    return YES;
}
    - (void)registerForRemoteNotification {
        if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
                if( !error ){
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                }
            }];
        }
        else {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }

在功能中启用推送通知

关于IOS 9 不请求远程通知权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715368/

相关文章:

objective-c - 委托(delegate)和 IBAction

objective-c - applicationWillTerminate 不会在强制退出 iOS 应用程序时被调用

iOS- MKMapKit-如何在用户位置上放置注释?

objective-c - 如何在 Cocoa 应用程序中将 NSView 添加到 NSWindow?

ios - 使用 URL 方案启动 Podcast 应用程序

swift - Xcode 14 在通过 Interface Builder 访问时无法定位项目类

iphone - 如何在 objective-c 中按范围读取大文件?

ios - 位置管理器为旧位置提供新时间戳

ios - 在应用程序中使用多个 UISplitViewController

同名的 Objective-C 类别