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



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

    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/

10-11 23:01