0

I know that this question is tipical but I'm missing something and I don't kwon what is.

I have a simple application, in iOS 7. Using Xcode 6. I'm developing in objective-c.

I want to receive push notifications and update my app icon badge to (1) but I can't trigger the didReceiveRemoteNotification method when the app is in background so I can't set the badge number. This is not my first app with push notifications and badges but I can't do it...

My code:

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
NSLog(@"RECEIVING NOTIFICATION");

When i'm on the app the badge is correctly setted and the log is printed, but if I'm in background mode the method is not triggered.

My payload APNS

{"aps":{"alert":"New push","sound":"default"}}

I want to set the badge manualy to 1. I don't need it on de payload.

Any suggestions? Thanks

pekpon
  • 761
  • 1
  • 10
  • 24

2 Answers2

1

The remote notification will set the badge automatically according to the number of "badge" in aps.

For example:

{"aps":{"alert":"New push","sound":"default","badge":1}}

KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • I am sending message to channel which have some large number of users in it. I don't have the unread message count of each user. How can I handle this? – Beu May 07 '20 at 06:52
1

A bit late, but it might help someone else.

You can trigger - application:didReceiveRemoteNotification: when your app is in background by following this two steps:

  1. Set Required background modes to App downloads content in response to push notifications in your Info.plist file
  2. Add "content-available": 1 to your notification payload.

This will call - application:didReceiveRemoteNotification: and allow you to handle notifications in background (i.e, refresh the content of your app, etc...).

Hope this helps!

pommefrite
  • 819
  • 10
  • 12
  • for number 2 is there something similar in Firebase (if you are familiar with Firebase) – Ahmadiah Jan 03 '18 at 23:58
  • Sorry, I haven't tried Firebase yet. However, this should be iOS specific, not tied to your notifications service. I'd surprised if it didn't work with Firebase. – pommefrite Jan 30 '18 at 10:01