ios - 请求用户允许在触发本地通知时显示警报

标签 ios xcode alert uialertview uilocalnotification

我想在触发本地通知时显示警报,但为此我必须请求许可,正如我在 iPhone 上运行该应用程序时对我说的那样:

Attempting to schedule a local notification {fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, user info = (null)} with an alert but haven't received permission from the user to display alerts

我该怎么做? 这是现在的代码:

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

最佳答案

添加这段代码,它会显示一个警告 View 来请求用户的许可。

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

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

关于ios - 请求用户允许在触发本地通知时显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203066/

相关文章:

ios - 从设置iOS设置Twitter帐户

ios - 在AspectFit之后获取UIImage的尺寸或CGRect

swift - 如何在 Xcode 中为 Perfect 检索/生成 PerfectLib.framework 文件?

javascript - 为什么添加 Javascript Alert 会改变此代码的行为

swift - LocalNotification警报干扰

ios - Sprite Kit - 为多个场景定义变量

iphone - 以编程方式更改 iOS 锁屏

ios - iPhone 应用程序编程中的行和列

c - .C & .H 文件错误

javascript - 更改警报框上的 'ok' 按钮以停止进一步移动