简体   繁体   English

苹果推送通知不会自动更改图标徽章

[英]Apple push notification not changing icon badge automatically

I have seen the mail app in my iPhone (4S, iOS 5.1) automatically updates the badge count if new mail arrives, even when app is not running. 我已经在iPhone(4S,iOS 5.1)中看到了邮件应用程序,即使收到新邮件,即使应用程序未运行,它也会自动更新徽章计数。 So it is possible to achieve this behavior in my app also, right? 因此也可以在我的应用程序中实现此行为,对吗?

My application successfully registers for push notifications for all 3 types - Badge, Alert and Sound. 我的应用程序成功注册了所有三种类型的推送通知-徽章,警报和声音。 Phone Settings is set ON for all 3 types of remote notifications for this application. 此应用程序的所有3种远程通知的“电话设置”均设置为“开”。

My app receives remote notifications and shows alert, plays sound, but it does not update the badge number. 我的应用程序会收到远程通知,并显示警报,播放声音,但不会更新徽章编号。 If I launch the app via the View button on the alert, then my app can read the badge value perfectly and I can change/remove/set the icon badge from code. 如果我通过警报上的“查看”按钮启动应用程序,则我的应用程序可以完美读取徽章值,并且可以从代码中更改/删除/设置图标徽章。

Any specific reason why iOS cannot change the icon badge of my app automatically when the notification arrives? 通知到达后,iOS无法自动更改我的应用程序图标徽章的任何特定原因? I have seen all the similar posts, they are all discussing about either phone settings, or about notification types that it registered, or about checking the payload JSON includes badge or not. 我已经看过所有类似的帖子,他们都在讨论有关电话设置或其注册的通知类型,或有关检查有效载荷JSON是否包含徽章的问题。

Is there any other reason that might cause this problem? 还有其他可能导致此问题的原因吗?

Here is my code blocks: 这是我的代码块:

Register for APNs every time app is launched - 每次启动应用程序时注册APN-

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

Device token received almost instantly. 设备令牌几乎立即收到。 Token has been sent to provider, and I am receiving notifications. 令牌已发送给提供商,并且我正在接收通知。 Alerts and Sounds works. 警报和声音有效。 But badge has no automatic effect until I launch the app and change it manually. 但是在我启动应用程序并手动更改它之前,徽章没有自动作用。 Please help. 请帮忙。

I also had this exact same problem. 我也有同样的问题。 The badge icon was not quoted. 没有引用徽章图标。 After many hours of trying to tweak the JSON (after verifying app on phone showed badges, sounds, alerts) - the problem was I had added UIRemoteNotificationTypeNewsstandContentAvailability as one of the alert types my app was registering for. 经过数小时的尝试调整JSON(在验证手机上的应用程序显示徽章,声音,警报之后)-问题是我添加了UIRemoteNotificationTypeNewsstandContentAvailability作为我的应用程序正在注册的警报类型之一。 (I went crazy, just picking everything) (我发疯了,只是捡了一切)

So, when UIRemoteNotificationTypeNewsstandContentAvailability is also in the mix, it seems to override the function of allowing aps/badge numbers to update an app icon. 因此,当还同时使用UIRemoteNotificationTypeNewsstandContentAvailability时,它似乎覆盖了允许Ap /徽章编号更新应用程序图标的功能。 (Must be looking to update newsstand info) (必须要更新报亭信息)

The issue is now fixed. 现在,此问题已解决。 The provider server was sending the badge number in quotation marks (as JSON String, ie "9"). 提供者服务器正在发送带引号的徽章编号(作为JSON字符串,即“ 9”)。 Strange that APNs/iOS does not recognize it as integer. 奇怪的是,APNs / iOS无法将其识别为整数。 Now the quotes are removed and it works :) 现在引号被删除,它可以工作:)

I had a similar problem, solved it by forcing integer in the badge number (using PHP with data coming from MySQL db): 我有一个类似的问题,通过在徽章编号中强制使用整数来解决此问题(将PHP与MySQL数据库中的数据一起使用):

$body['aps'] = array(
        'alert' => array(
        'action-loc-key'=> utf8_encode($r['button_action']),
        'body'          => utf8_encode($r['message']),
        'launch-image'  => ''
        ),
    'badge' => (int)$r['badge_number'],
    'sound' => 'default'
    );

Ps.: take a look at the utf8_encode command to avoid a json_encode error I faced earlier too. 附注:请查看utf8_encode命令,以避免我之前也遇到过json_encode错误。

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

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