iOS的推送通知 - 无需警报更新徽章? [英] iOS Push Notifications - update badge without alert?

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

问题描述

有没有办法来更新徽章的数量,而不显示警告或打开应用程序?

我写一个应用程序,应该始终显示在图标徽章未读邮件的当前数目,但我想不显示任何提示给用户这样做。

我正在开发适用于iOS 5.0 +

编辑:为了更清楚,我问一个办法做到这一点的当应用程序没有运行即可。我希望服务器推一个新的证件号码,而不显示一个警告..这可能吗?


解决方案

您可以做到这一点。
它可以发送一个推送通知而不发出警报。
你甚至可以注册您的应用程序只是为了徽章通知,在这种情况下,提供服务器甚至不能够发送警报或声音。


  

的通知有效载荷


  
  

每个推送通知与它携带的有效载荷。有效载荷
  指定的用户如何被提醒数据等待被
  下载到客户端应用程序。允许的最大大小
  通知有效载荷为256字节;苹果推送通知服务
  拒绝超过此限制的任何通知。请记住,
  通知交付是尽力而为,并不能保证。


  
  

有关每个通知,供应商必须构成一个JSON字典对象
  严格符合RFC 4627.这本词典必须包含
  通过密钥识别APS另一个字典。在APS字典
  包含指定以下动作的一个或多个特性:


  
  

这是提示信息显示给用户。


  
  

一个数字,徽章应用程序图标


  
  

要播放的声音


请注意,它说的一个或多个属性。警报属​​性是可选的。你甚至可以发送通知与空 APS 词典(即只发送自定义属性)。


  

5示例下面的示例显示一个空的APS字典;
  因为徽章属性缺失,显示当前任何证件号码
  上的应用程序图标被移除。该acme2自定义属性是
  阵列两个整数的。


  {    APS:{    },    acme2:[5,8]}

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

在这个例子中您注册非警报通知(徽章,只有声音):

 清单2-3注册的远程通知 - (无效)的applicationDidFinishLaunching:(UIApplication的*)应用{   //这里其他设置任务....    [UIApplication的sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];}//代表团方法 - (无效)应用:(*的UIApplication)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*){DEVTOKEN    常量无效* devTokenBytes = [DEVTOKEN字节]    self.registered = YES;    [个体经营sendProviderDeviceToken:devTokenBytes]; //自定义的方法} - (无效)应用:(*的UIApplication)应用didFailToRegisterForRemoteNotificationsWithError:(NSError *)错误{    的NSLog(@登记错误错误:%@,ERR);}

股价从Apple地方采取和推送通知编程指南。

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.

I am developing for iOS 5.0+.

EDIT: 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.

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

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

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);

}

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

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

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