Ios 请求用户在触发本地通知时显示警报的权限
我想在本地通知被触发时显示警报,但为此我必须请求许可,正如我在iPhone上运行应用程序时对我说的那样: 试图安排本地通知{火灾日期=2014年6月13日星期五12小时10分钟27秒中欧夏季时间,时区=(null),重复间隔=0,重复计数=UILocalNotificationInfiniteRepeatCount,下一次火灾日期=2014年6月13日星期五12小时10分钟27秒中欧夏季时间,用户信息=(null)}具有警报,但尚未收到用户显示警报的权限 我该怎么做? 以下是目前的代码:Ios 请求用户在触发本地通知时显示警报的权限,ios,xcode,alert,uialertview,uilocalnotification,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];
添加此代码,它将显示一个警报视图以请求用户权限
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
categories:nil]];
}
您可以在应用程序中添加此代码:didFinishLaunchingWithOptions;方法,这样应用程序将在用户启动应用程序时询问用户,或者您可以在设置本地通知时添加此代码,这取决于您。蘇健豪'她回答得很好 在Swift中,它如下所示:
let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:")
if registerUserNotificationSettings {
var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))
}
另请参见此处:为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;
}
用迅捷的语言
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
iOS 8需要请求许可。请看检查这一条:这段代码很棒,谢谢!大家注意:在你的应用程序中,如果你想请求用户允许发送通知,请仔细考虑,因为你不想在他们打开你的应用程序时吓跑他们。首先建立一些信任,然后征求他们的同意。UX思想食粮:)在swift中,它看起来是这样的:让registerUserNotificationSettings=UIApplication.InstanceResponsionToSelector(“registerUserNotificationSettings:”)如果registerUserNotificationSettings{var types:UIUserNotificationType=UIUserNotificationType.Alert | UIUserNotificationType.Sound UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(类型:类型,类别:nil))}建议iOS 8及以上版本使用此代码段。我可以重新询问吗?如果用户意外不允许此权限,我可以制作一个按钮吗?如果用户点击此按钮,应用程序将重新询问通知权限吗?
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();