首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocalNotification for iOS

LocalNotification for iOS
EN

Stack Overflow用户
提问于 2014-02-25 09:32:59
回答 1查看 351关注 0票数 1

我正在我的iOS应用程序中实现iOS。实际上,在创建通知时,我有一个带有方法的实现:

代码语言:javascript
复制
-(void)setAlarmAction: (NSDate *) notificationDay days: (NSNumber *) day alert :  (NSNumber * ) priority  name:(NSString * ) nameNotification {

 NSDate *notificationDate = notificationDay;

 UILocalNotification *localNotification = [[UILocalNotification alloc] init];
 if (localNotification == nil) {
    return;
 }

 localNotification.fireDate = notificationDate;
 localNotification.timeZone = [NSTimeZone defaultTimeZone];

 localNotification.alertBody =  [NSString stringWithFormat: @"Quedan %@ dias" , day];
 localNotification.alertAction = @"Ver Documento";

 localNotification.soundName = UILocalNotificationDefaultSoundName;
 localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1; //aumenta en uno el numero de notificaciones

////////////
 NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:@"notification" forKey:nameNotification]; //1º que se guarda , 2º con que clave
 localNotification.userInfo = infoDictionary;
////////////
 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotification];
 [[NSUserDefaults standardUserDefaults] setObject:data forKey:nameNotification];

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

 // Get list of local notifications
 NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

 for( UILocalNotification * local  in localNotifications) {

    NSLog(@"Notificacion: %@ fecha :%@",local.alertBody,local.fireDate.description);
 }

}

-(void) launchNotificacion:(Documento *)document  {

  self.txDate.text = document.text;

}

我希望应用程序在后台中运行,当它出现在tabBar上时,我可以将它推送到一个具体的视图中,并启动一些数据。我是在didFinishlaunchingWithOptions做的。

代码语言:javascript
复制
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 //self.viewDocumentForm
 VTDocumentFormViewController *VTDocument = [[VTDocumentFormViewController alloc] initWithNibName:@"VTDocumentFormViewController" bundle:nil];

 UILocalNotification *notification = [launchOptions objectForKey:
                                     UIApplicationLaunchOptionsLocalNotificationKey];

 if (notification) {

   [self.window.rootViewController presentViewController: VTDocument animated:YES completion:nil];

   Documento *reminderDocument = [NSEntityDescription insertNewObjectForEntityForName:@"Documento" inManagedObjectContext:self.managedObjectContext];
   reminderDocument = [notification.userInfo objectForKey: key ];

    [self.viewDocumentForm launchNotificacion:reminderDocument]; 
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
 }

// Override point for customization after application launch.
return YES;

}

但这不管用,我也不知道原因。拜托救救我。

EN

回答 1

Stack Overflow用户

发布于 2015-02-24 16:15:07

只有当app在到达didFinishLaunchingWithOptions:之前被挂起/终止时,launchOptions中的UIApplicationLaunchOptionsLocalNotificationKey才会被调用。

如果你的应用程序只是在后台(而不是被杀死)或活动,你会被application:didReceiveLocalNotification:炒掉

另外,在您的didFinishLaunchingWithOptions:中,我看不到您在哪里注册了用户通知设置(用于iOS8)…小象:

代码语言:javascript
复制
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;

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

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22009920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档