iphone - iOS 推送通知 - 在没有警报的情况下更新角标(Badge)?

标签 iphone ios ipad push-notification apple-push-notifications

有没有办法在不显示警报或打开应用程序的情况下更新角标(Badge)中的数字?

我正在编写一个应始终在图标角标(Badge)中显示当前未读消息数的应用,但我想在不向用户显示任何警报的情况下这样做。

我正在为 iOS 5.0+ 开发。

编辑:为了更清楚,我询问的是当应用程序未运行时执行此操作的方法。我希望服务器在不显示警报的情况下推送新的角标(Badge)编号。这可能吗?

最佳答案

你可以的。 可以在没有警报的情况下发送推送通知。 您甚至可以将您的应用程序注册为角标(Badge)通知,在这种情况下,提供商服务器甚至无法发送警报或声音。

The Notification Payload

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.

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:

An alert message to display to the user

A number to badge the application icon with

A sound to play

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

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 ]

}

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

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

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 本地和推送通知编程指南。

关于iphone - iOS 推送通知 - 在没有警报的情况下更新角标(Badge)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15225677/

相关文章:

iphone - 发布需要用户注册的 iPhone 应用程序

ios - 关于 MKMapView -regionThatFits :? 的 Apple 文档不正确

ios - 将 UITableViewCellAccessoryType 上的默认附件类型设为按钮

ios - swift ios 使用大小类肖像模式检查 iphone 5 或 iphone 6

ios - 代号一中的 BackgroundFetch

iphone - 注册越狱的开发设备

ios - 收到内存警告。在 iPad 中级别=1

iphone - 如何在视频上添加叠加文本,然后重新编码?

ios - 应用程序设置为 'iPhone' ,但它仍然在 iPad 上延伸到全屏?

ios - GMSMapView - 本地图结束滚动时如何获得回调