I'm using Firebase to send silent remote notification to an iOS app. When running the app from Xcode it works fine both when the app is in foreground and background. It's fast and it works.
But, if I stop running from Xcode and start app from my device the notifications is not received regardless if the app is in the foreground or background. Sometimes I get the notification after 15+ minutes, but not always. Sometimes the notification appears if I open the app.
My notification have priority 10, content_available true and data. It does not have a title/body since I only wan't to trigger a network request and the show a local notification to the user based on the result of the network request.
Any ideas why this is?
Edit: If I include a title/body in the payload, the notification is delivered directly even without running from Xcode, but it doesn't seem to allow me to run code in the background until the notification is activated (application:didReceiveRemoteNotification:userinfo is not run).
Related
Right now I have a framework that receives a silent notification, get the data from it (custom data) and translate it into a local notification to show the alert to the user (this is donde in didReceiveRemoteNotification:fetchCompletionHandler method). I have implemented this framework on an app and everything seems to be working correctly, silent notifications are being process when the app is in background and foreground. However, when the app is killed by the user or it is not running, I cannot receive notifications because of this:
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. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
The reason I use this method for showing notifications is because the payload I sent to APNS has custom data with key-values that indicate how the notification must behave.
I've been doing some research and I found that Pushkit for VoIP can do the job. However, many post suggest that this can cause app rejection.
So my question is, how can I achieve receiving remote notifications even if my app was killed and considering that data in the payload has custom information to build the notification?
Silent push notifications are unreliable: they might get delayed, delivered in groups or even not delivered at all.
If you need to modify the content of the notification before presenting a banner for the user, you should use a Notification Service App Extension. You can also share some information between your app and this extension - using app groups or the keychain - if it needs something from your app to process the notification data.
When I receive a remote notification I updated a counter that I save to UserDefaults and I also show a local notification. Everything works as expected when the app is in the foreground, background, and suspended states Ref. When the app is in the Not Running state my counter is not updated nor is my local notification shown.
It is my understanding that I should be able to receive and process Remote Notifications while the app is completely off. A few articles online claim that when a Remote Notification arrives while in the Not Running state that the application:didFinishLaunchingWithOptions: should be called followed by application: didReceiveRemoteNotification:fetchCompletionHandler: but in my case it is not.
Is it possible to receive remote notifications while in Not Running state?
If your app is a VoIP app and you are using VoIP pushes through PushKit then a push notification will launch your app from the terminated state in order to deliver the notification. If you are using standard push notifications then your app will not receive the notification if it is terminated. You can include an alert text with a 'silent' notification that will be displayed to the user in the event that your app is terminated in order to prompt them to launch your app.
First of all, it sounds like you have a silent notification set up. Once you add alert data to your push notification (information like the title, body etc.. you can find more on that here), it'll start to display on the lock screen.
Second, it's not possible for your application to launch from a push notification, silent or otherwise, if it's in the Not Running state. The documentation on this is actually incorrect, as it states that the application will only not be launched if the user has quit the app. However, this actually also applies for any circumstance under the not-running state, for example if your app has never been launched since installation/rebooting, or if it was quit due to memory (a fairly common occurrence - iOS purges apps which haven't been run recently as required).
did you check this mark when app is background?
I'm using a combination of Firebase and Batch for a chat like application.
When a push notification is received the app should be able to perform some tasks based on the data received in the notification.
From my understanding this can only be done in app states suspended -> active and not terminated.
I have it working when the app is in the foreground and also when closing the app via home button.
Should the device be locked however, didReceiveRemoteNotification:fetchCompletionHandler: is never called. At least not until the user interacts with the notification from notification center.
Is this correct? Can no work be done on a remote notification if the device is locked?
I have all the appropriate entitlements and "content-available":1 set correctly.
Is there another way that I can wake the app up from a notification and do a few background tasks? I also tried using Notification Service Extension but had the same issue with nothing happening while the device was locked.
According to Apple's documentation on remote notifications:
Discussion
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. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
In my case (iOS 7.1.1) remote notification is not delivered to the app after the app was killed (swipe up from Recent Apps List) and the phone was restarted. If I open the app, notifications get delivered as expected. What am I missing?
Edit: To avoid any misunderstanding. What I expect is the following flow:
User kills the app;
User restarts the phone;
App server sends a new message;
OS attempts to launch the app and deliver the notification.
The problem is that you are a developer, and your usage is not typical. What you do: Launch the app, swipe it out, turn off the phone (not standby, but turned off), reboot, enter your passcode, make your server send a notification. That doesn't work.
For some reason, notifications sent within 90 seconds or so after rebooting the phone are not received. Wait 90 seconds, then you send the notification, and it should be received. Since there is no relation between the time the phone of the user is rebooted and the time you send the notification, this is only a problem for developers and testers, not for real users.
From the article you linked, for the method you're referencing (application:didReceiveRemoteNotification:)
If the app is not running when a remote notification arrives, the
method launches the app and provides the appropriate information in
the launch options dictionary. The app does not call this method to
handle that remote notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
remote notification payload data and respond appropriately.
Are you checking for your remote notification payload in the options dictionary for the will/didFinishLaunchingWithOptions:?
I couldn't find anywhere how to debug a silent remote notification.
I know that a normal remote notification can be debugged by setting the project scheme to "wait for executable to be launched" but since a silent remove notification doesn't open the app, it didn't work.
I'm also not sure which method should be called when i get a silent remote notification.
Already tried:
-application:didFinishLaunchingWithOptions
-application:didFinishLaunching
-application:didReceiveRemoteNotification
-application:didReceiveRemoteNotification:fetchCompletionHandler
-application:handleActionWithIdentifier:forRemoteNotification:completionHandler
None of these worked...
This is my payload:
{
"aps": {
"content-available": 1,
"sound":"silent.wav"}
}
Can anyone help me with that?
What's happening is you've got an incorrect payload. In order for it to be considered a silent push notification that will trigger a background fetch, the only thing allowed in the "aps" dictionary is "content-available":1. Since you have a sound, the system ignores the content-available part and sends it on as a regular notification. And since there's no "alert" portion, there's no notification to interact with and no way to launch your app. Remove the sound part and your notification will come through -application:didReceiveRemoteNotification:fetchCompletionHandler
It makes no difference if your app is running in the background or hasn't been started on the device. If the app is not running, iOS will wake it up and deliver the notification after the app launches in the background. If it's been run but it's backgrounded or it's running in the foreground, the notification will simply be delivered to your app. No matter what it still goes to the same method.
There are two other requirements for this to work:
Your device has to have background fetch enabled for your app.
You can't have killed the app manually by swiping up from the multitasking UI. If you do this, iOS will NEVER wake up the app until it is ran by the user again.