应用程序关闭时如何通过推送更改应用程序徽章 [英] How to change application badge with push when application is closed

查看:84
本文介绍了应用程序关闭时如何通过推送更改应用程序徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

但没什么...

你能帮我吗?

推荐答案

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

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.

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

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

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

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

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

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

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

application.applicationIconBadgeNumber = badge;

这篇关于应用程序关闭时如何通过推送更改应用程序徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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