Custom notification-like popup - ios

When push notification arrives, and app is in foreground, I need to show custom popup. It must come from the upper edge of the screen, stay visible to a few seconds and then disappear back behind the top edge.
My question is how do I show it? As I know, in order to make view visible, I must add it as a subview to some existing view. But push notification may come at random time, so I do not know beforehand which view controller will be active at that moment.
So: where do I attach my custom view to (to make it visible on top of everything)?

You need to implement the following delegate in your appDelegate and your push notification will appear in foreground as well.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
And on tapping on the notification you can get the same behavior as you do when app is in background.

Related

How to Update App Badge with Local Notification

I used local notification to deliver a message to the user at the same time I want to update App badge when the notification triggers, but the local notification delegate have functions that deal with notifications when the app is in the foreground and when the user interacts with notification (like a tap on it). Is there any way to update the app badge when the notification triggers and the app is in background?
Handle notification when the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// run code when app in foreground
}
Handle notification when the tap action on it
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// run code when user interact with notification only
}
To update App badge when local notification trigger set the badge value in the code bellow when set the notification:
func schedulNotification() {
let content = UNMutableNotificationContent()
content.badge = 1 // That will appear on app icon when notification trigger
// Continue notification settings...
}

Unable to trigger didReceive notification delegate when you bring the app from background to foreground

I have implemented push notification for ios10. Tapping on the notification alert would trigger "didReceive" delegate, were i save the notification in coredata or silent notification if i m in foreground. The problem is if i receive a stack of notification in background and When i bring my app to foreground from background, Is there a possibility to call "didReceive" delegate or any other push notification delegate were i could sync my items to coredata.
Note
I don't want to sync(didReceive or any delegate) the items in background using silent notification nor tapping on the alert. It should sync automatically the stack of push notification when i bring the app to foreground
func handleInboxNotification(didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let inboxValue = userInfo["inbox"] as? String, let value = inboxValue.boolValue(), value {
let mappedMessage = MessageCenterSDK.shared.constructReorderedDictionary(userInfo: userInfo)
MessageCenterDataManager.shared.synchronize(messages: mappedMessage)
messageCenterDelegate?.didFindNewMessages(hasNewMessages: true)
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
/// Handle outbound remote
handleInboxNotification(didReceiveRemoteNotification: response.notification.request.content.userInfo)
completionHandler()
}
I was facing the same issue, but my target was for iOS 13 & I'm using BGTaskScheduler to fetch data from the server in the background as well in terminated state.
I too want to trigger the notification when the app is in background. But not tapped but that seems to be not possible so we have changed our implementation by enabling background mode and it worked for me.
Hope it also helps.
For more reference on BGTaskScheduler, https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler
you need to use willpresent delegate in appDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// Change this to your preferred presentation option
completionHandler([])
}
This delegate is called when a notification comes in foreground

Get Action of Push Notification Allow Button

I'm working on a ios Application,
now I want to get the action of push notification's allow button,
the user will be push to second view controller once user select one of push notification option(don't allow, allow).
You need to call these methods in AppDelegate Class.
//Ios 10 delegates for Push Notifications
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping ( _ options: UNNotificationPresentationOptions) -> Void){
print("Handle push from foreground")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void){
print("Handle push from background or closed")
}

How to determine if a user views a notification on lock screen by using default view button

I have been working with Rich Push Notifications (IOS)
and want to know is there any way to determine if a user views a notification on lock screen by default view button
You can't know if the user "viewed" the notification external to your app but you can know a few other things:
If the notification was sent
If it was received by the phone
If the user clicked on the notification
The second and third items can be tracked in AppDelegate through this method:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping() -> Void) {

iOS Notification creating an Alert

I'm creating an iOS-Native app. I'm using XCode 8, testing on iOS10, using swift 3.
I'm dealing with APNS and sending Remote notifications (through OneSignal).
The notifications work flawlessly outside my app (when it is in background). However, when I have the app in foreground, no matter what I pass into the completionHandler() (ex. [.alert, .sound], or [.sound] or []) the system creates a UIAlertController with the contents of the notification (title and body) and presents it to the user. This is incredibly obnoxious as a user-interface and I would imagine there is some way to work around it.
I'm implementing the UNUserNotificationCenterDelegate as follows:
extension ViewController: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
//This is my BRYXBanner notification presentation/
let banner = Banner(title: notification.request.content.title, subtitle: notification.request.content.body, image: nil, backgroundColor: .blue, didTapBlock: nil)
banner.alpha = 0.9
banner.animationDuration = 0.2
banner.position = .top
banner.show(genView, duration: 3.0)
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch(response.actionIdentifier){
//switch cases for actions/
default: break
}
completionHandler([])
}
}
I'm also implementing the UIApplicationDelegate but that one simply has print statements.
What I'm seeing when I receive a notification is this:
Screenshot of a custom notification I sent for this
I've seen others have had this issue (How to disable default notification alert view on iOS remote notifications?) but have no idea how they got around it. Most posts simply lead to "oh, I fixed it" with no further explanation.
How can I bypass the showing of this notification, whether it be silencing the notification (if that works) or dealing with the completion handler or intercepting the notification before it is displayed?

Resources