xcode 应用程序关闭时如何通过推送更改应用程序徽章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10398309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:09:31  来源:igfitidea点击:

How to change application badge with push when application is closed

iphonexcodepush-notificationpushbadge

提问by user1319267

I'm trying to change the badge of the application I'm developing. When closed, unfortunately I was only notified a message without changing the badge.

我正在尝试更改我正在开发的应用程序的徽章。关闭时,不幸的是我只收到一条消息而没有更改徽章。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];

NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSInteger badge = [[apsInfo objectForKey:@"badge"] integerValue];
if( [apsInfo objectForKey:@"alert"] != NULL)
{
    application.applicationIconBadgeNumber = badge;
}

But that does not change anything. I have not found any solution around. What method is invoked on arrival of a push notification and the application is closed? I also added:

但这不会改变任何事情。我还没有找到任何解决方案。推送通知到达时调用什么方法并关闭应用程序?我还补充道:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

#if !TARGET_IPHONE_SIMULATOR

NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);

NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

#endif
}

But nothing...

但没什么...

Can you help me?

你能帮助我吗?

回答by kdeo

The badge icon number is governed by the badge property in the payload received as push message. The app itself has no role to play in what number shows up till the app becomes active. Setting the 'badge' property to any integer value (Note : it should not be string and should NOT be enclosed within "") does what is needed. The OS on receiving the notification looks for the value for 'badge' and immediately sets it on the app badge icon. All this happens when app is not active. To make sure it increments or decrements based on what happens in the app, the app should send message to server with the updated badge number.

徽章图标编号由作为推送消息接收的有效负载中的徽章属性控制。在应用程序激活之前,应用程序本身对显示的数字没有任何作用。将 'badge' 属性设置为任何整数值(注意:它不应是字符串且不应包含在 "" 中)可满足需要。收到通知的操作系统会查找“徽章”的值,并立即将其设置在应用程序徽章图标上。当应用程序未处于活动状态时,所有这些都会发生。为了确保它根据应用程序中发生的事情增加或减少,应用程序应该向服务器发送带有更新的徽章编号的消息。

In the active state, it is app's responsibility to handle the remote notification and change the badge icon.

在活动状态下,应用程序负责处理远程通知并更改徽章图标。

Note: 'badge' property is enclosed in the 'aps' dictionary received within the payload. My payload looks like : aps = { alert = "Push notification!"; badge = 9; sound = default; }

注意:'badge' 属性包含在有效负载中收到的 'aps' 字典中。我的负载看起来像: aps = { alert = "推送通知!"; 徽章= 9; 声音 = 默认值;}

回答by Angel G. Olloqui

You should define better what you want to do because it is not clear to me if wether you are talking about remote or local notifications, or what exactly you want to get and what are you getting.

你应该更好地定义你想要做什么,因为我不清楚你是在谈论远程通知还是本地通知,或者你到底想得到什么以及你得到什么。

Anyway, regarding remote notifications (APNS), take into account that you should set the badge as part of the payload if you want it to be set with the app closed. Your app does not get invoked unless the user manually opens the app (from the NotificationCenter or the dock).

无论如何,关于远程通知 (APNS),请考虑到如果您希望在应用程序关闭的情况下设置徽章,您应该将徽章设置为有效负载的一部分。除非用户手动打开应用程序(从通知中心或停靠栏),否则您的应用程序不会被调用。

On the other hand, when you are running the app, you will receive the call to

另一方面,当您运行应用程序时,您将收到调用

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

in your AppDelegate, and then you can set the badge by using

在您的 AppDelegate 中,然后您可以使用

application.applicationIconBadgeNumber = badge;

回答by Matteo Pompili

Look at this, maybe it is a settings problem!: application Icon Badge Number in ios5

看这个,可能是设置问题吧!: ios5中的应用图标徽章编号

Try also to give applicationIconBadgeNumber an int value, not NSInteger.

还尝试给 applicationIconBadgeNumber 一个 int 值,而不是 NSInteger。

回答by Imeksbank

OK. Now my report about strange things. I got it! It works good for now.

好的。现在我报告奇怪的事情。我知道了!它现在很好用。

Problem was: i created my App with push notification everything was ok with simple integer (1,8,99) but then i noticed that my integer 35.06in the PHP variable $pricedid not recognize it at all even (int)$priceunsuccessful . I thought the number is too long (but actually 4 digits is very popular). I decided to use

问题是:我用推送通知创建了我的应用程序,用简单的整数 (1,8,99) 一切正常,但后来我注意到PHP 变量$price中的整数35.06根本无法识别它,甚至(int)$price不成功. 我觉得这个数字太长了(但实际上 4 位数字很受欢迎)。我决定使用

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

but as it was mentioned it does not work at all when the App is not started. No chance. It will never update the badge. I spent a lot of time thinking about what could be wrong and suddenly something came on my mind.

但正如前面提到的,当应用程序未启动时它根本不起作用。没有机会。它永远不会更新徽章。我花了很多时间思考可能出了什么问题,突然想到了一些事情。

in the payload i just did this

在有效载荷中,我只是这样做了

'badge' => $price*100

an of course it worked good for me =) I dont know what is the problem , why didn't work (int)$price(it is actually integer) but it is ok now. My badge shows now something like this 3.506and of course you can move the dot on step and you have your 35.06

当然,它对我很有用 =) 我不知道问题出在哪里,为什么(int)$price不起作用(它实际上是整数)但现在可以了。我的徽章现在显示类似3.506 的内容,当然您可以在步骤中移动点,然后您就有了35.06

Hope it could help you.

希望能帮到你。