Push Notifications: no sound - ios

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.

Related

How To Create Alarm Notification Swift iOS?

I want to when user get notification from fcm (Firebase Cloud Messaging) or scheduler local notification like alarm notification like image below :
Here my code :
func onTest() {
let content = UNMutableNotificationContent()
content.title = "Weekly Staff Meeting"
content.body = "Every Tuesday at 2pm"
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 16 // 14:00 hours
dateComponents.minute = 11
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}
My code right now just like the usual notification which disappears in 5 seconds I want to make it like an Alarm notification or Whatsapp Calling Notification. Please Help Thanks.
Due to Apple's limitation, app dev can only play notification tone up to 30 seconds. It will play default notification tone if your tone is longer than 30 seconds.
If your notification tone is gone after 5 seconds, try to set your notification presentation options to list, badge and sound.
Once there's no banner for notification, your 30seconds tone will play till the end.
Unfortunately there's no legal way or any workaround to have notification ring continuously such as alarm clock. Hope I was wrong though.
https://developer.apple.com/documentation/usernotifications/unnotificationpresentationoptions
What I think, max you can do is to present a local notification. But yes that would not be exactly like an Apple Alarm notification. In Reality, you don't have permission for this. You can only present the local notifications.

UNNotificationRequest to send local notification daily at a specific time

Since UILocalNotification was deprecated in iOS10, I'm having trouble understanding how to update the following code to the UNNotificationRequest framework. Im basically letting a user schedule a daily notification at a time of their choosing. For example, if they want to get a notification everyday at 11:00AM. The below code works for iOS versions below iOS10 but since UILocalNotification is deprecated, it no longer works. Any help is greatly appreciated.
let notification = UILocalNotification()
notification.fireDate = fixedNotificationDate(datePicker.date)
notification.alertBody = "Your daily alert is ready for you!"
notification.timeZone = TimeZone.current
notification.repeatInterval = NSCalendar.Unit.day
notification.applicationIconBadgeNumber = 1
UIApplication.shared.scheduleLocalNotification(notification)
You can use UNCalendarNotificationTrigger for creating a notification that fires repeatedly using UNUserNotificationCenter. You can do something like this. The trick is to only have the time component in the Trigger date.
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Attention!"
content.body = "Your daily alert is ready for you!"
content.sound = UNNotificationSound.default
let identifier = "com.yourdomain.notificationIdentifier"
var triggerDate = DateComponents()
triggerDate.hour = 18
triggerDate.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error {
// Something went wrong
print("Error : \(error.localizedDescription)")
} else {
// Something went right
print("Success")
}
})
You can't schedule a notification that repeats daily. That notification would happen only once, and then you would have to schedule it again, which means that you would have to open the app again.
There is BGTask API introduces in iOS 13, that can be used to perform some background tasks, but not this one, you can not schedule the task for specific time.This API last only works when app is in the background, not when it is killed. You can only set some time interval that the system will use as a guiding point to determine when to perform you app's code. But in my experience it is pretty unreliable.
The only way to achieve this is to implement remote push notifications. Push notifications also work even when the app is killed.

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.

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