提醒用户启用 ios 设置中的通知

Posted

技术标签:

【中文标题】提醒用户启用 ios 设置中的通知【英文标题】:Alert user to enable notifications from settings in ios 【发布时间】:2015-12-14 05:54:08 【问题描述】:

嗨,在我的应用程序中,我有通知部分,用户可以使用开关启用通知。首次启动后,当用户在开关上时,我从 ios 获得不允许或确定警报视图。如果用户选择不允许并切换将关闭,用户将不会收到通知。现在,如果用户尝试打开开关,我想向用户显示带有文本“请启用设置中的通知”的警报。任何人都可以建议这样做的方法。

【问题讨论】:

【参考方案1】:

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

[[UIApplication sharedApplication] currentUserNotificationSettings]

【讨论】:

【参考方案2】:

您可以使用isRegisteredForRemoteNotifications方法检查权限。

- (void)checkForNotificationPermission

    if (!([[UIApplication sharedApplication] isRegisteredForRemoteNotifications] && [self pushNotificationsEnabled])) 
    
       // Show alert here
    



// For fixing iOS 8 issue mentioned here http://***.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);
    

【讨论】:

【参考方案3】:
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");
                

            

【讨论】:

【参考方案4】:
        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.";
            

        
    

【讨论】:

【参考方案5】:

试试这个代码。它适用于 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;
    

【讨论】:

以上是关于提醒用户启用 ios 设置中的通知的主要内容,如果未能解决你的问题,请参考以下文章

来自 Firebase 的应用(Android 或 iOS)中的提醒通知

从设置应用程序 ios 获取推送通知启用/禁用事件?

如何显示自定义警报以在 ios 中启用通知

iOS 本地通知的实现

iOS 本地通知的实现

iOS:如果之前启用,则禁用远程通知