且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 iOS 中打开或关闭推送通知

更新时间:2023-02-26 21:05:13

如果应用程序曾经通过 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