iOS - Read the push notification once Received - ios

I have 4 scenario
When the app is not Launched
Given the app was not Launched or Killed
When the push notification receive
And opened the app without tapping the notification
Then the app should capture the notification.
When the app is running in foreground
Given the app running in foreground
When the push notification receive
Then the app should capture the notification.
When the app is running in background
Given the app is running in background
When the push notification receive
And opened the app without tapping the notification
Then the app should capture the notification.
When the app is not Launched and cleared the notification
Given the app is not Launched or Killed
When the push notification receive
And user cleared the notification
And opened the app
Then the app should capture the notification.
The first 3 scenario works fine with the following code
The last scenario is not worked When the app is not Launched and cleared the notification
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: { requests in
for request in requests {
self.setNotification(userInfo: request.request.content.userInfo as NSDictionary)
}
})
}
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
setNotification(userInfo: userInfo as NSDictionary)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
setNotification(userInfo: userInfo as NSDictionary)
}

According to your query
When the app is not Launched and cleared the notification
Given the app is not Launched or Killed When the post notification
receive And user cleared the notification And opened the app Then the
app should capture the notification.
This is not possible for a Normal Push notification unless the user interacts with that notification. You may want to try Silent notifications, these are not shown in the UI but the control reaches the app and you can use the data/payload from there in your code.
When user clears the notification from bar, there is no way to fetch that information.
You could also try to add the same information that is being sent in the push within an API and call it once the user opens the app.
This link deals with all the detail involved. According to your implementation, you can try a combination of both.

Related

how to process the FCM notification without typing the pop-out windows on IOS

I am new to Swift and Firebase, I implemented the FCM perfectly, I am processing the data inside each notification and display it. However, I had a problem: Instead of tapping the pop-out windows of notification, I later tapped the APP ICON and the notification wasn't got processed and no data was updated on my app page.
Can someone please tell me how to fix this? My intuition is to write something in AppDelegate, but I had already had the same process in func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) and application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) as the other function has. Thank you!
I later tapped the APP ICON and the notification wasn't got processed and no data was updated on my app page.
That's expected. If your app has 10 notifications received and you tapped on the app icon, how is your suppose to know which notification it needs to process?

iOS push notification: how to store push notification payload when application is kill or terminate in swift?

I am sending multiple push notification using firebase. My application getting all notification. Now how to store all push notifications data in user default when application is killed or terminate?
How to store all push notifications data when application is killed and the user click one push notification from all notification?
Your app is not getting invoked just with a push notifications. What you receive in the Notification tray is handled by iOS directly and the content of the push notification, specifically what is inside aps key of the Notification body is used to populate the content you see in the notification tray.
The app will not get invoked just because you received a push notification if the app is in suspended or killed state.
Unless of course your apns-push-type is set as background and content-available = 1 in your notification.
The call would go to
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Where you may process the contents as required by the app.
To detect if the app was launched from a Notification,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
if let dict = launchingOptions?[.remoteNotification]{
//App launched from Notification
if let userInfo = dict as? [AnyHashable:Any]{
//Do what your app requires with the notification using the userInfo.
}
}
}

Push Notification not received while app is in foreground using firebase [duplicate]

I have a strange case. My swift ios app is connected to Cloudkit. If the app is NOT running (background state), I receive my notifications badge and alert just fine, every time!
If the app is running, no notifications are received! I know it is not hitting the remote because I do this:
1. Adding a breakpoint to the didReceiveRemoteNotification event
2. Running xcode in a plugged iphone
3. NSLog("detected didReceiveRemoteNotification"), so final code look like this
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
NSLog("detected didReceiveRemoteNotification")
}
I know the error is not coming from cloudkit or from APNS because I do receive alert banner and badge when the phone is in the background state.
Can you guide me to set this up properly for the Foreground state!?
I am running ios v9.3
UPDATE #1
I think the wording of the documentation is poor. It clearly says that both run on the foreground, which is what I cared about; nevertheless, the fix is more accurate than the documentation!
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background.
You've implemented the wrong method. Implement:
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
As the docs explain:
Unlike the application:didReceiveRemoteNotification: method, ... the system calls this method when your app is running in the foreground or background.

How to handle Push on App not running state?

Is it possible to open the iOS App based on push notification content? I believe it's not possible. Is there any other way around? I believe we can go with the widget in iOS10, right? Please suggest some good solution?
Thanks!
Whenever your payload has content-available:1 your app will get called in the backgroundstate as soon as it arrives and will call application(_:didReceiveRemoteNotification:fetchCompletionHandler:). It will only not get called if the user killed the app manually. (This doesn't launch your app. It requires your app to be launched though)
Having that said if your app was terminated (by user) but the user taps on the notitication then your app is launched from didFinishLaunching...delegate method.
Whenever the user taps on push notification the application launches from not running state to running state the in AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
}
in this function you can check application is launch from push or application icon
var notification: [AnyHashable: Any]? = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [AnyHashable: Any])
if notification != nil {
print("app received notification from remote\(notification)")
application(application, didReceiveRemoteNotification: notification)
}
else {
print("app did not receive notification")
}
Yes we can not open ios applications on push notifications but we can wake up the app on push notifications. Its called VOIP notification. You can have more info regarding this from below link https://github.com/hasyapanchasara/PushKit_SilentPushNotification

Use Push Notification payload when App launches via the Notification

I was wondering how I can make use of the payload of a push notification, when the user is launching the app via opening that notification. Is this possible?
I want to implement an action which happens upon opening a notification, but this action requires part of the payload to work.
Cheers
Just implement the application(_ application:, didReceiveRemoteNotification userInfo:) in your AppDelegate. The userInfo is a JSON representation of your Push Notification (including the Payload).
Additionally you can check if the App is "inactive", wich means (in this case), that the User just came in to the App via this Push Notification
Example Code:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if (UIApplication.shared.applicationState == .active) {
// you just got a PushNotification, but the app is running currently
} else if (UIApplication.shared.applicationState == .inactive) {
// Do your work in here, the User just opened the App via the Push Notification
// check userInfo for the Payload
}
}

Resources