How to get app icon badge numbers in iOS7 without push notifications-阿里云开发者社区

开发者社区> 悟静> 正文

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

简介: How to get app icon badge numbers in iOS7 without push notifications Refresh your app in the background with a new iOS7 feature ITworld | 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 | February 5, 2014



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.


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
怎么设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程
6367 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
3960 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
7586 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
9309 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
10718 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
2901 0
+关注
悟静
从事软件行业近二十年
690
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载