【问题标题】:IOS 9 not asking permission for remote notificationsIOS 9 不请求远程通知的权限
【发布时间】:2017-07-31 14:53:05
【问题描述】:

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

它在 iOS 10 上完美运行,但是当我尝试在 iOS 9 设备上执行此操作时,它没有显示任何警报,并且 UIApplication 委托方法“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];
}
}

有人知道吗?

【问题讨论】:

  • 您在使用设备还是模拟器?请记住,通知在模拟器上效果不佳
  • @RicardoAlves 正如我在问题中所写的那样;我只在真实设备上测试,而不是模拟器。无论如何,谢谢。

标签: ios objective-c xcode remote-notifications


【解决方案1】:

试试这个代码

#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];
        }
    }

在功能中启用推送通知

【讨论】:

  • center requestAuthorizationWithOptions: 块可以在非主线程中调用 if( !error ){dispatch_async(dispatch_get_main_queue(), ^{ [app registerForRemoteNotifications]; });}
猜你喜欢
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 2016-01-05
  • 2016-04-10
  • 1970-01-01
  • 2017-11-02
相关资源
最近更新 更多