How to get app icon badge numbers in iOS7 without push notifications
来源:互联网 发布:网络词语大全2016 编辑:程序博客网 时间:2024/06/06 01:40
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, 2014Using 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".
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.
- How to get app icon badge numbers in iOS7 without push notifications
- How to Implement Push Notifications for Android
- How to Implement Push Notifications for Android
- How to Implement Push Notifications for Android
- How to correctly set application badge value in iOS 8?
- Is there a way to add a badge to an application icon in Android?
- ios7 APP icon尺寸
- ios7 APP icon
- Socket Programming : How to Integrate Socket.io java Client in your Android App without Maven
- How to find identifier in IOS7
- how to get pdf page numbers
- How to change the Push and Pop animations in a navigation based app
- 友盟统计出现Add the Push Notifications feature to your App ID.的错误
- How to get table metadata without sql-statement invoking
- How to Get App Name and Version
- iOS Programming 101: How To Get the User Location in iPhone App
- how to show an envelope icon in the notification area?
- How to set the QT Application icon in the windows.
- Openstack Keystone 认证流程(一)--Overview
- 如何在应用中集成Facebook SDK for Android(初始篇 )
- 运放相关知识3
- SDNLAB每周播报
- 政府の電源論議―欠かせない使う側の視点
- How to get app icon badge numbers in iOS7 without push notifications
- 使用hibernate的createSQLQuery方法时,写sql语句,注意的地方
- android Download Manager被禁用
- 面向对象第七课,this隐式参数,内存分析
- 最新数据库排名
- 给年轻工程师的十大忠告
- 230 - Borrowers
- 【UML001】Business Use-Case VS System Use-Case
- 什么是idc