2017-03-03 1 views
0

Ich versuche, meinem Code eine Benachrichtigung hinzuzufügen, so dass ein Benutzer bei Betätigung einer Schaltfläche in einer Datenbank gespeichert wird und eine Benachrichtigung angezeigt wird, dass der Benutzer erfolgreich in der Datenbank gespeichert wurde Datenbank. Ich habe meinen Code überprüft, funktioniert aber immer noch nicht. Es werden keine Fehler gemeldet, aber auch keine Benachrichtigungen. Ich versuche, ein kleines Banner oben auf dem Bildschirm zu erstellen, in dem "Kunde in Datenbank gespeichert" steht.Benachrichtigungen in Xcode werden nicht angezeigt

Hier ist mein Code; Wenn Sie Fehler entdecken oder Vorschläge machen könnten, wäre ich wirklich dankbar!

Verwenden von Xcode 8.2.1. und Objective C

in AppDelegate.m:

-(void)application:(UIApplication *)application  didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings{} 

in NewCustomerViewController.m:

@interface NewCustomerViewController(){NSUserDefaults *defaults;} 

im viewDidLoad:

defaults = [NSUserDefaults standardUserDefaults]; 
UIUserNotificationType types = UIUserNotificationTypeBadge| UIUserNotificationTypeSound| UIUserNotificationTypeAlert; 
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 

und schließlich in meinem IBAction für die Schaltfläche, die Kunden speichert:

[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]; 

Danke nochmal!

+0

Welche iOS-Version verwenden Sie? Ihre App ist geöffnet und im Vordergrund? –

+0

Mein iOS-Bereitstellungsziel ist 8.0, meinst du das? Ich habe die App in einem Simulator auf meinem Desktop geöffnet. –

+0

Sie können keine lokale Benachrichtigung anzeigen, wenn Ihre App im Vordergrund ist (es sei denn, Sie simulieren sie mithilfe einer UIView, die wie eine aussieht), sofern Sie nicht auf iOS 10.0 ausgerichtet sind. Gibt es einen Grund für iOS 8.0? Es gibt nur etwa 5% der Benutzer mit iOS 8. – Gruntcakes

Antwort

0

Die Art und Weise Sie mit Local-Benachrichtigung in iOS 9 verwenden zu arbeiten und unten ist ganz anders in iOS 10. Below-Code für lokale Benachrichtigung ist:

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“); 
    } 
    }]; 

Sie können das Apple Referenzdokument https://developer.apple.com/reference/usernotifications for UserNotification beziehen.

Verwandte Themen