2017-03-03 75 views
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]; 

再次感谢!

+0

您使用的是哪个iOS版本?你的应用是开放的,并在前台? –

+0

我的iOS部署目标是8.0,这是你的意思吗?我在桌面上的模拟器中打开了该应用程序 –

+0

如果您的应用程序处于前台(除非您使用您制作的UIView来模拟它),否则无法显示本地通知,除非您的目标iOS 10.0。有没有任何理由针对iOS 8.0?只有大约5%左右的用户使用iOS 8. – Gruntcakes

回答

0

您使用的iOS 9本地通知工作,下面的方法是在iOS的10 完全不同的下面是本地通知代码:

的Objective-C:

1. In App-delegate.h file use @import UserNotifications; 
2. App-delegate should conform to UNUserNotificationCenterDelegate protocol 
3. In didFinishLaunchingOptions use below code : 

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) 
          completionHandler:^(BOOL granted, NSError * _Nullable error) { 
      if (!error) { 
        NSLog(@"request authorization succeeded!"); 
        [self showAlert]; 
           } 
          }]; 

-(void)showAlert { 
    UIAlertController *objAlertController = [UIAlertControlleralertControllerWithTitle:@"Alert" message:@"show an alert!"preferredStyle:UIAlertControllerStyleAlert]; 
     
    UIAlertAction *cancelAction = [UIAlertAction 
                                   actionWithTitle:@"OK" 
                                   style:UIAlertActionStyleCancel 
                                   handler:^(UIAlertAction *action) { 
                                       NSLog(@"Ok clicked!"); 
                                   }]; 
    [objAlertController addAction:cancelAction]; 
     
     
    [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:objAlertController animated:YEScompletion:^{ 
         
    }]; 
     
} 
4. Now create a button in any view controller and in IBAction use below code : 
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; 
    objNotificationContent.title = [NSStringlocalizedUserNotificationStringForKey:@“Notification!”arguments:nil]; 
    objNotificationContent.body = [NSStringlocalizedUserNotificationStringForKey:@“This is local notification message!“ 
                                                         arguments:nil]; 
    objNotificationContent.sound = [UNNotificationSounddefaultSound]; 
     
    /// 4. update application icon badge number 
    objNotificationContent.badge = @([[UIApplicationsharedApplication] applicationIconBadgeNumber] + 1); 
     
    // Deliver the notification in five seconds. 
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger 
                                                  triggerWithTimeInterval:10.f repeats:NO];        
     
    UNNotificationRequest *request = [UNNotificationRequestrequestWithIdentifier:@“ten” 
                                                                          content:objNotificationContent trigger:trigger]; 
    /// 3. schedule localNotification 
    UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter]; 
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
        if (!error) { 
            NSLog(@“Local Notification succeeded“); 
        } 
    else { 
        NSLog(@“Local Notification failed“); 
    } 
    }]; 

你可以参考苹果参考文档https://developer.apple.com/reference/usernotifications for UserNotification