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.
Related
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?
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))")
}
})
My app works perfectly on ios 10 but on ios 11 it does not push local Notification.
Is there any error in my code?
let notificationContent = UNMutableNotificationContent()
let sound = self.defaults.string(forKey: "azan")!
// Configure Notification Content
notificationContent.title = "کاتەکانی بانگ"
notificationContent.sound = UNNotificationSound(named: sound)
//notificationContent.subtitle = self.notificationstring
notificationContent.body = self.notificationstring
// Add Trigger
if(TimeInterval(time) > 0)
{
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(time), repeats: false)
// Create Notification Request
let notificationRequest = UNNotificationRequest(identifier: identifier, 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))")
}
}
}
I have a Firebase listener that launches a notification when it fires like so:
chat.ref.child("lastMessage").observe(.value, with: { snapshot in
let lastMessage = snapshot.value as! String
chat.lastMessage = lastMessage
self.tableView.reloadData()
chat.lastMessageText = "New Message!"
let content = UNMutableNotificationContent()
content.title = "You have a new Message!"
content.subtitle = chat.title
content.body = chat.lastMessage
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger (timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Also have:
override func viewDidLoad() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in })
}
The problem I am having is, although the HomeScreen notifications are working in the simulator, they are not showing on the device. All permissions are granted on the device. Reading some questions here I found some answers that suggested I add the following in the app delegate:
application.beginBackgroundTask(withName: "showNotification", expirationHandler: nil)
but this actually disabled notifications on the simulator too. The notifications did work on the device before but stopped very suddenly. Any thoughts?
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)")
}
}
}