0
我想向我的代码添加一个通知,以便当按下按钮时,用户被保存到数据库,并弹出一个通知,说用户已成功保存到数据库。我检查了我的代码,但它仍然无法工作。没有错误出现,但没有任何通知。我试图在屏幕顶部创建一个小横幅,标题为“客户保存到数据库”。Xcode中的通知没有出现
这是我的代码;如果你能发现任何错误或建议如何解决它,我会非常感激!
使用Xcode 8.2.1。和目标C
在AppDelegate.m:
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings{}
在NewCustomerViewController.m
:
@interface NewCustomerViewController(){NSUserDefaults *defaults;}
在viewDidLoad中
:
defaults = [NSUserDefaults standardUserDefaults];
UIUserNotificationType types = UIUserNotificationTypeBadge| UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
终于,在我IBAction为为节省客户的按钮:
[defaults setBool:YES forKey:@"notificationIsActive"];
[defaults synchronize];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = 0;
//Enter the time here in seconds.
localNotification.alertBody= @"Customer Saved to Database";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//localNotification.repeatInterval= NSCalendarUnitDay;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
再次感谢!
您使用的是哪个iOS版本?你的应用是开放的,并在前台? –
我的iOS部署目标是8.0,这是你的意思吗?我在桌面上的模拟器中打开了该应用程序 –
如果您的应用程序处于前台(除非您使用您制作的UIView来模拟它),否则无法显示本地通知,除非您的目标iOS 10.0。有没有任何理由针对iOS 8.0?只有大约5%左右的用户使用iOS 8. – Gruntcakes