Firebase Notification not receiving in App inactive state? - ios

How can we store firebase notifications in iOS app, if the app in kill state (inactive)?

I think this method should work:
Use this method to process incoming remote notifications for your app. 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. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {...)

Related

APNs: Change the app badge while the app is the foreground

I have an app and a server-side push sender. When new notifications arrive, the server sends an empty push message which only contains a badge update.
When the app is in the background, the badge is successfully updated. However, when the app is in the foreground, the badge is not updated at all - the push is delivered to the app, which discards it.
The obvious workaround is to catch the push and update the badge from within the app. For some technical reasons this would take some time to take effect (development time, app store check time, users who don't frequently upgrade etc.)
I wonder if there's a way to circumnavigate this and update the badge using a server side APNs push regardless of the app state, foreground or background.
Is there a way to change an iOS app badge using a push message, when the app is in the foreground, without handling the push notification from within the app?
This can only be achieved through application delegate methods defined in your AppDelegate
Deprecated in iOS 10
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
or,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
The above delegate functions gets called when app is in foreground there you can decode your Push Payload and assign the application badge as follows
[UIApplication sharedApplication].applicationIconBadgeNumber=[[userInfo objectForKey:#"aps"] valueForKey:#"badge"];
Cheers.

PushNotification in BackGround

This method is called when a push notification is received while the app is in background mode and the user clicks on notification. But I want to call a method when notification comes in background mode without the user to click on the notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"%#",userInfo);
}
For this to happen you need to enable Background mode for remote notifications. You can find it Capabilities section of the project. Besides that your incoming Notification Payload should contain content-available:1 key-value pair.
Then this method will be called immediately without user interaction.
Note: This works if the app is in Background or suspended state. If the app is completely killed or force quit by the user, it will not work.

Will AppStore reject my app if i enable remote notifications in UIBackgroundModes?

I have an app that uses silent Push notification. To be more precise, My application should update the application badge when a push message is received even if it is running in background. So for that I have enabled Remote notifications under Background Modes and implemented the below method in the code. I want to submit the app to the AppStore. So whether AppStore rejects my app as i enabled the Remote notifications in Background Modes?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

(silent) RemoteNotification

In an iOS app using remote notifications (coming from Parse.com) I have arrived, after spending some time researching on the subject, to the conclusion that I should not use remote notifications in the background or possibly use silent notifications.
But silent notification seem quite complex.
For example:
In this code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"application:didReceiveRemoteNotification:fetchCompletionHandler:");
completionHandler(UIBackgroundFetchResultNewData);
}
The fact of adding the last line (completionHandler) prevents an error message.
But what does this do exactly? Where does this completionHandler come from?
I have never touched silent-notifications and I am kind of lost there.
The last line is completing block that will be called when you application is in background.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
Above method get called when your application in running in background OR foreground mode.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
If you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives.
When a remote notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Launching your app in the background gives you time to process the notification and download any data associated with it, minimizing the amount of time that elapses between the arrival of the notification and displaying that data to the user.
Let me know if you need anymore clarification.

IOS: Apple Push Notifications understanding

Four questions about Push Notifications.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//app is in foreground I can fetch
} else {
//App is in background, can I still fetch the server? Is there a time limit?
}
}
Using the code above, when the app is in background, can I still fetch the server? Is there a time limit? If the answer is: No, I cannot fetch the server in the background, are there alternatives?
Is there any other benefit of using silent notification besides triggering some method to run within 30 seconds when the app is in the background?
If using silent notification and the app is in the foreground, is the following method still called?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
}
In the payload does "content-available" : 1 calls the method in Q3 (above)? or is there anything else that can trigger the method in Q3?
Yes you can still fetch the server when the app is in background. For many apps, the background state is just a brief stop on the way to the app being suspended.
No this is the main benefit of using silent notification see this link.
Yes application:didReceiveRemoteNotification:fetchCompletionHandler: is called regardless of app state.If app is suspended or not running, then the system wakes up or launches your app and puts it into the background running state before calling the method.
This method is intended for showing the updated content to the user.When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. If the handler is not called in time, your app will be suspended.
Yes it calls application:didReceiveRemoteNotification:fetchCompletionHandler:, If you want a push notification to wake your app up in the background you need to enable the Remote Notifications capability and then implement the code to handle that remote notification (either by detecting it in the application:didFinishLaunchingWithOptions: if your app is not already running, or by implementing application:didReceiveRemoteNotification:fetchCompletionHandler: in the case your app is already running). In response to the remote notificaiton you would internally trigger your fetching code. Of course you also need to be doing registering for the remote notifications and sending the token to your server. please see this discussion

Resources