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 | 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".


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.

时间: 2024-09-21 12:48:15

How to get app icon badge numbers in iOS7 without push notifications的相关文章

PhotoShop绘制简单的小铃铛APP Icon图标制作教程

本PS教程教大家用PhotoShop绘制简单的小铃铛APP Icon图标.简单的东西,其实别小看一个小小的Icon,它里面包含的知识是非常多的, 这个比较简单,高手路过-- 最终效果: 1.新建文件,新建空白图层,按Ctrl+R调出标尺,利用钢笔工具绘制路径,并转换成选区(Ctrl+Enter). 2.使用渐变工具,为选区填充径向渐变,颜色代码如图. 3.新建图层,用椭圆选框工具,绘制选区,并再为其制作径向渐变,颜色如图: 分类: PS鼠绘教程 ps图标制作教程

android 如何让 App icon 不显是在 launch 上,但此app 能从别的程序入口进入

问题描述 android 如何让 App icon 不显是在 launch 上,但此app 能从别的程序入口进入 最近在做一个项目,目的是为了让 app 安装后,在 launch 上找不到 所安装app 的 icon , 但能从别的程序中进入. 解决方案 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="andr

iOS10 App适配权限 Push Notifications 字体Frame 遇到的问题_IOS

添加配置权限 <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>"xx"想使用您的相册,需要您的允许</string> <!-- 相机 --> <key>NSCameraUsageDescription</key> <string>"xx"想使用您的相机,需要您的允许</strin

android app icon 图标大小尺寸_Android

在android程序中,在drawable中设置好图标后,但结果在小米2可没有显示出来,但在我自己的htc中和模拟器中显示正常.后来发现小米的dpi不一样. 后来发现,android为了适应不同的分辨率和dpi,加载了一系列的图标. LDPI (Low Density Screen,120 DPI),其图标大小为 36 x 36 px. MDPI (Medium Density Screen, 160 DPI),其图标大小为 48 x 48 px. HDPI (High Density Scre

android app icon 图标大小尺寸

在android程序中,在drawable中设置好图标后,但结果在小米2可没有显示出来,但在我自己的htc中和模拟器中显示正常.后来发现小米的dpi不一样. 后来发现,android为了适应不同的分辨率和dpi,加载了一系列的图标. LDPI (Low Density Screen,120 DPI),其图标大小为 36 x 36 px. MDPI (Medium Density Screen, 160 DPI),其图标大小为 48 x 48 px. HDPI (High Density Scre

iOS开发系列--通知与消息机制

概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地通知:另一类是推送通知,也叫远程通知.两种通知在iOS中的表现一致,可以通过横幅或者弹出提醒两种形式告诉用户,并且点击通知可以会打开应用程序,但是实现原理却完全不同.今天就和大家一块去看一下如何在iOS中实现这两种机制,并且在文章后面会补充通知中心的内容避免初学者对两种概念的混淆. 本地通知 推送通

iOS开发系列--通知与消息机制详解_IOS

概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地通知:另一类是推送通知,也叫远程通知.两种通知在iOS中的表现一致,可以通过横幅或者弹出提醒两种形式告诉用户,并且点击通知可以会打开应用程序,但是实现原理却完全不同.今天就和大家一块去看一下如何在iOS中实现这两种机制,并且在文章后面会补充通知中心的内容避免初学者对两种概念的混淆. 本地通知 本地通

改变iOS app的icon(iOS10.3)

改变iOS app的icon 官方 iOS10.3新增了可以让开发者去更改app的icon,接下来看看怎么更改.官方API给的东西很少,只是介绍了一个实例方法: open func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Swift.Void)? = nil) 根据传入的参数可知,我们只需要传入备用icon名字即可,然后在回调里面拿到修改的结果,成功的话error为空,不

App各种Icon及Launch image的尺寸和用途 移动端尺寸基础知识

IOS7,8  Asset iPhone 6 Plus (@3x) iPhone 6 and iPhone 5 (@2x) iPhone 4s (@2x) iPad and iPad mini (@2x) iPad 2 and iPad mini (@1x) App icon 180 x 180 120 x 120 120 x 120 152 x 152 76 x 76 App icon for the App Store 1024 x 1024 1024 x 1024 1024 x 1024