简体   繁体   English

提醒用户启用来自iOS设置的通知

[英]Alert user to enable notifications from settings in ios

Hi in my app i have notification section and user can enable notifications using switch.After first launch when ever user on the switch i am getting don't allow or ok alertview from ios.If user select don't allow and switch will be off and user will not get notifications. 您好在我的应用程序中我有通知部分,并且用户可以使用switch启用通知。首次启动后,一旦有用户在该开关上,我就会表示不允许或从ios收到警报视图。如果用户选择不允许,则开关将关闭用户将不会收到通知。 Now if user try to on the switch i want to show an alert to user with text "Please enable notifications from settings".Can any one please suggest the way to do this. 现在,如果用户尝试打开开关,我想向用户显示“请启用来自设置的通知”文本的警报。任何人都可以建议这样做的方法。

You can check the the permission using isRegisteredForRemoteNotifications method. 您可以使用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);
    }
}

对于UILocalNotification权限,请检查以下内容,如果用户不允许,则types参数值将为none。

[[UIApplication sharedApplication] currentUserNotificationSettings]
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");
                }

            }
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);

        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIAp

plication sharedApplication] registerUserNotificationSettings:settings]; plication sharedApplication] registerUserNotificationSettings:settings];

// [[UIApplicationsharedApplication]registerForRemoteNotifications]; // [[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.";
            }

        }
    }

Try this code. 试试这个代码。 It will work for iOS 8.0 later and before versions. 它适用于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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM