2016-07-15 15 views
0

Ich versuche, ein UILocalNotification wie diese zu senden:UILocalNotification sendet keine

func sendNotifiaction() { 
     let notification = UILocalNotification() 
     notification.userInfo = [ 
      "Identifier": self.identifier! 
     ] 
     notification.alertTitle = "Alarm!" 
     notification.alertBody = "test" 
     //notification.soundName = "Temporary-bleep-sound.aiff" 
     notification.category = "category" 

     UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    } 

Ich habe versucht, einen Haltepunkt auf dieser Methode zu setzen und es aufgerufen wird und laufen, aber die Benachrichtigung nicht überhaupt gesendet.

Wer hat eine Idee warum?

+0

Haben Sie der App erlaubt, Push-Benachrichtigungen zu senden? – Lumialxk

+0

@Lumialxk Ich denke, das ist der Punkt, wie kann ich das tun? –

+0

Wo ist dein Feuerdatum? – iMHitesh

Antwort

0

Du musst die Benachrichtigung registrieren erste

UIApplication.sharedApplication().cancelAllLocalNotifications() 
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil) 
UIApplication.sharedApplication().registerUserNotificationSettings(settings) 

Hier ist meine Demo für die Benachrichtigung zu einem bestimmten Zeitpunkt Brennen

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 

     UIApplication.sharedApplication().cancelAllLocalNotifications() 
     let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil) 
     UIApplication.sharedApplication().registerUserNotificationSettings(settings) 

     let localNotification1 = UILocalNotification() 
     localNotification1.alertBody = "Your alert message at 9:00 pm" 
     localNotification1.timeZone = NSTimeZone.defaultTimeZone() 
     localNotification1.fireDate = self.getNinePMDate() 
     UIApplication.sharedApplication().scheduleLocalNotification(localNotification1) 
     return true 
    } 

    func getNinePMDate() -> NSDate? { 
     let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
     let now: NSDate! = NSDate() 
     let date21h = calendar.dateBySettingHour(21, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst) 
     return date21h 
    } 
} 
0

Sie didReceiveLocalNotification in appdelegate implementieren sollten, und Sie müssen manuell Alarm Ansicht anzuzeigen mit Titel und Nachricht als alertbody Ihrer Benachrichtigung.