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?
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").
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With