Local notification delivered in background

Hi Guys!


I'm not sure is possible to do, but is there a way to update the value of a variable when a local notification is delivered and the app is in background?


I'm trying to update the badge icon but it seems there is no way to do it for local notification...


Thanks

Replies

I think it is possible. Look at the documentation :

h ttps://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html


If your app is not running, or if it is in the background, the system displays local notifications directly to the user. The system can alert the user with an alert panel or banner, with a sound, or by badging your app’s icon. If your app provides a notification content app extension, the system can even use your custom interface to alert the user. If your app is in the foreground when a notification arrives, the system gives your app the opportunity to handle the notification internally.

NOTE

Local notifications are supported only in iOS, watchOS, and tvOS. In macOS, apps do not require local notifications to badge their icon, play sounds, or display alerts while running in the background. Those capabilities are already supported by the AppKit framework.


Did you watch WWDC ? There are 2 sessions on notification, notably this :


h ttps://developer.apple.com/videos/play/wwdc2017/708/


Best Practices and What’s New in User Notifications

Session 708 iOS, watchOS

Local and push notifications enable your app to keep users informed with timely and relevant content, by displaying messages and accepting users' actions, or playing distinctive sounds, or updating the badge on your app icon. Service extensions enable your app to decrypt and augment push...


I think the second one is less relevant

Rich Notifications

Session 817 iOS, macOS, watchOS

Discover the keys to creating informative, useful and beautiful rich notifications in iOS. Get practical and detailed guidance about how to design short looks, long looks, and quick actions that will make your app's notifications something people look forward to receiving.

Hi Claude31 Thank you for your answer... I'm new to swift (been coding just sonce June...).


Haven't watch the WWDC and I will definitely do it now that you've shared the link, thanks!


As far as the other link you shared is concerned:

Are you referring to this delegate method: ?


UNNotificationDismissActionIdentifier


Because I have seen that page but probably due to my inexperience I have seen just the possibility to interact with the app if is in background and the user take some actions.

Now reading with a bit of more attention I believe this delegate might fit for my solution...Am i right?


Thank you!

I have watched the WWDC... maybe it's me... but haven't found the answer (watched also the 2016 WWDC).


I'm trying to auto-increment the badge icon.

If i assign the badge upon registration it won't work...


let content = UNMutableNotificationContent()
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber


If i register another notification before the first one is delivered the badge will still be one.


What I am trying to achieve is to autoincrement the badge either if the app is in the background or in foreground.


When is in foregorund the willPresent method it's called upon delivery and i can use it to increment the badge. But if the app is in background? how can i trigger a method which will increment the badge?

Have you seen Apple's example ? Here it is for a scheduleed notification:

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
                                                        arguments: nil)
// Configure the trigger for a 7am wakeup.
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
// Create the request object.
let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)


You will have to look in detail at the whole doc to configure exactly for your app.

h ttps://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/index.html#//apple_ref/doc/uid/TP40008194-CH3-SW1

Hi Claude31 and thanks again for the answer... probably i'm missing the part you are talking about.


I have read all that doc, already yesterday. Unfortunatly i don't see any reference to autoincrement the badge icon.

it talks about scheduling and so far so good. I've been able to schedule teh notification and to get it delivered.


The problem is that when the notification is delivered the icon badge is not updated, this is because i don't understand how to auto increment the


UIApplication.shared.applicationIconBadgeNumber when the notification is delievered. Exactly i wouldn't know how to do it when the app is in background since the willPresent method is not called in background


Tnak you!

In the next chapter of the same document :

h ttps://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html#//apple_ref/doc/uid/TP40008194-CH4-SW1


did you request authorization ?

And register notifications ?


But look here: badge can only be updated from server when in background:

h ttps://stackoverflow.com/questions/31379868/local-notification-badge-number-not-more-than-one


More interesting, someone seems to have found a turnaround for local notification (it is not possible to update directly):

h ttps://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification

look at last answer for Swift 3 solution

Hi Claude31


auth is requested, the notification is registered and scheduled and so it's delievred.


I have all the chapter and i've also seen that answer on stackoverflow... basically you confirming the issue... it is not possibile to auto update the badge with local notifiaction bust just with push... yet i wonder how apple does it with their native app (like calendar...)

I almost cannot belive they did not give a wau to do so... but it's 3 days that i'm looking around the web and seems nobody has a solution for this...


Thanks anyway! 🙂