Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 607
tudoricc Avatar asked Dec 01 '22 15:12

tudoricc


2 Answers

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").

like image 53
Tancrede Chazallet Avatar answered Dec 12 '22 09:12

Tancrede Chazallet


  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];
        }
    }
    
like image 32
ibamboo Avatar answered Dec 12 '22 09:12

ibamboo