首页 文章

在发出本地通知时询问用户是否允许显示警报

提问于
浏览
13

我希望在本地通知被触发时显示警告,但为此我必须请求许可,因为当我在iPhone上运行应用程序时,它对我说:

尝试安排本地通知{开火日期= 2014年6月13日星期五12小时10分27秒中欧夏令时,时区=(null),重复间隔= 0,重复计数= UILocalNotificationInfiniteRepeatCount,下次开火日期= 6月13日星期五2014 12 h 10 min 27 s中欧夏令时,用户信息=(null)}带有提醒但尚未收到用户显示提醒的权限

我怎样才能做到这一点?这是现在的代码:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:timeUntilNotification];
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.alertBody = @"ZEIT!";
    localNotif.alertAction = @"Show me the Timer!";
    localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

5 回答

  • 4

    添加此代码后,它将显示警报视图以询问用户是否允许 .

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
                                                                                                              categories:nil]];
    }
    

    您可以在应用程序中添加此代码:didFinishLaunchingWithOptions;方法,以便应用程序在启动应用程序时询问您的用户,或者您可以在设置本地通知时添加此代码,这取决于您 .

  • 1

    苏健豪的回答很好 .

    在Swift中它看起来像这样:

    let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:")
    
    if registerUserNotificationSettings { 
        var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound     
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotific‌​ationSettings(forTypes: types, categories: nil))
    }
    

    另见:Ask for User Permission to Receive UILocalNotifications in iOS 8

  • 40
    //register notifications
    if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) //ios 8+
    {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
        [application registerForRemoteNotifications];
    }
    else // ios 7 or less
    {
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
    }
    
  • 0

    用迅捷的语言....

    var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
    var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
    UIApplication.sharedApplication().registerUserNotificationSettings(setting);
    UIApplication.sharedApplication().registerForRemoteNotifications();
    
  • 1

    尝试使用Objective-C

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.
            NSLog(@"didFinishLaunchingWithOptions");
    
            if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
                [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
            }
    
            return YES; 
        }
    

相关问题