ios, swift: group local notifications together - ios

I'm trying to group different local notifications together, but iOS 15.2 seems to ignore the threadIdentifier parameter and it stills display the notifications in separate rows:
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let notificationContent = UNMutableNotificationContent()
// ... more properties
notificationContent.threadIdentifier = "please-group-together" // not working
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: completion)
Am I missing something?

Related

Delay UNLocationNotificationTrigger on UNNotificationRequest?

I have properly set up location based local notifications. As of now the user receives a notification when they enter a defined zone (CLCircularRegion). Id like to delay that notification about 10 minutes after they enter the zone, could that be possible?
// SENDING WITH (UNLocationNotificationTrigger)
let trigger = UNLocationNotificationTrigger(region: destRegion, repeats: false)
let request = UNNotificationRequest(identifier: notificationInfo.notificationId,
content: notification,
trigger: trigger)
UNUserNotificationCenter.current().add(request)
I also send notification locally for other reasons at different places in my app. This causes a delay for the elapsedSeconds.
// SENDING WITH (UNTimeIntervalNotificationTrigger)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: elapsedSeconds, repeats: false)
let request = UNNotificationRequest(identifier: "cartabandon", content: localnotification, trigger: trigger)
UNUserNotificationCenter.current().add(request)
Maybe theres a way to combine the two? Or another option?

iOS - Trigger Notification Service Extension through Local Push Notification?

Is it possible to trigger Notification Service Extension through Local Push Notification?
My code snippet
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
// Adding title,subtitle,body & badge
content.title = "title"
content.body = "body"
content.sound = UNNotificationSound.default
let aps = ["mutable-content": 1]
let dict = ["aps": aps, "some-key": "some-value"] as [AnyHashable : Any]
content.userInfo = dict
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content as UNNotificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
No, It is not possible to trigger notification service extension through push notification.
Reason: Service extension lets you customize the content of a remote notification before it is delivered to the user. e.g decrypt the message or download the attachments
but I personally think there is no point of downloading the notification content attachments in service extension because you can always download the attachments before scheduling. e.g
// Download attachment asynchronously
downloadAttachments(data: data) { attachments in
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
// Add downloaded attachment here
content.attachments = attachments
// Adding title,subtitle,body & badge
content.title = "title"
content.body = "body"
content.sound = UNNotificationSound.default
// No need of adding mutable content here
let dict = ["some-key": "some-value"] as [AnyHashable : Any]
content.userInfo = dict
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content as UNNotificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Let me know if you have different use case.
Pleas read the documentation before posting. The first line states:
A UNNotificationServiceExtension object provides the entry point for a Notification Service app extension, which lets you customize the content of a remote notification before it is delivered to the user.

iOS push notifications enabled but not working

I am trying to set push notifications in my app.
I have enabled my APN certificates..
And I have turned push notifications on in Xcode..
I have turned push notifications on in previous xcode projects and it worked fine. This is the code I have..
//add push notifications every 5 hours
func pushNotifications() {
let pushNotification = UNMutableNotificationContent()
pushNotification.title = "Time to track your water useage!"
pushNotification.badge = 1
let minute:TimeInterval = 60.0
let hour:TimeInterval = 60.0 * minute
let eighthDay:TimeInterval = 5 * hour
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: eighthDay, repeats: true)
let request = UNNotificationRequest(identifier: "timerDone", content: pushNotification, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
I am calling this in my viewdidload func
What am I doing wrong?
You can try this code,
func sendLocalPush() {
let objNotificationContent = UNMutableNotificationContent()
objNotificationContent.title = "Manish Kumar"
objNotificationContent.body = "iOS Developer"
objNotificationContent.userInfo = getUserInfoDict()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
let request = UNNotificationRequest(identifier: "LocalNotification", content: objNotificationContent, trigger: trigger)
/// 3. schedule localNotification
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
})
}
Hope this helps.

How to show multiple local notifications?

I have an messaging app,I am using VoIP notifications to send acknowledgement to users. I am firing a local notification each time the PushKit delegate is called.
The current scenario is that the previous notification gets removed and is replaced by a newer one. Is there a way to manage local notifications such that the user can see multiple notifications in their device?
This is the code I have tried:
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Title"
notificationContent.subtitle = "Subtitle"
notificationContent.body = "Body"
// Add Trigger
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
// Create Notification Request
let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)
// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
}
}
P.S : I don't want to schedule local notification for a later period
Using for loop to register multiple Notification with the unique identifier https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649634-identifier?language=objc
let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)
you should change this identifier "cocoacasts_local_notification" to dynamically reset the unique identifier
let notification = UNMutableNotificationContent()
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dayComponent, repeats: true)
let lnMessageId:String = messageDict["Id"] as! String
let dayRequest = UNNotificationRequest(identifier: lnMessageId , content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(dayRequest, withCompletionHandler: {(_ error: Error?) -> Void in
if error == nil
{
//print("success")
}
else
{
//print("UNUserNotificationCenter Error : \(String(describing: error?.localizedDescription))")
}
})

Local notifications - repeat Interval in swift 3

I want to repeat local notification every week, before iOS10 there is repeatInterval, but i am not able to find anything suitable to repeat notifications in iOS10.
TimeTrigger and calendarTrigger both have repeat as true or false, where can i apply repeat as weekly, daily, monthly.
Thanks.
Try this.
func scheduleNotification(at date: Date, body: String) {
let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = body
content.sound = UNNotificationSound.default()
//content.categoryIdentifier = "todoList"
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}

Resources