I made a lot of search during many days (20 posts+) but I didn't the solution to my problem.
I have an application which receive push notification, I can receive those remote notifications but when I tap on it, the delegate "didReceiveRemoteNotification" is not called (I want to intercept the payload to do action in fonction of it).
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("test")
}
In advance, thanks for help.
The didReceiveRemoteNotification method does not fire when the user launches the app by tapping on the notification and not when the notification is received, never.
Sorry for my bad english and if you have question, don't hesitate.
First import UserNotification framework and then implement UNUserNotificationCenterDelegate in ios10 to support push notification in ios10
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//Handle notification tap
}
Related
I'm trying to listen user clear push notification action. I've the code below, It's working when the app is on the foreground but when i put the app on the background it doesn't go into that function. Is there any way that i can do some coding when the user clear the notification on the background state?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if(response.actionIdentifier == UNNotificationDismissActionIdentifier){
...
//I need to do some coding here!
}
}
I use this for receiving remote notifications behind the scenes:
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// Process this userInfo dictionary
processNotification(dictionary: userInfo)
}
In my willFinishLaunchingWithOptions in AppDelegate I call this:
fileprivate func setupAPN(application: UIApplication) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
MobilePlatformPush.setRemoteDelegate(delegate: self)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
application.registerForRemoteNotifications()
}
I'm working on an app that handles push notifications.
For some reason,
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
Works as expected, but
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
is never called.
Anyone ever come across this situation?
All the usual suspects are in place:
UNUserNotificationCenter.current().delegate = self
is set in didFinishLaunchingWithOptions
let center = UNUserNotificationCenter.current()
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
center.requestAuthorization(options:options) { (granted, error) in
if error == nil{
DispatchQueue.main.async() {
UIApplication.shared.registerForRemoteNotifications()
completion(granted)
}
}
}
is returning true (permission granted)
Other items worth mentioning:
The Push notifications are sent via SendBird
When the app is in the background, everything works as expected.
Times and again, read the documentation.
Apple states for func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void):
Asks the delegate how to handle a notification that arrived while the app was running in the foreground.
That is, only when your app is running in FORGROUND and you get a notification will the system call this method to ask you whether this notification should still be shown to the user since they're already inside your app. You can call the completionHandler and pass in options like alert and sound so the user will still see the banner and hear the sound. If you only pass back sound, then the user sees no banner and only hears the notification sound.
I am using FCM for push notifications. I used to refresh the content of the app using the below method
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Now with iOS13 this method is not firing anymore. I have included apns-push-type and apns-priority as well.
I found the issue.
UIApplication.shared.registerForRemoteNotifications()
this has to run for every launch. Better to keep this in didFinishLoadingWithOptions method. In my previous version, I used to call this for the first time but looks like it has to be for every launch.
and make sure set delegates for notification and messaging also.
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if #available(iOS 13.0, *) {
// your code
completionHandler(UIBackgroundFetchResult.newData)
} else {
}
}
also check your APNS certificate on FCM, it must .p12 type must be available in keychain that you are using for your current XCode version
I need to get local notification data from lock screen or apps was kill. Is there a way to detect it or any handler that would trigger after show?.
i try using UNUserNotificationCenter present and UNUserNotificationCenter didReceive but still not working..
response.notification.request.content.userInfo contains the notification data
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
completionHandler()
guard let bodyText = response.notification.request.content.userInfo["body"] as? String else {return}
}
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any])
{
if application.applicationState == .inactive || application.applicationState == .background
{
//opened from a push notification when the app was on background
}
}
I'm trying to debug why I can't get messages from firebase on my iPhone,
I am connected to the FCM server, and I've also subscribed to a topic, however, when I try to print data recieved from FCM I get nil values.
This is the code I have from google's tutorial, it gets executed so that does mean the app is being notified correctly that a new topic is available on FCM?
Also how can I debug to see if there's something I'm doing wrong in setting FCM up?
Thanks
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
FIRMessaging.messaging().subscribe(toTopic: "/topics/newNotificationtest")
print("Message ID: \(userInfo["gcm.message_id"])")
debugPrint(userInfo)
// Print full message.
print("%#", userInfo)
}
Please Try With this code and debug
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
let aps = userInfo["aps"] as? NSDictionary
print(aps)
print(userInfo)
print("Message ID: \(userInfo["gcm.message_id"]!)")
print("%#", userInfo)
}