我想在iOS设备中检查“推送通知选项”,如果应用程序正在运行(或从恢复模式打开)。如果选项为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];
}
}
然后我使用以下代码转到“设置标签>>通知中心”,以便用户可以手动操作:
- (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)];
}
}
}
但是,现在我遇到的问题是,它只在启动应用程序后第一次出现。它按我的意愿工作。但在那之后,如果我从“设置”关闭“推送通知选项”,它就不会给我“警告信息”。
答案 0 :(得分:41)
在iOS 8中,您现在可以使用:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
要检查设置的设置方式,您可以使用:
[[UIApplication sharedApplication] currentUserNotificationSettings];
答案 1 :(得分:21)
如果应用程序曾在registerForRemoteNotification
注册,那么您可以禁用以及启用。一旦你禁用并且即将重新使用它,那么这将启用registerForRemoteNotification
,而不使用Popup来发送警报。
Technical Note TN2265: Troubleshooting Push Notifications
第一次推送启用的应用注册推送通知,iOS 询问用户是否希望接收该应用的通知。一旦 用户已响应此警报,除非再次显示 设备已恢复或已卸载应用程序至少a 一天。
如果您想模拟首次运行的应用,可以离开 该应用程序已卸载一天。没有你可以实现后者 实际上等一天通过设置系统时钟一天或 更多,完全关闭设备,然后关闭设备 上。
修改:适用于checking with alert enable
-
使用
if (types & UIRemoteNotificationTypeAlert){}
而不是
if (types == UIRemoteNotificationTypeNone){}
修改: 来自doc for iOS 8 or later的最新更新,您可以查看:
- (BOOL)isRegisteredForRemoteNotifications
答案 2 :(得分:8)
这对我有用。希望这有帮助! :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);
}
}
答案 3 :(得分:4)
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");
}
}