简体   繁体   English

iOS推送通知 - 在没有提醒的情况下更新徽章?

[英]iOS Push Notifications - update badge without alert?

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+. 我正在为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; 通知有效负载允许的最大大小为256字节; Apple Push Notification Service refuses any notification that exceeds this limit. Apple推送通知服务拒绝任何超出此限制的通知。 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. 对于每个通知,提供者必须编写严格遵守RFC 4627的JSON字典对象。此字典必须包含由密钥aps标识的另一个字典。 The aps dictionary contains one or more properties that specify the following actions: aps字典包含一个或多个指定以下操作的属性:

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 . 请注意,它表示one or more of the properties The alert property is optional. alert属性是可选的。 You can even send a notification with an empty aps dictionary (ie send only custom properties). 您甚至可以使用空的aps字典发送通知(即仅发送自定义属性)。

Example 5. The following example shows an empty aps dictionary; 示例5.以下示例显示了一个空的aps字典; 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. acme2定制属性是一个包含两个整数的数组。

{

    "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. 所有引用均来自Apple Local and Push notifications编程指南。

you should use applicationIconBadgeNumber for locally handling your app badge number 您应该使用applicationIconBadgeNumber来本地处理您的应用程序徽章编号

[UIApplication sharedApplication].applicationIconBadgeNumber = number_of_notifications;

I don't think it is possible to do without alert as far as adding badge counter from remote notification. 就远程通知添加徽章计数器而言,我认为没有警报是可能的。 You should read about APN Service , in your case you might register for UIRemoteNotificationTypeBadge you should read about Local & Push Notification Programming guide 您应该阅读有关APN服务的信息 ,在您的情况下,您可以注册UIRemoteNotificationTypeBadge您应该阅读本地和推送通知编程指南

您可以使用

[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

use this method....this will help u. 使用这种方法....这将有助于你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM