How to change application badge with push when application is closed

拟墨画扇 提交于 2019-12-13 01:33:45

问题


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?


回答1:


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

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

application.applicationIconBadgeNumber = badge;



回答2:


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.

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




回答3:


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

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




回答4:


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.06 in the PHP variable $price did not recognize it at all even (int)$price unsuccessful . I thought the number is too long (but actually 4 digits is very popular). I decided to use

- (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.506 and of course you can move the dot on step and you have your 35.06

Hope it could help you.



来源:https://stackoverflow.com/questions/10398309/how-to-change-application-badge-with-push-when-application-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!