Limitations of Apple Push Notifications

Limitations of Apple Push Notifications Image

 

*Bluetube was acquired by Perficient in October 2016. To learn more, please click here.* 

I was adding Push Notifications to an iOS app recently, and ran into a few places where Push Notifications didn’t work exactly as expected.  I’ll go into more detail on Push Notification behavior in general, and strategies to use them correctly in iOS applications a little further down this post.  However, I think it’s important to understand what the Push Notification is really intended to be used for.

Please note, this is a high level overview of Apple Push Notifications.  This post is intended to help the reader gain a conceptual understanding of Push Notifications for iOS and how Apple intends them to be used.  The post WILL NOT go into technical detail on actually implementing Push Notifications into your application, nor will it cover building a server to send Push Notifications.

Push Notifications in iOS are essentially Apple's way of getting around not letting apps just continue to "run" in the background all the time.  They are really just to let the user know "something happened", or "is about to happen" that he or she should be aware of.  At the very basic level (unless related to a calendar event or something else related to the native OS), the Push Notification is really just to say, "Hey, open my app!", then it’s up to the app to do its thing.

The responsibility of the app to take over once the Push Notification has alerted the user is the part that I think most of the misconceptions surround.  For example, when I began implementing Push Notifications into the app I mentioned earlier, I was under the impression that I could use information contained “inside” the Push Notification to make the app react accordingly.  As I proceeded, I realized that this is only the case if the app is launched FROM the Notification itself.  Also, I assumed that, if I sent three separate Notifications to a client, when the client selected one of them and launched the app, I could remove ONLY the selected Notification from the clients “Notification Center”.  This is not the case at all.

So let’s go through an example scenario to demonstrate:

For the purpose of this explanation let’s assume:

- The server sent the client (iOS device) a simple Push Notification.

- The Push Notification contains a message: "New Push Notification!"

- The Push Notification displays the same message to the iOS “Notification Center”

- The Push Notification adds a “1” badge to the app’s icon

When the client receives the new Push Notification, there are several things that can happen depending on:

1)     If the app is currently running in the foreground

2)     If the app running in the background, or the app is not running at all.

Let’s go through each, and I’ll explain how the OS and the app will react:

1)     The app is currently running in the foreground.

-          In this case, the code in your app written to handle the Notification will be run (and will have access to all the “data” contained in the notification)

-          The “1” badge will NOT be added to the app’s icon

-          The Notification’s Message will NOT be added to the iOS “Notification Center”

2) The app is currently running in the background, or not running at all.

-          The system presents the Push Notification; displaying an alert, adding the message to the "notification center", adding a badge to the app icon, and plays a sound.

Depending on the user's settings, the alert will be either: a "popup" style alert with a "Close" button and a "Launch" (Action) button, or a "banner" style alert (which goes away automatically) to which clicking the "banner" does the same thing as the Action button on the "popup" style alert.

Now the user can do a few things:

A) Touch the Action button ("View" button) on the Notification Alert:

-          The system launches the app, and the code in your app written to handle the Notification will be run (and WILL have access to all the “data” contained in the notification).  Remember though, you must now remove ALL of you notifications from the "notification center", not just this specific one.

B) Dismiss the Notification Alert via ("Close" button)

- The Notification Alert is closed

C) Click the notification in the "Notification Center"

-          The system launches the your app, and the code in your app written to handle the Notification will be run (and WILL have access to all the “data” contained in the notification).  Again, you must now remove ALL of you notifications from the "notification center", you cannot reference a specific notification to remove.

D) Open the your app via the app’s icon on the Home screen

- Here is where the unexpected results occur.  There is no way to access the Push Notifications in the "notification center" associated with your app, or any of the data associated with the Notifications.

The callback (didReceiveRemoteNotification or didFinishLaunchingWithOptions in Objective-C) function that is called when a new push notification is received cannot be called or referenced if the app is opened via its app icon.  Crazy, I know.

So there are a couple possible solutions:

1) When the app is opened via the app's icon, remove the badge from the app's icon and remove the notification from the "notification center".  (I can access the app icon's current badge number, and setting it back to zero removes the notification from the "notification center")

2) Like #1, check if there is currently a "badge" on the app icon, and if there is, send a simple message to the server to tell it the app was opened without showing the "notification" (if the notification was displayed, it would set the badge back to zero so the simple message to the server shouldn't be sent).  The server could immediately reply with either another push notification, or just a response that the app could parse and display a similar message as the push notification.

Conclusion?

When integrating Apple Push Notifications into your iOS application's experience, it is very important to remember that the application itself may or may not receive the data contained in that Push Notification.  So anything that your application should do in response to receiving a Push Notification (aside from displaying a badge) must be handled manually by your application, totally separate from the Notification itself.

-Colby Williams

Posted in: Xamarin Mobile and Tablet | Permalink

Posted by Colby Williams