APNS push working in foreground but not background - ios

Is it possible to have foreground push come in, but not receive push notification when the app is in background?
I've had some users report this problem and was wondering what could be the cause. Maybe it's just confusion? Please enlighten me if anyone else had the same problem and if so what the cause was. Thank you!

Perhaps you could exclude the alert in the payload dictionary of the push. This might prevent the message from being displayed on the device you push to. You could however include a message in the dictionary using a custom key . If you have not registered for background fetch, then it should not update the content of your app when the app is in background. In the didReceiveRemoteNotification method of your App delegate you can create a UIAlertView to show to the user by referring to the custom key. I guess it depends on what actions you are trying to prevent when the user receives the push.

Since iOS 7.0, there is a new message that your UIApplicationDelegate can respond to when a push notification arrives while the app is in the background: In addition to the application: didReceiveRemoteNotification:, there is now a method application: didReceiveRemoteNotification: fetchCompletionHandler:.
The older method is only called while the app is in foreground or when the user slides the notification sideways on the lock screen. The new method with the additional fetchCompletionHandler: parameter is always called. Have a close look at the reference, which describes a few important details on how to respond to this message.

Related

Prevent auto generated push notification

When we receive a push notification while the application is in the background mode, it automatically uses aps and other parameters like Sound, Badge, Alert to generate a notification that appears on top.
What I want is to prevent that auto generated notification let it call didReceiveRemoteNotification and generate my custom local notification and display it on top and notification centre.
The reason behind this is that the message is customised according to situation which is managed locally after some data received in push notification.
I haven't tried anything, because I couldn't find any solution for this kind of scenario. I don't even know whether it is even possible or not.
Any help or other suggestion that could to solve this other way is highly appreciated.
You can have a look to iOS Silent Notifications here
But you have 2 differences cases :
1 your app is in front, the did receive remote is called, and the notification is not displayed.
2 your app is in background, and I don't think the didreceiveRemove is called.
So in the 2 situations, you will not achieve your goal. You shroud maybe try to customize the notification before sending it
Even when your App is in the background, suspended, inactive or terminated it is woken by a Silent push notification.
(Note: the only scenario when the app is not woken by silent push is when it had been killed off by the user from the control centre)
So you can send a silent push, perform whatever operations you need to perform on the data and then generate a local notification which would go in the tray.

iOS Push notification- Handle/Edit message in the 'alert' tag before showing it in the notification in status bar

I am receiving a native language in the 'alert' tag in the push notification. Due to some issues with string encoding the message is shown as roman characters. So I want to fetch the message in the 'alert' tag and edit it before showing it as the notification. I tried didReceiveRemoteNotification callback and I don't think it's being called. How can I do this?
There's a different behaviour for push notifications when the app is in the foreground/background and when the app isn't running at all. When the app is running, whether in the foreground, you'll receive a call to didReceiveRemoteNotification: and you'll be responsible for handling the notification's display (the operating system won't do it for you).
When the app is in the background, you'll receive a call to didReceiveRemoteNotification: only if/when the user presses the notification banner/alert displayed to him/her by the operating system. If the user chooses not to interact with the notification, your app won't even know about it.
If the app isn't running at all, the behaviour will be similar to when the app is in the background, only you'll receive the push notification indication and data in the userInfo dictionary of application:didFinishLaunchingWithOptions:
So, if your question refers to cases in which the app is in the foreground, that shouldn't be a problem and you can do anything you want with the alert field before displaying it to the user. Otherwise, it's the operating system's responsibility to show the alert so that makes it a bit more complicated. Try using the tools available to you for localizing the alerts, as described here: Change language of alert in banner of Push Notification. Hopefully, it will help. Good luck.

Unable to get push notification data when app hasn't yet been launch

I implemented the support for push notification and it is working like a charm. However, in one particular scenario, it doesn't give me the desired result.
When the app is running (state is either active or is in background), whenever a push notification is received, the function "didReceiveRemoteNotification" is called. BUT, when the app is terminated and a push notification is received, I believe "didReceiveRemoteNotification" NEVER gets called (can't test this scenario using Xcode as app hasn't been launched yet). This prevents me from loading a particular view controller to the user.
Can someone please tell me how I can get the data corresponding to the push notification when received while the app hasn't been launched yet? Is there some other function that I need to look into for this particular scenario ONLY?
Appreciate some help.
Per the documentation:
If the app is not running when a push 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 push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
In particular, the key you should be looking for in the launch options dictionary is UIApplicationLaunchOptionsRemoteNotificationKey.

How can I handle unread push notifications in iOS?

I have a iOS 5.1 application that registers to the APNS service to receive notifications. The register is successful and I receive the notifications correctly. The problem comes when I try to handle the notifications.
Once the application is running, the method didReceiveRemoteNotification in the AppDelegate is called correctly and so the notification is handled as intended. This, however, only happens when the application is running on the foreground.
However, when the application is running on the background or is simply stopped, that method is not called. I've read that you should add some lines to the method didFinishLaunchingWithOptions method to obtain the notification from the userInfo dictionary, and handle it. This works just fine, but ONLY when the application is opened by clicking on the notification at the Notification Center. This means that if you open the application by clicking on its badge, or simply by changing context if you were running it on the background, the app never realises that a notification came in. Additionally, if more than one notification was received, we can only handle one of them at once by clicking on the Notification Center, which is a pain :-)
Is there any way to read the pending notifications in the Notification Center? I know there is a way to flush them using the method cancelAllLocalNotifications but I haven't found a way to just read them. And I really need to handle all of them. I thought of implementing a communication protocol with the third-party notification server to retrieve the information again when the application comes to the foreground, but since the information is already in the operating system I would find it strange if it's impossible to access it somehow.
So, does anybody know a way to do it? Thanks in advance.
When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.
For example, if a chat functionality is provided in your app and you send chat messages via push notifications then you should also keep chat messages on the server. If a user clicks 'Cancel' on any push notification then that chat message will not be displayed on the iOS device. In that case when a app comes in foreground later, you make a call to the server and fetch all the past chat messages (sent via push notification).
Ok, So a possible solution would be to have another database table with the messages in with a 'read' flag and a messageID field? Which by default the read flag is NO, then when the app successfully reads this and displays, it updates the flag to YES?
And with only 256 bytes to play with, what sort of ID field length would be necessary?
Edit,
Executed this plan and its working successfully.

Background Capablity on iOS

What are the requirements and how would I go about implementing the Application Push Notification Service to trigger methods when the application is in the background, or when the phone is closed. Would this even be possible?
Thank you
No thats not possible.
When the app is in the foreground it will receive the push notification directly and can do whatever it wants to in response to that.
However when its not in the foreground the notification is displayed to the user (if they haven't disabled them) and/or displayed in the notification center (if they haven't disabled that). Your app will be brought to the foreground to execute if and only if the user selects the notification.
The application does not receive the notification directly if its not in the foreground.
Read the Apple Docs:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1
It boils down to lots of setup, and then implementing application:didReceiveRemoteNotification. You can read an in-depth example here. Part two goes into the actual application:didReceiveRemoteNotification implementation.

Resources