您好我的应用程序我有通知部分,用户可以使用开关启用通知。首次启动时,当我的用户在交换机上我不允许或来自ios的提醒。如果用户选择不允许和切换将关闭,用户将不会收到通知。现在,如果用户尝试在交换机上,我想向用户显示一条警告“请从设置中启用通知”。任何人都可以建议这样做。
答案 0 :(得分:0)
您可以使用isRegisteredForRemoteNotifications方法检查权限。
- (void)checkForNotificationPermission
{
if (!([[UIApplication sharedApplication] isRegisteredForRemoteNotifications] && [self pushNotificationsEnabled]))
{
// Show alert here
}
}
// For fixing iOS 8 issue mentioned here http://stackoverflow.com/a/28441181/1104384
- (BOOL)pushNotificationsEnabled
{
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)])
{
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
return (types & UIUserNotificationTypeAlert);
}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}
答案 1 :(得分:0)
对于UILocalNotification
权限,请检查以下内容,types
参数值将是无用户不允许的。
[[UIApplication sharedApplication] currentUserNotificationSettings]
答案 2 :(得分:0)
NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];
if (versionVal >= 8)
{
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone)
{
NSLog(@" Push Notification ON");
}
else
{
NSString *msg = @"Please press ON to enable Push Notification";
UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
alert_push.tag = 2;
[alert_push show];
NSLog(@" Push Notification OFF");
}
}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types != UIRemoteNotificationTypeNone)
{
NSLog(@" Push Notification ON");
}
else
{
NSString *msg = @"Please press ON to enable Push Notification";
UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
alert_push.tag = 2;
[alert_push show];
NSLog(@" Push Notification OFF");
}
}
答案 3 :(得分:0)
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIAp
plication sharedApplication] registerUserNotificationSettings:settings];
// [[UIApplicationsharedApplication] registerForRemoteNotifications];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
if (types == UIUserNotificationTypeNone) {
[_TransparentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.8]];
lblDescription.text=@"Please enable notifications from settings.";
}
}
}
答案 4 :(得分:0)
试试这段代码。它适用于iOS 8.0以后版本。
if (([[[UIDevice currentDevice] systemVersion] compare:8.0 options:NSNumericSearch] != NSOrderedAscending)) {
if (![[UIApplication sharedApplication] isRegisteredForRemoteNotifications])
{
DisplayAlert(@"Please enable Permission from Settings->App Name->Notifications->Allow Notifications");
return;
}
}
else{
UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (status == UIRemoteNotificationTypeNone)
{
DisplayAlert(@"Please enable Permission from Settings->App Name->Notifications->Allow Notifications");
return;
}
}