How to get app icon badge numbers in iOS7 without push notifications

简介: How to get app icon badge numbers in iOS7 without push notificationsRefresh your app in the background with a new iOS7 featureITworld | Februa...


How to get app icon badge numbers in iOS7 without push notifications


Refresh your app in the background with a new iOS7 feature

ITworld   |  

Using the badge number on your app icon for iOS is a great way to let the user know that there is something new to interact with in your app. Here’s a quick way to achieve this without using push notifications.


The badge number is the little red circle you see on your app icons from time to time, most often mail and the app store notifying you of new items. Before iOS7, the only way to use this feature was to leverage it via Apple Push Notifications. While the push notification service is great, it takes a fair bit of configuration to enable. You need to initiate the notifications from the server side not the mobile device for starters, and the app needs to be provisioned properly to enable them.

Starting in iOS7 however, you can use a new feature called Background App Refresh to periodically poll for new data and update the app icon badge indicator based on that data. This can be done with just a small amount of code, especially in comparison to full blown push notifications.

It works like this, you register your app into a new app registration category called “fetch”. You then implement an event handler in your AppDelegate which will fire when iOS decides it’s going to allow your app to update. The interval in which this happens is unknown because iOS determines it dynamically based on your usage patterns with the app. If you use it a ton, it will fire more frequently, if you hardly use it at all, less so.

To get started, you first need to register your app into the “fetch” category. You do this by editing your app’s Info.plist file, add the key “Required background modes” and set a value of “App downloads content from the network”. If you’re using Xcode 5 you can simply click on your project target, go to the Capabilities tab, expand the Background Modes section, and check the box for “Background fetch".

1_17.png

Next, you need to add a little code to your AppDelegate.m file.

The first bit in the application didFinishLaunchingWithOptions: method lets you polietly suggest to iOS how frequently you’d like your app to be refreshed. It’s good to be responsible with this as it could affect battery life if abused. In this case, I set it to the minimum fetch interval.

The next method application performFetchWithCompletionHandler is the event handler that you need to add in order to perform the data call. In this method you can do whatever logic you need to execute in order to update your app or it’s badge number. Once you have that number, updating the icon is as simple as calling:

[UIApplication sharedApplication].applicationIconBadgeNumber = newItemCount;

Finally, you need to end your performFetchWithCompletionHandler by responding back that you're finished and provide a status such as

completionHandler(UIBackgroundFetchResultNewData);

iOS expects you to return this promptly, within about 30 seconds, otherwise it will start to penalize your app’s background execution to preserve battery life.

As you can see, with just a few lines of code and some minor setup, you can add this welcomed feature into your iOS7 app. Users today expect to be notified when their apps have actionable items, and doing so with just an e-mail or with a barrage of push notifications can become a nuisance. This represents a great, lightweight alternative to keep the user informed and their apps synchronized.

Matthew Mombrea

Matthew Mombrea is a software engineer, founder ofCypress North, and a technology enthusiast.

The opinions expressed in this blog are those of the author and do not necessarily represent those of ITworld, its parent, subsidiary or affiliated companies.


目录
相关文章
|
7月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
248 3
|
7月前
|
iOS开发 开发者
一键制作 iOS 上架 App Store 描述文件教程
一键制作 iOS 上架 App Store 描述文件教程
|
7月前
|
移动开发 安全 数据安全/隐私保护
iOS移动应用安全加固:保护您的App免受恶意攻击的重要步骤
iOS移动应用安全加固:保护您的App免受恶意攻击的重要步骤
86 1
|
7月前
uni-app 185iOS端兼容处理
uni-app 185iOS端兼容处理
78 1
|
7月前
|
iOS开发 开发者
苹果iOS App Store上架操作流程详解:从开发者账号到应用发布
很多开发者在开发完iOS APP、进行内测后,下一步就面临上架App Store,不过也有很多同学对APP上架App Store的流程不太了解,下面我们来说一下iOS APP上架App Store的具体流程,如有未涉及到的部分,大家可以及时咨询,共同探讨。
|
7月前
uni-app 4.9封装badge组件
uni-app 4.9封装badge组件
97 0
|
1月前
|
传感器 iOS开发 UED
探索iOS生态系统:从App Store优化到用户体验提升
本文旨在深入探讨iOS生态系统的多个方面,特别是如何通过App Store优化(ASO)和改进用户体验来提升应用的市场表现。不同于常规摘要仅概述文章内容的方式,我们将直接进入主题,首先介绍ASO的重要性及其对开发者的意义;接着分析当前iOS平台上用户行为的变化趋势以及这些变化如何影响应用程序的设计思路;最后提出几点实用建议帮助开发者更好地适应市场环境,增强自身竞争力。
|
2月前
|
存储 前端开发 UED
uni-app:icon&修改tabber&unu-ui (四)
本文介绍了如何从阿里巴巴下载矢量图标并使用 `iconfont`,包括创建项目、下载文件、引入 `font.css` 到项目中以及在 `app.vue` 中引用的方法。同时,还详细说明了如何修改 `tabbar` 的样式和配置,以及如何在项目中导入和使用 `uni-ui` 组件库,包括简单的弹出框 `popup` 和带有头部或尾部图标的输入框 `input`。
102 0
|
4月前
【Azure Function App】在ADF(Azure Data Factory)中调用 Azure Function 时候遇见 Failed to get MI access token
【Azure Function App】在ADF(Azure Data Factory)中调用 Azure Function 时候遇见 Failed to get MI access token
|
4月前
|
iOS开发
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
240 0
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法

热门文章

最新文章