iOS9

时间:2015-10-02 06:52:29

标签: ios9 uilocalnotification localnotification

自iOS9起,本地通知无法正常运行。 有时用户会收到通知,有时他们不会收到通知。我的通知每天重复。 知道可能导致问题的原因吗?我看到一些帖子,iOS9中有一个错误,但我不确定是什么原因。

这是一段代码:

    NSDate *alarmDate = [date dateByAddingTimeInterval:DEFAULT_SNOOZE_DURATION * i];  
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];  
    localNotif.fireDate = alarmDate;  
    localNotif.timeZone = nil;          
    localNotif.alertBody = alertBody;  

    localNotif.hasAction = YES;  

    localNotif.applicationIconBadgeNumber = 1;  
    localNotif.soundName = UILocalNotificationDefaultSoundName;  
    localNotif.repeatInterval = NSCalendarUnitDay;  

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]  

感谢您的帮助

4 个答案:

答案 0 :(得分:4)

在AppDelegate.m中编写以下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{

    [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                   settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
                                                   UIUserNotificationTypeSound categories:nil]];
}

return YES;

}

也在AppDeleagte.m中编写此代码

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Reminder"    message:notification.alertBody
                                                           delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[notificationAlert show];

// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}

在ViewController中写下以下代码,您希望在其中实现本地通知。

forExample:这里我初始化UILocalNotification,它的Firedate是currentTime和5seval Interval Time。

在Fire localNotification之后,它将在您的屏幕上发出警报。

- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.repeatInterval = NSDayCalendarUnit;
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;


[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

答案 1 :(得分:3)

来自本地和远程通知编程指南:

  

在iOS 8及更高版本中,使用本地或远程通知的应用程序   必须注册他们打算提供的通知类型。该   系统然后给用户限制类型的能力   您的应用显示的通知。系统没有徽章图标,   显示警报消息,或播放警报声(如果有的话)   您的应用未启用通知类型,即使它们是   在通知有效负载中指定。

如指南所示,Apple显示的示例代码为:

 UIUserNotificationType types = UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
            [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

https://stackoverflow.com/a/27376475/6219830:Naughty_Ottsel

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIUserNotificationType types = UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings =[UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate = date;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Finally got it!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    return YES;
}

我希望有所帮助!

答案 2 :(得分:0)

如果你在iOS9上需要它,但在swift 3.1下:

final func registerToGetLocalNotificationsFor_iOS9(){
    let types : UIUserNotificationType = [.sound, .alert /*, .badge*/]
    let mySettings = UIUserNotificationSettings(types: types, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(mySettings)

}
iOS10上的

已被弃用。

答案 3 :(得分:-1)

在Appdelegate.m中包含以下代码

if(IS_OS_8_OR_LATER)
{
    UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}