【发布时间】:2015-04-12 14:20:36
【问题描述】:
有没有简单的方法来测试用户是否允许本地通知?在我拒绝发送本地通知后,我注意到控制台中出现警告。当相关事件发生时,它表示应用程序尝试发送通知,即使用户不允许。我想在尝试显示通知之前检查它是否被允许。在我的情况下看到评论,我该怎么做?
我的代码是:
app = [UIApplication sharedApplication];
-(void)showBackgroundNotification:(NSString *) message {
//check if app is in background and check if local notifications are allowed.
if (app.applicationState == UIApplicationStateBackground /*&& LocalNotificationsAreAllowed*/){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
我得到提示用户的权限,如下所示:
UIUserNotificationSettings *settings;
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
{
settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification TypeSound) categories:nil];
[app registerUserNotificationSettings:settings];
}
我不能使用设置对象吗?
编辑:我想我已经解决了。这似乎有效。
-(void)showBackgroundNotification:(NSString *) message {
if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
【问题讨论】:
-
在这里查看我的答案:stackoverflow.com/a/33779144/1463604
标签: ios ios8 uilocalnotification