2017-01-17 5 views
0

Ich entwickle eine iOS App, die eine lokale Benachrichtigung als Warnung auf dem Bildschirm in der App macht. Ich möchte, dass es die Benachrichtigung an den Hauptbereich Benachrichtigungen sendet und stattdessen nichts auf dem Bildschirm anzeigt. Wie kann ich das machen?Senden Sie UILocalNotification an den Hauptbereich Notifications anstelle von Alert

Im Moment sagt der Infobereich nur "No Notifications".

Hier ist mein Code:

AppDelegate

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateActive) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                 message:notification.alertBody 
                 delegate:self cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
     // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 
} 

Viewcontroller

self.itemText.text = @"Notification"; 
NSDate *pickerDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5]; 
NSLog(@"Picked date is %@", pickerDate); 

NSDate *todaysDate; 
todaysDate = [NSDate date]; 
NSLog(@"Todays date is %@", todaysDate); 


// Schedule the notification 
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = pickerDate; 
// TO DO : Assign proper text to self.itemText.text based on real data 
localNotification.alertBody = self.itemText.text; 
localNotification.alertAction = @"Another"; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
+0

Sie drücken also grundsätzlich eine 'UILocalNotification'. Wenn Sie die Benachrichtigung in der App erhalten, müssen Sie eine Warnung in der App anzeigen. Ist das richtig? – KrishnaCA

Antwort

0

UILocalNotification wird nur der Haupt Benachrichtigungen Bereich hinzugefügt werden, wenn die Anwendung im Hintergrund ist oder beendet wird. Im Vordergrund wird es Ihre Methode

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

Der einzige Weg, rufen Sie bekommen, was Sie wollen, ist UserNotifications.framework (seit iOS 10) zu verwenden.

Introduction to User Notifications Framework in iOS 10 ist nur der erste Tutorial

Here ist die Apple-Dokumentation auf dem lokalen Benachrichtigungen

und deren Verwendung, vergessen Sie nicht Methode des UNUserNotificationCenterDelegate zu implementieren india Benachrichtigung selbst wenn die Anwendung im Vordergrund zu bekommen ist .

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler; 

Hoffe es hilft!

+0

Hallo, danke für die Beantwortung meiner Frage. Im Moment gibt es nichts im Hauptbereich Benachrichtigungen, auch wenn die Anwendung im Hintergrund ist, obwohl ich ein Abzeichen in der App sehen kann. Was vermisse ich? Wir arbeiten leider unter iOS 9.2 :( – DrumPower3004

+0

Text sollte unter localNotification.alertBody hinzugefügt werden (derzeit ist es leer) und soundName sollte auf UILocalNotificationDefaultSoundName oder was auch immer gesetzt sein. – Sander

+0

yup hat das im ViewController. Es bekommt die Warnung in der App Bildschirm, aber nicht im Bereich Benachrichtigungen, wenn App im Hintergrund ist – DrumPower3004

Verwandte Themen