在 iOS 中打开或关闭推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20374801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push Notification ON or OFF Checking in iOS
提问by Tulon
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:
我想在 iOS 设备中检查“推送通知选项”,只要应用程序正在运行(或从恢复模式打开)。我使用以下代码检查该选项是否关闭:
-(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".
但是,现在我面临的问题是,它仅在启动应用程序后的第一次出现。它按我的意愿工作。但在那之后,如果我从“设置”中关闭“推送通知选项”,它不会给我任何“警报消息”。
采纳答案by Kumar KL
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.
如果应用程序曾经在 上注册过registerForRemoteNotification
,那么您可以禁用和启用 。一旦您禁用并且您将要重新注册它,那么这将启用 registerForRemoteNotification
,而不会弹出提示。
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.
启用推送的应用程序首次注册推送通知时,iOS 会询问用户是否希望接收该应用程序的通知。一旦用户对此警报做出响应,除非设备恢复或应用程序已卸载至少一天,否则它不会再次出现。
如果您想模拟应用程序的首次运行,您可以将应用程序卸载一天。您可以通过将系统时钟提前一天或更多,完全关闭设备,然后重新打开设备来实现后者,而无需实际等待一天。
Fore More Info: INFO && Info 2
Edit: For checking with alert enable
-
编辑:对于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 :
编辑:来自iOS 8 或更高版本文档的最新更新,您可以通过以下方式签出:
- (BOOL)isRegisteredForRemoteNotifications
回答by thijsai
In iOS 8 you can now use:
在 iOS 8 中,您现在可以使用:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
And to check how the settings are setup you could use:
并检查设置的设置方式,您可以使用:
[[UIApplication sharedApplication] currentUserNotificationSettings];
回答by 777Q
It's work for me. Hope this help! :D
这对我有用。希望这有帮助!:D
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
if (type == UIUserNotificationTypeNone){
ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
}
}
else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
}
}
回答by Maulik Salvi
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");
}
}