在 iOS 中打开或关闭推送通知 [英] Push Notification ON or OFF Checking in iOS

查看:158
本文介绍了在 iOS 中打开或关闭推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 iOS 设备中检查推送通知选项",只要应用程序正在运行(或从恢复模式打开).我使用以下代码检查该选项是否关闭:

-(void)PushNotificationServiceChecking{UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];如果(类型 == UIRemoteNotificationTypeNone){NSString *msg = @"请按ON开启推送通知";UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"推送通知服务禁用" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ON", nil];警报.标签 = 2;[警报显示];}}

然后我使用以下代码转到设置选项卡>>通知中心",以便用户可以手动进行:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{如果(alertView.tag == 2){如果(按钮索引 == 0){//这是取消按钮}否则如果(按钮索引 == 1){[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];}}}

但是,现在我面临的问题是,它只在启动应用程序后的第一次出现.它按我的意愿工作.但在那之后,如果我从设置"中关闭推送通知选项",它不会给我任何警报消息".

解决方案

如果应用程序曾经通过 registerForRemoteNotification 注册,那么您可以禁用和启用 .一旦您禁用并准备重新注册它,那么这将启用 registerForRemoteNotification,而不会弹出提示.

技术说明 TN2265:推送通知故障排除 ><块引用>

启用推送的应用首次注册推送通知时,iOS询问用户是否希望接收该应用程序的通知.一次用户已对此警报做出响应,除非再次出现设备已恢复或应用程序已被卸载至少一天.

如果你想模拟你的应用程序的第一次运行,你可以离开应用卸载了一天.你可以在没有实际上通过将系统时钟向前设置一天来等待一天或更多,完全关闭设备,然后将设备转回来

更多信息:信息&&信息 2

编辑:用于使用警报启用检查 -

使用

 if (types & UIRemoteNotificationTypeAlert){}

代替

if (types == UIRemoteNotificationTypeNone){}

适用于 iOS 8 或更高版本的文档,您可以通过以下方式查看:

- (BOOL)isRegisteredForRemoteNotifications

I want to check "Push Notification option" in iOS device, any time if the application is running (or ON from resume mode). I use the following code to check, if the option is OFF:

-(void)PushNotificationServiceChecking
{
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    if (types == UIRemoteNotificationTypeNone)
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ON", nil];
        alert.tag = 2;
        [alert show];
    }
}

Then i use the following code for going to the "Settings tab >> Notification center", so that user can on it manually :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        if (buttonIndex == 0)
        {
            // this is the cancel button
        }
        else if (buttonIndex == 1)
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];
        }
    }

}

But, now the problem that I am facing is, it only appears at the 1st time after launching the application. It works as I want. But after that, if I turn OFF the "Push Notification option" from "settings" it gives me no "Alert Message".

解决方案

If the App once got registered with the registerForRemoteNotification, then you can disable as well as enable . Once you disable and you are about to Re-Regigister with it, then this will enable the registerForRemoteNotification, without Popup for a alert.

Technical Note TN2265: Troubleshooting Push Notifications

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by setting the system clock forward a day or more, turning the device off completely, then turning the device back on.

Fore More Info: INFO && Info 2

Edit : For checking with alert enable -

use

 if (types & UIRemoteNotificationTypeAlert){} 

instead of

if (types == UIRemoteNotificationTypeNone){}

Edit : Latest update from the doc for iOS 8 or later, You can check out by :

- (BOOL)isRegisteredForRemoteNotifications

这篇关于在 iOS 中打开或关闭推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆