Since UILocalNotification is deprecated in iOS 10 so I have updated my local notification flow using the UNUserNotification framework.
The app plays custom sound using AVPlayer when the app is in the foreground and it works fine. But in background mode when local notification is triggered, instead of custom sound, a default notification sound is being played.
However, things were working fine in iOS9, using "didReceiveLocalNotification" method app can play custom sound even in background mode.
Update 1:
I'm setting local notification as follows:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
if let audioUrl == nil {
content.sound = UNNotificationSound.defaultSound()
} else {
let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
}
content.userInfo = infoDic as [NSObject : AnyObject]
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let identifier = "Reminder-\(date)"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if error != nil {
print("local notification created successfully.")
}
})
I have already gone through many SO Questions but didn't get the solution.
Any help will be greatly appreciated!
In Swift 4.2 and Xcode 10.1
For local notifications to play sound
let content = UNMutableNotificationContent()
content.title = "Notification Tutorial"
content.subtitle = "from ioscreator.com"
content.body = " Notification triggered"
//Default sound
content.sound = UNNotificationSound.default
//Play custom sound
content.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: "123.mp3"))
"123.mp3" file needs to be in your Bundle.
Found out that alertSound doesn't have the correct file path hence the content.sound sets nothing. I was saving the recorded file in documents directory and the file path retrieve for real device is different as compared to the simulator.
Setting a sound file which is placed in project bundle did the trick
content.sound = UNNotificationSound(named: "out.caf")
In your push notification package you need to include the custom sound you want to play in the background. This sound needs to be included in your application too.
For example, here is how I create a custom notification package in PHP for iOS. Notice in my notification object I have a sound object.
array("to" => $sDeviceID,
"data" => array($sMessageType => $sMessage,
"customtitle" => $sMessageTitle,
"custombody" => $sMessage,
"customimage" => "$mIcon") ,
"notification" => array("title" => "sMessageTitle",
"text" => $sMessage,
"icon" => "$mIcon",
"sound" => "mySound.mp3"));
Here is how to create a notification using the UserNotifications framework with a custom sound.
If notification is your UNNotificationContent and the name of your custom media file that is in your bundle is a.mp4, just use the following code:
notification.sound = UNNotificationSound(named: "a.mp4")
So, you don't need to use didRecieveLocalNotification.
I understand that
let notificationContent = UNMutableNotificationContent()
...
notificationContent.sound = UNNotificationSound(named: "out.mp3")
is working fine, but:
1) the sound file should not be longer than 30 sec (which is ok, I think)
2) the sound file has to be part of the Bundle.mail files. If it is stored somewhere else the notification will not have access to it, when in background. It works fine in foreground. Apple does not provide permission to access other folders as far as I know.
3) there are restrictions on the sound format. Please refer to Apple guides
4) I'm not sure if sound files in Bundle.main can be changed
programmatically. For example: I wrote a routine which extracts 30 sec of a song stored in apple music and chosen by the user... everything works fine, if the info is stored in a different directory, but only in foreground... once locked or in background there is no access to it. I'm not able to change/overwrite a file in Bundle.main. If that would be possible the problem would be solved ... but I think that would be a security issue. I conclude that alert sounds and music are working fine as long as the above is met.
let notificationContent1 = UNMutableNotificationContent()
notificationContent1.title = "Good morning! Would you like to listen to a morning meditation to start the day fresh ? Or maybe a cool relaxing soundscape? Open the app and browse our collection which is updated daily."
notificationContent1.subtitle = ""
notificationContent1.body = ""
notificationContent1.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "AlarmSound.wav"))
var dateComponents1 = DateComponents()
dateComponents1.hour = 08
dateComponents1.minute = 00
let trigger1 = UNCalendarNotificationTrigger(dateMatching: dateComponents1, repeats: true)
let request1 = UNNotificationRequest(
identifier: UUID().uuidString,
content: notificationContent1,
trigger: trigger1
)
let notificationContent2 = UNMutableNotificationContent()
notificationContent2.title = "Good evening! Hope you had a great day. Would you like to meditate on a particular topic ? Or just some relaxing sleep sounds ? Open the app and pick what you want so you can go to sleep relaxed."
notificationContent2.subtitle = ""
notificationContent2.body = ""
notificationContent2.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "AlarmSound.wav"))
var dateComponents2 = DateComponents()
dateComponents2.hour = 20
dateComponents2.minute = 00
let trigger2 = UNCalendarNotificationTrigger(dateMatching: dateComponents2, repeats: true)
let request2 = UNNotificationRequest(
identifier: UUID().uuidString,
content: notificationContent2,
trigger: trigger2
)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request1, withCompletionHandler: nil)
UNUserNotificationCenter.current().add(request2, withCompletionHandler: nil)
Related
I have an app where the user needs to complete a task every day. If the user does not complete a task, he/she gets a reminder at 8am the next day with a phrase prompting to complete the task.
We would like to send a phrase every morning but we don't want it to be the same phrase every day.
This is what we have right now:
static func scheduleDailyUnwatchedNotification() {
let notificationMessages = ["Phrase one", "Phrase two", "Phrase 3", "Phrase 4", "Phrase 5"]
let totalMessages = notificationMessages.count
let randomIndex = Int.random(in: 0..<totalMessages)
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: ["dailyReminder"])
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = notificationMessages[randomIndex]
content.sound = .default
var dateComponents = DateComponents()
dateComponents.hour = 8
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "dailyReminder", content: content, trigger: trigger)
center.add(request)
}
The problem is that even though a random phrase is selected, the notification will always repeat with that same random phrase.
How can we have it repeat with a different phrase?
You’ll need to schedule each different phrased notification manually. However if you’re sending a phrase every day and you have say 50 of them you could schedule each to repeat every 50 days. Then whenever the user opens the app you can always swap around the days the notifications are sent - so the phrases ordering isn’t always the same. It’s not the most ideal but does allow recurring notifications with different titles.
Alternatively if you want to be able to change the notification titles without new app releases/have more control you can use push notifications. This way you can set up a backend to send messages but it does have more overhead from a server perspective.
I am adding a local notification feature to my iOS app which will fire up a notification / alert on a specified time e.g. 7am daily. So I setup the alert using the code below:
let content = UNMutableNotificationContent()
content.title = "Planned transactions due today"
content.body = "You have [NUMBER] scheduled income and [NUMBER] planned expenses due for today."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
let date = Date(hour: 7, minute: 0, second: 0) //custom initializer from Date extension
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "Reminder", content: content, trigger: trigger)
center.add(request) { (error) in
if let error = error {
print("Error scheduling notification: \(error.localizedDescription)")
}
}
It works perfectly. But what I want to achieve is to add more useful information within the local notification's body, like replacing the [NUMBER] to an actual number which is derrived from a query from core data.
I need help on how to pull this off. I've looked at implementations using UNNotificationServiceExtension and UNNotificationContentExtension. But I can't seem to make it work. I was unable to find any implementations of UNNotificationServiceExtension that applies to local notification – I could be wrong but I think it's only for push notifications.
I've also tried implementing UNUserNotificationCenterDelegate on the AppDelegate to intercept the notification as it is being triggered. func userNotificationCenter(willPresent:) method allows me to get the notification but there seems to be no way for me to change the .content property as it is get-only.
So basically, I just want to be able to change the body content of the notification before it is being displayed.
My solution isn't the best, but to achive this, i do the following at each app launch:
first cancel the next notifications,
then update it with a new content.
It works fine if the app is used at a regular basis.
there is some something called BGTaskScheduler. Apple said that it is a
A class for scheduling task requests that launch your app in the
background.
https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler
Maybe you should check for it
I want to make an app just like iOS Reminders app. My problem is the custom repeat part. We can set custom repeats like "Every 2 Months on the third Monday"(the screenshot below) but I don't know how to implement this sort of repeats with User Notification.
What should I have do?
Although the question might be abroad to be fully answered, I would post an answer that should scratch the surface of how you could achieve it by using user notifications.
If you are aiming to let the displaying of the notification is based on a specific date/interval -as you mentioned "Every 2 Months on the third Monday"-, then you should work with UNCalendarNotificationTrigger:
The date and time at which to deliver a local notification.
Example:
import UserNotifications
// first, you declare the content of the notification:
let content = UNMutableNotificationContent()
content.title = "Notification Title"
content.subtitle = "Notification Subtitle"
content.body = "Notification Body"
// now, you should declare the UNCalendarNotificationTrigger instance,
// but before that, you'd need to describe what's the date matching for firing it:
// for instance, this means it should get fired every Monday, at 10:30:
var date = DateComponents()
date.weekday = 2
date.hour = 10
date.minute = 30
// declaring the trigger
let calendarTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
// creating a request and add it to the notification center
let request = UNNotificationRequest(identifier: "notification-identifier", content: content, trigger: calendarTrigger)
UNUserNotificationCenter.current().add(request)
iOS 10 introduced push notification framework updates,
UserNotificationsUI.framework
As written on apple docs, it lets us customize the appearance of local and remote notifications when they appear on the user’s device.
So if anyone have idea how to display image in push notification when on lock screen. same like andorid push notification are doing.
Thanks,
If you want to customize the appearance of local and remote notifications, perform the following steps:
Create a UNNotificationCategory and add to the UNUserNotificationCenter category:
let newCategory = UNNotificationCategory(identifier: "newCategory",
actions: [ action ],
minimalActions: [ action ],
intentIdentifiers: [],
options: [])
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([newCategory])
Create a UNNotificationContentExtension:
then use code or storyboard to customize your UIViewController.
Add category to UNNotificationContentExtension's plist:
4.Push Notification
Local Notification
Create a UNMutableNotificationContent and set the categoryIdentifier to "newCategory" which includes UNUserNotificationCenter's categories and UNNotificationContentExtension's plist:
let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"
let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request)
Remote Notification
Set "mutable-content : 1" and "category : newCategory". Note that the category value is set to "newCategory" which matches what you previously added to UNUserNotificationCenter and UNNotificationContentExtensions plist.
Example:
{
"aps" : {
"alert" : {
"title" : "title",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "newCategory"
},
"otherCustomURL" : "http://www.xxx.jpg"
}
Note: you need a device or simulator which supports 3DTouch, otherwise you can't show a custom UNNotificationContentExtension viewcontroller.(In iOS10 Beta1, it`s not work. But now this work without 3d touch)
And ... if you just want to show an image on a push notification displayed on the lock screen, you need to add UNNotificationAttachment:
let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"
let fileURL: URL = ... // your disk file url, support image, audio, movie
let attachement = try? UNNotificationAttachment(identifier: "attachment", url: fileURL, options: nil)
content.attachments = [attachement!]
let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request)
For more detail feature,Demo
Actually if you are setting up a local notification and you're just interested in having an image show up in the notification itself, you don't have to bother with NotificationsUI.framework or UNNotificationContentExtensions at all. That is only useful if you want a custom UI when the user 3D touches or expands the notification.
To add an image that is already on the device (either shipped with the app, or generated/stored by the app at some point before the notification is created), then you only need to add a UNNotificationAttachment with a file URL where the path ends in .png (or .jpg will likely work too?). Something like this:
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = #"Title";
content.body = #"Body";
content.sound = [UNNotificationSound defaultSound];
NSURL *imageURL = [NSURL URLWithString:#"file:/some/path/in/app/image.png"];
NSError *error;
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:#"image" URL:imageURL options:nil error:&error];
if (error)
{
NSLog(#"error while storing image attachment in notification: %#", error);
}
if (icon)
{
content.attachments = #[icon];
}
Then when the notification appears, the image will show up on the right-hand side of the notification banner like it does for Messages notifications. And you don't have to jump through all of the hoops of setting up a content extension with a categoryIdentifier, etc.
EDIT: Updated to add that this is only a valid solution for local notifications.
You have to do some work on creating push notification and also when you are handling.
When you creating payload you need to add extra attribute attachment, something like below:
{
aps : {
alert: { },
mutable-content:1
}
my-attachment = "url to resource"
}
When you receive notification system will call didReceive method of service extension, override notification extension didReceive method like this
public func didReceive(_ request:UNNotificationRequest, withContentHandler contentHandler:(UNNotificatinContent) -> Void) {
let fileUrl = //
let attachment = UNNotificationAttachment(identifier : "image", url: fileUrl, options: nil)
let content = request.content.mutableCopy as! UNMutableNotificationContent
content.attachment = [attachment]
contentHandler(content)
}
Here is WWDC video talk on this topic.
Implement your notification's custom view using a UIViewController that conforms to UNNotificationContentExtension.
See https://developer.apple.com/reference/usernotificationsui/unnotificationcontentextension
I am trying to set the local notification sound to my downloaded audio.
But when notification trigger no sound is played. I don't know where i am going wrong. Please have a look on code and let me know my mistake.
func setNotificationWithDate(date: NSDate, onWeekdaysForNotify: [Int], snooze: Bool, soundName: String, title: String, vibrate: Bool) {
let AlarmNotification: UILocalNotification = UILocalNotification()
AlarmNotification.alertBody = title
AlarmNotification.alertAction = "Open App"
AlarmNotification.category = "AlarmCategory"
AlarmNotification.timeZone = NSTimeZone.defaultTimeZone()
let datesForNotification = correctDate(date, onWeekdaysForNotify:onWeekdaysForNotify)
for d in datesForNotification
{
AlarmNotification.fireDate = d
let tracksDic = appDelegate().alarmTrackArray.objectAtIndex(d.dayOfWeek()!)
let sound = tracksDic.valueForKey("t_id") as? String
AlarmNotification.soundName = sound! + ".mp3"
print(AlarmNotification.soundName)
let userInfo = ["date":date,"soundDetail":tracksDic,"title":title ,"vibrate":vibrate]
AlarmNotification.userInfo = userInfo
print(AlarmNotification.soundName)
UIApplication.sharedApplication().scheduleLocalNotification(AlarmNotification)
}
}
Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4—pretty much anywhere that explains how to convert sounds for iOS will tell you how to do that), and is under 30 seconds.
Reference Link :
Choose custom sound for local notifications
Update according to your comment :
Downloaded file will not be in your bundle. so it will not play that sound!!!!