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

时间:2014-06-13 10:20:17

标签: ios xcode alert uialertview uilocalnotification

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

  

尝试安排本地通知{开火日期= 2014年6月13日星期五12小时10分27秒中欧夏令时,时区=(null),重复间隔= 0,重复计数= UILocalNotificationInfiniteRepeatCount,下次开火日期=星期五2014年6月13日12小时10分27秒中欧夏令时,用户信息=(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 个答案:

答案 0 :(得分:40)

添加此代码后,它会显示一个警告视图,询问用户是否允许。

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

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

答案 1 :(得分:4)

苏健豪的回答很好。

在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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

用迅捷的语言......

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

答案 4 :(得分:0)

尝试使用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; 
    }