tudoricc
tudoricc

Reputation: 729

UILocalNotification not working

I am trying to make a chat app in which, when you are in a conversation with another person and receive a message from another one you display a local notification.

This far I implemented this in my view:

else {
    // in case the user that sent the message 
    // is not the same as the one you are currently talking to
    var partOfIt = msg.componentsSeparatedByString("\n")[1].componentsSeparatedByString(":")[6] as NSString
    var tuple = (partOfIt,fromuser)
    println("open up a new view")
    let notification: UILocalNotification = UILocalNotification()
    notification.timeZone = NSTimeZone.defaultTimeZone()

    let dateTime = NSDate(timeIntervalSinceNow: 2)
    notification.fireDate = dateTime
    notification.alertBody = "Woww it works!!"
    notification.alertAction = "Testing notifications on iOS8"
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

Unfortunately, nothing happens except for the println. I have allowed notifications to be shown when I use my app.

Furthermore, I also want to open a new view. when the user clicks this notification, how do I do this?

Is it something that I am doing wrong?

Upvotes: 4

Views: 6621

Answers (3)

Timur Kuchkarov
Timur Kuchkarov

Reputation: 1155

Local notifications are handled in AppDelegate. If your app is running, you have to implement application:didReceiveLocalNotification:, if not - you should handle it in didFinishLaunchingWithOptions (local notification is found at UILocalNotificationKey or something like that).

Upvotes: 4

ibamboo
ibamboo

Reputation: 1577

  1. the fireDate must be future time.
  2. app must be running in backdrop, or is closed.
  3. one more thing, do not forget to show query whether to allow push, add below code to appDelegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
          UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
          [application registerUserNotificationSettings:settings];
        }
    }
    

Upvotes: 7

Tancrede Chazallet
Tancrede Chazallet

Reputation: 7245

Notifications show up only when your app is in background. Even local ones. Since you send the notification in 2 seconds, I guess your app is still open.

By the way you should still be able to see the notification in the notification center (even if you didn't have any "popup bar").

Upvotes: 7

Related Questions