UILocalNotification wants permission to show, but it is already granted - ios

I am allowing remote and local notifications in my app, it works perfectly fine for remote notifications but when trying to use local notifications it then does not show the notification, but it is running the code.
Remote notifications work when I am out of the app, but local notifications don't want to show when I am in the app?
Here is the code:
In the didFinishLaunchingWithOptions method:
let notificationTypes:UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
and the receiving of the notification:
if(application.applicationState == UIApplicationState.Active) {
var ln: UILocalNotification = UILocalNotification()
ln.userInfo = userInfo
ln.soundName = UILocalNotificationDefaultSoundName
ln.alertBody = notification["alert"] as NSString
ln.fireDate = NSDate()
application.scheduleLocalNotification(ln)
println("local")
} else {
PFPush.handlePush(userInfo)
}
When in the app, it is printing out local.
Any ideas?

It sounds like you don't quite get what a local notification is. The whole point of a local notification is that it is a way for the system to notify the user on your behalf when your app isn't frontmost. If your app is frontmost, there is nothing more for the system to do. Local notifications, therefore, do not fire any alert to the user when the app is frontmost. Instead, if your app is frontmost when a local notification fires, your app is notified and you can alert the user if you like.

Related

Local Notification on offline (Swift)

I want to receive a local notification in my app (swift) when I'm not connected to Internet and I have some information registered in my Local Data base.
Is it possible to do that please?
Local Notification doesnot requires internet.
About Local Notification from apple developer site
Local notifications give you a way to alert the user at times when your app might not be running. You schedule local notifications at a time when your app is running either in the foreground or background. After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time. Your app does not need to be running for the system to deliver the notification.
For more info check this link. You can also check this link for tutorial.
do like this :
public func presentNotification(_ notifAction: String, notifBody: String) {
let application = UIApplication.shared
let applicationState = application.applicationState
if applicationState == UIApplicationState.background {
let localNotification = UILocalNotification()
localNotification.alertBody = notifBody
localNotification.alertAction = notifAction
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber += 1
application.presentLocalNotificationNow(localNotification)
}
}
UIApplicationState has these states :
case active
case inactive
case background

detect specific localNotification opened

I am scheduling timer and sending some local notification for user about some data, example is - if there is some store near.
func configureNotification(shop: Shop) {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 0)
notification.alertBody = "There is a store \(shop.name) near!"//Localized().near_shop_string + shopName
notification.alertAction = "Swipe to see offer!"//Localized().swipe_to_see_string
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
When app running in background if there is some store near users coordinates, there is a local notification.
For example, there is three local notification received about different stores and user swipes the second one and make app active from it.
The question is, to recognize from what specific notification applicationDidBecomeActive was launched, some launcOptions, as for push notifications from server? Any solutions?
You need handle it in didReceiveLocalNotification delegate method
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
// do your jobs here
}
notification param will contain info for every notification.
Also launchOptions has a key UIApplicationLaunchOptionsLocalNotificationKey that contains notification.
You can get it like
let localNotification:UILocalNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey)

ios swift local notifications no sound

Iam trying to make a regular UILocalNotification with default sound
this is my function
func makeLocalNotificationForNow(str : String , id : String)
{
let notification = UILocalNotification()
notification.alertBody = str // text that will be displayed in the notification
notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = NSDate(timeIntervalSinceNow: 5) // todo item due date (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
in my didFinishLaunchingWithOptions function I put
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
Note : I also use parse push notifications
when I make the notification , it appears but without an alert or sound (I need to swipe the from top to see the notification
what's wrong ?!
as far as i know notifications only make a sound when the app is in the background, meaning quit you app, put your phone on locked mode and send a notification through the parse website, then you should hear the sound, if not, check the settings on the app you're making to see if sound alerts are enabled, and last resource, check that your phone is not on vibrate only
it was a stupid mistake , the problem was my phone , I changed the phone and it all worked

Local notifications sent to background works in simulator but not on device

In the simulator I'm able to get the exact result I want: When I trigger a notification and my simulator phone is locked, then the notification gets pushed to the watch.
This used to work on device (my iPhone 6 on iOS 9.1 and Watch on watchOS 2.0). For some reason it stopped working and I don't know how to debug the problem.
So on device, I make sure that the app is in background by going to home screen and locking the phone and making sure my watch is asleep. When the notification is triggered, nothing happens. When I open up the app, that's when the notifications finally gets triggered. I.E. if I trigger 3 notifications, none of them register until I open the app back into foreground. This is not how it works in simulator (simulator has correct behavior described above).
The notification is triggered by me changing a text object that is stored in my Firebase db. The change calls the sendNotification function. Again, this works perfectly fine in the simulator and used to work on device but for some reason doesn't work anymore.
In app delegate I have:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
// Setting up Local Notification
let assistAction = UIMutableUserNotificationAction()
assistAction.activationMode = UIUserNotificationActivationMode.Background
assistAction.identifier = categoryID
assistAction.title = "Assist"
assistAction.authenticationRequired = false
assistAction.destructive = false
// Category
let assistCategory = UIMutableUserNotificationCategory()
assistCategory.identifier = categoryID
assistCategory.setActions([assistAction], forContext: UIUserNotificationActionContext.Default)
// Only way to get this to work with parameter
let categories = NSSet(object: assistCategory)
// Settings
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
Then in my view controller I have:
func sendNotification(customerName: String, helpText: String) {
// Local Notification
let notification = UILocalNotification()
notification.alertBody = "\(customerName) \(helpText)"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate()
notification.category = categoryID
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
print("Sent notification");
}
Simulator seems to work differently than device. It's weird that simulator local notifications was acting like remote push notifications.
I ended up giving up on trying to make local notifications show up while app is in background on device because I've learned that that is not the expected behavior.
The correct solution is to just set up remote push notification, which I used Parse to do. And now it's working as expected. I have a web server that uses the Parse REST API to send a POST request to Parse and my iOS app is then set up to receive remote push notifications which now show up on my watch when my phone is locked.

Push Notifications: no sound

I've implemented push notification system in my application using Parse.com and everything works great!
My only problem is that when the notification arrives: it does not play any sound!
I go to settings (in my tablet), under notifications, I see this:
As you see the "sound" and the "badge" are OFF. If I turn them on then when a push notification arrives: it plays the sound!
So... I would like that when I install my application these 2 options are by default TRUE.
Is this possible? How can I do that?
I am working in Swift and this is my code so far:
method didFinishLaunchingWithOptions
var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge , categories: nil)
application.registerUserNotificationSettings(pushSettings)
application.registerForRemoteNotifications()
Thanks a lot for helping me
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
you have to set the soundName also because the default is no sound :
For this property, specify the filename (including extension) of a
sound resource in the app’s main bundle or
UILocalNotificationDefaultSoundName to request the default system
sound. When the system displays an alert for a local notification or
badges an app icon, it plays this sound. The default value is nil (no
sound). Sounds that last longer than 30 seconds are not supported. If
you specify a file with a sound that plays over 30 seconds, the
default sound is played instead.
yourNotification.soundName = UILocalNotificationDefaultSoundName
soundName
for remote notifications you need to use
The Notification Payload
If you wish to play the IOS standard sound for your Notification, you need to set your content.sound to UNNotificationSound.default()
You can do this in your schedule function like I did here:
func schdule(date:Date,repeats:Bool)->Date?{
let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()
content.title = "Title"
content.body = "blablablabla"
content.setValue("YES", forKeyPath:"shouldAlwaysAlertWhileAppIsForeground")
let trigger = UNCalendarNotificationTrigger(dateMatching: yourDateComponents,repeats: repeats)
let request = UNNotificationRequest(identifier: "TestNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error:Error?) in
if error == nil {
print("Notification Schduled",trigger.nextTriggerDate()?.formattedDate ?? "Date nil")
} else {
print("Error schduling a notification",error?.localizedDescription ?? "")
}
}
return trigger.nextTriggerDate()
}
Similar to what you have for UIUserNotificationSettings, try it for RemoteNotification.
For example, in Objective-C:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
I guess that this is a problem you are facing only on that precise device. Try it on another one and see if it is different.

Resources