Is there a way to post and show local notifications in the foreground?
If not, is there a way to "fake it"?
I need to make an iOS appear like an Android app, which does have notifications in the foreground.
For iOS 10 and next you can use UNUserNotificationCenterDelegate and see this post for more information. If you app run on 8 or 9 ios you should use 3d party libs (you can search)
Sample:
Add it to your delegate methods in the AppDelegate and conform to this protocol - UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.badge])
}
Related
Though till Swift 3 we used didReceiveRemoteNotification userInfo: [AnyHashable : Any] method its working fine, but in Swift 4 its not working. Does anyone have any idea about it ?
For displaying notification while app is in foreground, use the following method.
// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
According to apple documentation, you can display notification while app is running
Before iOS 11 , when the app receive a notification while running in the foreground , the notification will not appear unless using a custom view to show it.
Is the way we handle the received push notifications while app is running in the foreground changed in iOS 11?
Try this in Appdelegate class
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
this is the delegate calls when app is in foreground and if you want to show alert as well then add .alert in completionHandler.
It is added in iOS 10 onwards
Further Ref : Silent pushes not delivered to the app on iOS 11
I have APNS connected to my app.
The problem is in different behavior of system delegate didReceiveRemoteNotification. It is working properly only when the debugger is attached to the app, otherwise, it is not called when the app is in the background state. For example, the delegate is called if test phone is connected to the mac via USB cable and app is being debugged. Right after cable disconnection delegate is not called anymore.
The question is how app state (debugged or not) influence on didReceiveRemoteNotification behavior. (both IOS 10 and 11)
To detect delegate call I make backend requests and I'm sure there is no problem with call detection.
I had the same problem, spent a lot of time looking for a solution. I found a workaround mentioned in the Apple forums.
Make the class where you handle the push notifications a delegate of UNUserNotificationCenter
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
and then implement the willPresent notification: UNNotification method
#available(iOS 10.0, *)
extension APNSService: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
Log.debug?.message("Received push in foreground through UserNotificationCenter, calling Inbox.incrementalSync")
...
completionHandler([])
}
}
I am writing a reminder app in iOS 10; my local notifications are send using the UserNotifications framework. Sending the notification works fine already; my problem is rather the background handling of the notification.
Earlier days, you could use didReceiveRemoteNotification in the app delegate for handling stuff like userInfo; now, UserNotification has apparently it's own methods.
I want to detect, generally, if in my absence a notification has arrived. Case: I received it, I tap open the app icon, bam: alert controller that says: you've received a notification.
I am using these both functions for it:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")
UserDefaults.standard.synchronize()
print("\(notification.request.content.userInfo)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("Handle push from background or closed")
UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")
print("\(response.notification.request.content.userInfo)")
}
But they ONLY work, if I access the app by tapping on the notification in Notification Center. So, how do I detect if I've received a notification in the scenario that I don't enter the app through the notification, but through the app itself?
I have been struggling with this problem for days and I have looked at every post about this and nothing works.
I am using FCM (Firebase Cloud Messaging). FCM talks directly to the app when in foreground, but it will use APNS when the app is in background.
I am trying to call a method everytime the app got notification by calling the method in the push notification's delegate function
I have these 2 delegate functions:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
and
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
which does not get called in background, according to apple documentation.
I also have this function which I believe should get called in the background but it does not.
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
I have all of these functions in ViewController
In background mode, I get banner notifications, but none of the delegate is called. Is there any way to make any of them work in the background, if not what should I do?
In addition I also get this error : <FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2 in the log everytime I send a payload while the app is in the background.
Here is the payload that I send using postmate:
{
"priority":"high",
"notification":{
"sound": "default",
"badge": "1",
"title":"mytitle",
"body":"mybody",
"message":"Hello"
},
"content_available":true,
"to" : "/topics/myTopic"
}
I already turned on push notification in capabilities and checked push notification in background modes under capabilities.
I use Xcode 8 and have been testing on iphone 6 iOS 10.0.2
There can only be 1 delegate for a protocol.
In this case, I made APPDelegate and ViewController conform to UIApplicationDelegate. And only delegate functions in APPDelegate got called. That's why the one in ViewController is not called.
The solution would be just put didReceiveRemoteNotification in the AppDelegate.