gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:18:55 26 4
gpt4 key购买 nike

我想在触发本地通知时显示警报,但为此我必须请求许可,正如我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com