iOS 推送通知 - 在没有警报的情况下更新徽章? [英] iOS Push Notifications - update badge without alert?

查看:25
本文介绍了iOS 推送通知 - 在没有警报的情况下更新徽章?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不显示警报或打开应用程序的情况下更新徽章中的数字?

Is there a way to update the number in the badge without showing an alert or opening the app?

我正在编写一个应用程序,它应该始终在图标徽章中显示当前未读消息的数量,但我想这样做而不向用户显示任何警报.

I am writing an app that should always display the current number of unread messages in the icon badge, but I want to do so without displaying any alerts to the user.

我正在为 iOS 5.0+ 开发.

I am developing for iOS 5.0+.

更清楚地说,我问的是一种应用未运行时执行此操作的方法.我希望服务器在不显示警报的情况下推送一个新的徽章编号.这可能吗?

To be more clear, I am asking about a way to do this when the app is not running. I want the server to push a new badge number without showing an alert.. Is this possible?

推荐答案

你可以做到.可以在没有警报的情况下发送推送通知.您甚至可以将您的应用程序注册到徽章通知,在这种情况下,提供商服务器甚至无法发送警报或声音.

You can do it. It is possible to send a push notification without an alert. You can even register your application just to badge notifications, in which case the provider server won't even be able to send alerts or sounds.

通知有效载荷

每个推送通知都带有一个有效载荷.有效载荷指定如何提醒用户等待数据下载到客户端应用程序.允许的最大尺寸通知有效载荷为 256 字节;苹果推送通知服务拒绝任何超过此限制的通知.请记住通知的传递是尽力而为",不能保证.

Each push notification carries with it a payload. The payload specifies how users are to be alerted to the data waiting to be downloaded to the client application. The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit. Remember that delivery of notifications is "best effort" and is not guaranteed.

对于每个通知,提供者必须组成一个 JSON 字典对象严格遵守 RFC 4627.这本词典必须包含另一个由密钥 aps 标识的字典.aps 字典包含一个或多个指定以下操作的属性:

For each notification, providers must compose a JSON dictionary object that strictly adheres to RFC 4627. This dictionary must contain another dictionary identified by the key aps. The aps dictionary contains one or more properties that specify the following actions:

显示给用户的警告信息

用于标记应用程序图标的数字

A number to badge the application icon with

播放声音

请注意,它表示一个或多个属性.警报属​​性是可选的.您甚至可以使用空的 aps 字典发送通知(即仅发送自定义属性).

Note that it says one or more of the properties. The alert property is optional. You can even send a notification with an empty aps dictionary (i.e. send only custom properties).

示例 5. 下面的示例显示了一个空的 aps 字典;由于缺少徽章属性,显示的任何当前徽章编号上的应用程序图标被删除.acme2 自定义属性是两个整数的数组.

Example 5. The following example shows an empty aps dictionary; because the badge property is missing, any current badge number shown on the application icon is removed. The acme2 custom property is an array of two integers.

{

    "aps" : {

    },

    "acme2" : [ 5,  8 ]

}

用户将看到的唯一警报是询问他/她是否允许推送通知的警报.该提醒只会在应用安装后第一次启动时显示.

The only alert the user will see it the alert that asks him/her whether to allow push notifications. That alert will only be displayed the first time the app is launched after installation.

在此示例中,您注册了非警报通知(仅限徽章和声音):

In this example you register to non alert notifications (badges and sounds only) :

Listing 2-3  Registering for remote notifications

- (void)applicationDidFinishLaunching:(UIApplication *)app {

   // other setup tasks here....

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

}



// Delegation methods

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

    const void *devTokenBytes = [devToken bytes];

    self.registered = YES;

    [self sendProviderDeviceToken:devTokenBytes]; // custom method

}



- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

    NSLog(@"Error in registration. Error: %@", err);

}

所有引用均来自 Apple 本地和推送通知编程指南.

All quotes are taken from the Apple Local and Push notifications programming guide.

这篇关于iOS 推送通知 - 在没有警报的情况下更新徽章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆