Is is possible for an application to handle the response from a push notification? - iphone

Is it possible to get the user's response from a push notification and create a notification reply? i.e. if a user clicks the view button and views the app, can we take that action and let the sender of the notification know that the receiver has viewed / opened the app?

Short answer, Yes.
When the application starts due to a user electing to from a notification (local or remote) the application is started in a special way so that the application can process the notification. At this point it can do whatever you want, like send a message back to your service.
See Handling Local and Remote Notifications for details.

Related

How Google's Gmail app on iPhone disable notifications?

My question can seems weird, but I just realize that when I receive a push for a new mail from Gmail app and I open Gmail on a web page, the notification disappear.
Can someone explain me how this is working ?
This is related to this question, though it's not an exact duplicate.
This can be implemented with Apple Push Notifications.
Your server has to maintain for each user a list of device tokens of all the devices belonging to that user (iPhones, iPads, etc...).
When the user reads the message on one platform (whether via a web browser or via a mobile application), you can execute some API call on your server to notify it that the user read the message.
Then your server can send a follow-up push notification to all the devices belonging to that user with "badge"=0 (and without the "alert" and "sound" fields), and the notification will be cleared from the lock screen and app icon.
One possibility is that they use notifications delivered in the background (they get delivered directly to the app rather than being shown to the user), then use local notifications. Local notifications can be cancelled.
So it would go like this:
remote notification "there's a new e-mail" delivered in the background
local notification "new email"
...
you connect to the website
remote notification "user has seen new e-mail" delivered in the background
cancel local notification
I've never actually tried to do this, but I suppose that would be the way it works. I'm not aware of any (documented) way to cancel a remote notification directly.

How does WhatsApp know if the user gets the push notification even if the app is runnng in the background and is not open?

I have already asked related question:
any option to know if apple app get the push notification?
And I got an answer that it is not possible to know if push notification comes to the iPhone when application is in the background and the user does not have it opened...
But I found something interesting:
I take 2 iPhones with WhatsApp. I open, in the first iPhone, WhatsApp and exit it (the WhatsApp is in the background now), then with the second iPhone, I send WhatsApp massage to the first iPhone.
When the push notification of WhatsApp reaches first iPhone I can see two "V" (symbols) near to the message (and I don't open the Push notification). After this I close the Internet & 3G in the first iPhone and again send WhatsApp message with the second iPhone to the first one. - and I can see only one "V" near to the massage (not read - not get push notification).
Now after re-enabling the Internet on the first iPhone and after I get the push notification I can see two "V" (again - I don't open the push notification).
How does it work? How does WhatsApp know whether the iPhone gets the push notification or not while the WhatsApp works in the background?
Let us call the iPhone with WhatsApp application closed - the receiver and the other iPhone you send messages with - the sender, and let us use generic term application instead of WhatsApp
The application at the sender is not able to learn that push notification has reached the receiver. It is by the nature of push notifications.
However, the sender is possible to send another - not a push notification, but prioprietary protocol - message to the application at the receiver which it (the receiver) is welcome to acknowledge to the sender in any application-specific way it wants.
So, I would guess (I don't know - I'm not affiliated with WhatsApp in anyway) that the double-v icon you get is when application message reaches the application at the receiver - and has nothing to do with push notification.
What you mean is just the basics of push-technology :
The whatsapp software runs in the background listening on a specific port
for incoming messages. It does this continuously.
BTW :
One V = message delivered from your mobile to the whatsapp server
Two V's = message pushed from whatsapp server to recipient mobile
Blue V's indicate message has been read
Whatsapp doesn't show if the person reads the message or not. It just informs when the message has been sent to the whatsapp servers(one V) and when it has been sent the person phone (2 V's). The double does not mean the message was read it just means that whatsapp connected to the phone and was able to pass on the message.
After receiving a push notification the receiver can download data from a URL that you include in the payload of your push notification. By requesting that URL the server knows that the receiver received the push notification and can pass this status on to your sender application.
Like so:
Add a specific URL in your push notification payload
Implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method in your app delegate and in that method call the URL from the payload.
Implement a logic on your server that recognizes the URL call as a received notification
Let your sender application continuously get the current notification status from the server.
The above posters are all correct. Just to add one more thing. If Whatsapp has recently been closed, it is still running in the background and can still receive the message (hence the return receipt). However, after the app is left unused for a while (or other apps have higher priority) Whatsapp is terminated, then it will no longer receive incoming messages and will not return receipt.
Of course the observant person would notice a small lag between opening the app and the new messages being displayed which seems to suggest the messages are not downloaded in the background but only downloaded upon opening the app. So why does it return receipt while running in the background?

any option to know if apple app get the push notification?

I build xcode app that get push notification, the main problem is that the push notification is very critical for me.
so I want to check if the push notification is delivered to the device with the app installed, I understand that if the iphone dosn't have internet connecction / 3G the push notification is not getting to the device.
how can I check if the device get the notification or not?
how can I check if the APNS successful to deliver the push notification?
I want to send sms if the push notification is not deliver to the device so I think about the idea to get the notification event when it's open by the push notification, and to send request to my server so i can know if the push notification is successful deliver or not. the main problem is that the user need to open the app every time he get the notification and in the night it's a problem. so this option is not good for me.
I check the feedback server push notification but i don't find any info that I can get if the push notification is delivered or not
any idea??
With iOS7 you have a new method called
application:didReceiveRemoteNotification:fetchCompletionHandler:
which you probably could use for your task. From Apple's Docs:
Implement this method if your app supports the remote-notification background mode.
...
When a push 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. Use this
method to download any data related to the push notification. When your method is done,
call the block in the handler parameter.
Unlike the application:didReceiveRemoteNotification: method, which is called only when
your app is running, the system calls this method regardless of the state of your app.
The short answer, you can't, since APNS is one way. However, since an app can execute arbitrary code upon receipt of a notification, you can use this to say, send an http request to your own server when the notification is recieved.
There are any number of reason why push notifications might not get delivered to your user, or might not be delivered in a timely manner. Apple does not provide any mechanism for you to query the status of a push notification that you have sent.
If your app is currently running on the user's device and the user is accepting notifications for your app, you can implement the following method in your app delegate. It would be called whenever a push notification is received and in this method you could send a request back to your server to indicate the message was received. However this will only work while the user is running your app.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In general though, it sounds like you'e relying on push notifications for something you shouldn't. From Apple's Local and Push Notification Programming Guide:
Important Because delivery is not guaranteed, you should not depend on
the remote-notifications facility for delivering critical data to an
application via the payload. And never include sensitive data in the
payload. You should use it only to notify the user that new data is
available.
There is no way to find out whether the notification was delivered to the device or no. APNS is a one way service. If there is no internet connection on the device then the APNS server will hold the last notification for some period of time which is no specified by Apple. If a new notification is sent to APNS for delivery then the old notification data is lost and replaced by the new data if its undelivered. If the notification is delivered then also the old notification data is deleted on the APNS server.
Please go through the following link : Apple Push Notification
Hope this helps you...........
If you are using JAVAPNS to send the APNS notification, you can use the below:
List<PushedNotification> notifications =
Push.combined("alert", badge, "default", "cert.p12", "certpassword", true, deviceToken);
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
//Push is successful. Do your thing...
}
else {
//Push is not successful. Do your thing...
}
}

How can I control the Apple Push Notifications I receive

I have successfully created a server that sends Apple Push Notifications, and my iphone receives them.
For example I have Notification of type A and notifications of type B,
How can I control the types of notifications I received on the iphone side? For example I only want type A and not B (Just like Facebook, I want notifications for Friend Request, but not for Walls)
Thanks
you cannot stop your device on receiving a specific type of notification unless it is done on server side. Though you can ignore a notification when app is in running state as you get the notification in didReceiveRemoteNotification and you can simply ignore it after checking it but if app is in background or it is closed then you cannot control the incoming notifications from within your app.
you can make a service on server to set preferences for notification types.
from device, user can enable/disable the push service for individual features and update the preferences on server from device.
On server, before sending the PUSH, you can check for the preferences selected by user from the table and send only those notifications which the user has opted for.

Can push notification be sent without an alert for certain actions?

I know you can register to have alerts or not when you call the push notification API. However my problem is that I want a certain class of actions to have an alert notification while no alert notification for other class of action?
So for example, an alert should be shown when we send the notification "Heart rate dropping alert!". But no alert should be shown when we send the notification "downloading updated patient data", the app should just take the notification as an instruction to being download if it is launched. And simply ignore it if it is not launched.
How to implement this?
Check Silent Push Notifications for iOS 7.In the WWDC 2013's "What's New with Multitasking" presentation, there is a section about Silent Push Notifications.
You can embed custom JSON data in the push notification, look at The Notification Payload in the Apple docs.
Update: I don't think that quite answers your question. You can send a blank notification that has the effect of cancelling any previous push notification (including those from other applications). I'm not sure if the app gets notified of that when it is actually running. If it does you might be able to do that in conjunction with a custom JSON payload to achieve what you want?
{"aps": {"badge": 0}}
You probably know this already - you can't use a push notification to launch the app on the iPhone without the user seeing a popup (apps can never run in the background on the iPhone).
However, you can display a different popup message and include different JSON data in the notification. Then if the user presses the button to launch the app ("Start", or whatever you call the button on the right) that JSON data is passed into the app. Your app can then carry out a different action based on that data.
Not possible. Push notifications cannot initiate tasks - nothing can cause an app to execute without user action. Similar question to Can I use Push Notification for this. You can trigger a sound, a text alert, or a badge value. That's it.