I am looking for a way for my IOS application to exchange information with my server once a remote notification has been received but before the user has taken any action and decided whether he should open the application or not.
In my App Delegate, I have tried something like
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
NSLog("%# remote notification received ", __FUNCTION__)
MyServerJSONRPCRequest.getMyStuff();
completionHandler(UIBackgroundFetchResult.NewData)
}
When the remote notification is received I definitely gets into this but the actual request is never sent to my server, it is sent only if I actually open the app from the application.
Possible explanation : has it anything to do with the fact the request is asynchronous?
2nd possible explanation : I do not have any clue about the right usage of UIBacgkroundFetchResult
Related
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?
I'm using method for background refresh, works when app is active. But in background mode (app isn't killed), can't make Api request to take new data from server.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> ()) {
case .active:
// works
...
completionHandler(.newData)
case .background:
// doesn't refresh / make api request
completionHandler(.newData)
}
Also remote notifications are enabled on background mode Capabilities.
Any solution for refreshing/ fetching data in background mode?
Maybe the following question, and the answer on that question can help you since it is a similar problem, maybe even a 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.
I've read a lot of answers here and I can't find answers of this questions
How to stop badge, alert and sound from notifications when the application is not running ?
How to stop badge, alert and sound from notifications when the application is in Background?
in this function:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
I've put completionHandler([]) without [.badge, .alert, .sound] and I still receive notifications in background when the app is runned.
Also I have function in my code that sends notification when somebody is typing to you and you see that he is typing to you when you are in-app, but when the application is not runned you are receiving notification with badge alert sound? How to prevent that?
PHP Code:
$body['aps'] = array(
'content-available' => 0,
'typingNewMessage' => 'true',
'senderId' => $your_id
);
Two things first: Are you using websocket/socket.io for the chat in your app? And as for notifications, have you set up remote notifications in background modes?
Background modes.
Read up on the docs for push notifications, it sounds like you're adding steps here, or missing some. Basically as kathayatnk mentioned, his method should give you what you want. So if you haven't activated remote background notifications:
To configure your app to process silent notifications in the
background
In the Project Navigator, select your project.
In the editor, select your iOS app target.
Select the Capabilities tab.
Enable the Background Modes capability.
Enable the Remote notifications background mode.
If this is done, you should receieve them in the background..
Also, you are using the wrong method for recieving background notifications in the background.. As stated in the same link above:
In iOS, the system delivers silent notifications by calling the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method of your app delegate. Use that method to initiate any download
operations needed to fetch new data.
how i usually use it :
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
guard let aps = userInfo["aps"] as? [String:Any] else {
//handle something here..
return
}
//handle the content here...
}
You can also perform background tasks from inactive states here by playing with the return value of completionHandler. I suggest you read up on it too.
For your chat issue...
Clarify how you're performing this...
If you're using a websocket/socket.io architecture:
I would strongly recommend not using notifications to notify a user of anything with APNS when using a websocket. Websockets are very efficient and much quicker. Handling both can greatly complicate things. Stick to the socket approach, and use a method server side that checks if a user is still within the app. Something like a ping-pong method like here. Have that method switch to APNS delivery when the socket client ping method shows that the user has left the app or has closed it. This means, if the user is still in the app, but not in the conversation, stay with the ping approach but program the UI response differently (take FB Messenger or Snapchat's notification tabs for example).
If you are not using websockets/socket.io:
...Switch to websockets technologies or packages (depending on your server platform type) or socket.io or use firebase realtime chat services.
Let me know your setup like i asked earlier, but if youre doing all of this already and still getting the issues, then post your code.
Personnaly, i've used RawWenderlicht for APNS guides for over a year, good stuff if you ask me.
To silent push notification the payload should contain content-available key as:
{
"aps": {
"content-available": 1
}
}
Also, you need to enable Remote Notifications in Background Modes under Capabilities setting of your Xcode project.
Now your app will wake up in the background when it receives one of these push notifications.
didReceiveRemoteNotification will be called in AppDelegate whenever the push notification is received.
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
{
let aps = userInfo["aps"] as! [String: AnyObject]
if aps["content-available"] as? Int == 1
{
//Your code
}
}
For more on how to handle push notifications in foreground/background refer to: https://www.raywenderlich.com/156966/push-notifications-tutorial-getting-started
you can add silent push notifications (set content-available key appropriately) when sending pushes, like below.
//when you want the badge and sound
aps {
content-available: 1
}
//When you want silent notifications
aps {
content-available: 0
}
This will call the appropriate delegate method but wont display badge and sound
I am integrating Silent Remote notification in my application, all is working fine. But the issue is when my app is in killed state or not running state then no application delegate methods are getting triggered.
When I see in device log I can see:
"Springboard : High Priority Push: - App killed".
Can anybody please help me out.
I have implemented
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Swift.Void)
When app is in background and foreground state then above method is getting trigger.
I have enabled back ground fetch and Remote notification in the capabilities
Note:
I am sending silent push notification through terminal like this :
apn push <device_token> --certificate <path_of_pem_file> --payload '{"aps":{"content-available":1}}'
Thanks in advance