I have a macOS Swift app where I am using local notifications. Here is a small methode for sending these:
func sendPushMessage(title: String, message: String, userInfo: [String:Any]) {
let notification: NSUserNotification = NSUserNotification()
notification.title = title
notification.informativeText = message
notification.userInfo = userInfo
notification.soundName = NSUserNotificationDefaultSoundName
notification.deliveryDate = Date()
self.center.scheduledNotifications = [notification]
}
This works for a long time and I have received all my notification (as well all notifications were shown in notification center). But actually the latest notification overrides the previous one. Let's say there is only one notification slot that always get overridenn with the latest notification.
In my notification center there is also only a single notification visible (the latest) instead of all received notifications. I have no idea when this "stops working" but I think one or two month ago? I am still on 10.13.6 high sierra.
The notification settings are correct.
I have no idea whatg I have done "wrong".
Fixed the issue by changing notification order (in notification settings) to manually by app and then it worked. After that I could change it back to newest and still working. Was only a bug in macOS for sure.
As refer documentation, you should set different identifier for each notification.
The identifier is unique to a notification. A notification delivered
with the same identifier as an existing notification replaces the
existing notification rather than causing the display of a new
notification.
You can set like that
notification.identifier = "yourIdentifier"
Related
Hello notifications are not displayed on Mac OS Catalina, here is my code:
func showNotification() {
let notification = NSUserNotification()
// All these values are optional
notification.title = "Test of notification"
notification.subtitle = "Subtitle of notifications"
notification.informativeText = "Main informative text"
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)
}
I noticed that in my mac application now when I open the application a notification request appears. Maybe I have to implement that? But I can not find any documentation on it. How to view local notification...
Even with UserNotification it does not work
The NSUserNotification has been deprecated past MacOS 10.14 as per https://developer.apple.com/documentation/foundation/nsusernotification
Instead you might want to have a look at the UserNotification framework as in https://developer.apple.com/documentation/usernotifications?language=objc
And in addition it is now crucial to request permission from users to authorize notifications.
will system loads Notification service extension and calls its didReceive(_:withContentHandler:) for local notifications in iOS 10?
If yes how we can do that?
No. The accepted answer describes Notification Content Extensions, which allow you to present a ViewController in the expanded notification view, and works with both remote and local notification.
Notification Service Extensions, that let you change the content of the notification (attaching images, etc) do not work with local notifications. However, you can attach images as part of the process to show a local notification.
Notification Service extension is for remote notification not for local notification.
As per apple doc
UNNotificationServiceExtension
An object that modifies the content of a remote notification before it's delivered to the user.
You need to create a Notification Content Extension for displaying custom notification with iOS10. In the Xcode menu bar, go to File->New->Target. Then from the list select Notification Content Extension.
Enter the corresponding details and click Finnish. You will see a new folder with the name of your extension. In the folder, there will be 3 files :
NotificationViewController : Here you can design your custom interface and implement responses.
MainStoryboard : You can use this to design your custom notification.
Info.plist
In the Info.plist file, add the following:
This will be the category identifier you will use in your main project when scheduling notifications.
let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [], intentIdentifiers:[], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
content.categoryIdentifier = "myNotificationCategory"
Your NotificationViewController class should look something like this.
func didReceive(_ notification: UNNotification) {
//change properties of notification here.
}
func didReceive(_ response: UNNotificationResponse, completionHandler completion: #escaping (UNNotificationContentExtensionResponseOption) -> Void) {
//implement response logic here.
}
There are a couple of good tutorials available online. You can check here, here & here.
Hope this helps.
Notification Extension is supported for local notifications too. It is clearly mentioned here
UNNotificationContentExtension
An object that presents a custom interface for a delivered local or remote notification.
I'm not sure if it is technical issues. I tried to implement a scheduled local notification by calling this:
// Schedule the notification ********************************************
if UIApplication.sharedApplication().scheduledLocalNotifications.count == 0 {
let notification = UILocalNotification()
notification.alertBody = "This is local push notification"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate()
notification.category = categoryID
notification.repeatInterval = NSCalendarUnit.Minute
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
from
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
}
It works fine. However, when I revert the code to the origin version without any line of codes related to the scheduleLocalNotification() the local notification still running.
I can't stop it even I clean the project/remove the application from devices before a rebuild.
Please help if possible, thank you!
You have created a repeating local notification and handed it to the system. At that point it is out of your hands. You have told the system to repeat this notification, and it will do so, forever.
Your only choice at this point is to delete your app.
What you have done is a classic mistake — one, unfortunately, that many apps do in fact make. And I have had to delete those apps from my device as well, as there is no other way to stop the repeating notification.
The right thing to do would have been to build into your app a cancelation of that local notification, i.e. you stop the notification in code. For example, you tell the UIApplication shared instance to cancelAllLocalNotifications. But you failed to do that.
I need to check periodically a web page.
My idea is to set a local notification periodically, for example every 20 minutes. When notification leaves, device should load the web page, check a condition and if the condition is true, device should rang, otherwise nothing.
(NOTIFICATION) -> (LOAD WEB PAGE) -> [VERIFY CONDITION]-|if true|-> (RING)
Is this technically possible to do? How can I load a web page while app isn't running?
My sketch of code was like this:
func check () {
pageCode = // find a way to load the page
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_MSEC * 100))
dispatch_after(delayTime, dispatch_get_main_queue()){
let readCode = self.pageCode.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].outerHTML")
if letturaCodice?.containsString("Some text") == true {
ringPhone()
}
}
}
Not possible using UILocalNotification, you could do it with a remote notification though.
Apple has a guide here: See - "Using Push Notifications to Initiate a Download"
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
A remote notification platform I've used before is called OneSignal, it's completely free and allows you to schedule notifications. https://onesignal.com
Edit:
Doesn't answer question about ringing, not sure about that bit sorry!
Option 1:
You could build the app to use background app refresh function that you could cause an alert if lets say you do not get an http status code of 200.
If you use this option you could build in the function to have a custom tone to play that you build into the app.
Option 2:
Use a server side script todo the checking and have it fire off a push notification. This method would depend on phone settings as to how the notification would function.
You can try to use Grand Central Dispatch to run a background process and inside it add notification observer.
UPD: Like this:
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
() -> Void in
// catch notifications here
}
I am using local notifications in my application. I want to show local notifications as Alert Type on iphone devices while it is displaying as Banner Type. I have also set the alert type of local notification that is not contains the null value. Please Suggest.
You can't change the way a Notification is presented by code, iOS handles the displaying the notifications.
Only the user can set the way a notification is presented from the settings app.