2017-03-03 68 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