ios swift local notifications no sound - ios

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

Related

Apple Watch Notification Actions not working when iPhone companion app is terminated watch OS 5.3.2

I am working on iOS app which is supported watch as well
below is my concerns.
watch os - 5.3.2 and iOS 13.1
I am facing problem with notification actions in watch app. when iPhone app is terminated and if I get a notification in watch , if I tap on action nothing happens.
I am relaying on push notification and for user notification actions I am registering for user notification.
as below
// this I will call in iPhone app code in did finishlaunching with options.
func registerForPushNotifications(withUserInfo userInfo: [AnyHashable: Any]?) {
if #available(iOS 10.0, *) {
self.requestAuthorization(withUserInfo: userInfo)
} else {
registerUserNotificationSettings(withUserInfo: userInfo)
UIApplication.shared.registerForRemoteNotifications()
}
}
func registerUserNotificationSettings() {
let approveAction = UIMutableUserNotificationAction()
approveAction.title = NSLocalizedString("Approve", comment: "Notification approve button title")
approveAction.identifier = "approve"
approveAction.isDestructive = false
approveAction.activationMode = .background
approveAction.isAuthenticationRequired = true
let denyAction = UIMutableUserNotificationAction()
denyAction.title = NSLocalizedString("Deny", comment: "Notification deny button title")
denyAction.identifier = "deny"
denyAction.isDestructive = true
denyAction.activationMode = .background
denyAction.isAuthenticationRequired = true
let signInCategory = UIMutableUserNotificationCategory()
signInCategory.setActions([approveAction, denyAction], for: .default)
signInCategory.identifier = "pushotp"
let categories = NSSet(object: signInCategory) as? Set<UIUserNotificationCategory>
let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: categories)
DispatchQueue.main.async(execute: {
UIApplication.shared.registerUserNotificationSettings(settings)
})
}
Scenarios
When iPhone app is in background and iPhone is locked and watch
is in background. If I push a push notification from server , watch
displays notification banner with two actions, if I tap any one
action , its working fine.
when iPhone app is killed and iPhone locked immmediatly If I send
pushnotification , watch displays notification banner with two
actions, if I tap any one actions its working fine.
When iPhone app is killed and iPhone locked, kept both watch and
iPhone idle for a hour so. then I will activate the watch and push
the notification from server, now watch displays banner with two
actions, now if I tap on the actions it is not working.
It would be great if you guys tell me what is the correct thing I have to do.
To ensure that the actions are taken in the 3rd use case, please ensure that Notification Payload should have contentAvailable property to YES. So that the application will launch in background.
And it will update the notification user actions accordingly.

Trying to fire UILocalNotification not working

I'm using the code below to create a notification, but somehow it's not firing. I really just want it to trigger as I open one certain view controller, but nothing is firing so far. What is the problem with my code?
func notify() {
let notification = UILocalNotification()
notification.fireDate = Date()
notification.repeatInterval = .minute
notification.alertBody = "The alert body"
notification.alertAction = "enter text here"
UIApplication.shared.scheduleLocalNotification(notification)
}
I also have this line in the app delegate:
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
One reason I believe this would fail is because notification.fireDate = Date() will set the fireDate to the current time, which will be in the past by the time the (separate) process managing UserNotifications is alerted that you want to fire a notification. Consider moving to UNNotifications
I changed the firing date to a certain time in the future (it was 6 PM where I changed it to 06:03 by using calendarComponents, and then it seemed to work. The notification got fired :)

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)

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.

UILocalNotification wants permission to show, but it is already granted

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.

Resources