Can we send/display badge count for silent iOS notifications? - ios

I want to handle badge count increment in iOS application. On the basis of research I did so far I understand that app can read/update badge programmatically when it is in foreground. Also, ‘application:didReceiveRemoteNotification’ is called only when application is in foreground state.
However, I want to be able to increase badge even when my app is in background state. I came across the notification callback method below which gets invoked even during the background state -
application:didReceiveRemoteNotification:fetchCompletionHandler
It is introduced in iOS 7. However I noticed that even this newly introduced callback is not invoking in background state. Then after doing little more research I realised that this method calls only in case of Silent iOS notifications. So now my question is, is it possible to show badge count for silent notifications, can I receive badge count in payload ? I was just thinking of implementing the badge count calculation by leveraging the background support available for silent notifications.
Any suggestions/guidance/help is extremely invaluable for us at this moment.
Thanks.

You can put badge count in payload. In this case, the server side should handle the number of badge count. For example:
{
"aps" : {
"badge" : 5,
},
}
If your app is running foreground and to increase/decrease the badge count sent by server, this following example would help.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"remote notification: %#",[userInfo description]);
if (userInfo) {
NSLog(#"%#",userInfo);
if ([userInfo objectForKey:#"aps"]) {
if([[userInfo objectForKey:#"aps"] objectForKey:#"badgecount"]) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:#"aps"] objectForKey: #"badgecount"] intValue];
}
}
}
}

Related

How handle silent push notification when application is InActive state in ios 9?

I have implement silent push notification.So "didReceiveRemoteNotification" method called when application is inactive state in ios 9.
There are some case when application is inactive state.
1.When user tab on particular notification.
2.When call or message receive.
3.When notification center and control center open.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) //Inactive state
{
[self RedirectScreenBasedOnNotification:self.userInfoDic];//Screen Redirection code
}
}
So how can i handle silent notification when app is inactive state?
I have face problem is when notification center open at that time if any notification come then redirection will do,but i want to stop that.
Notification payload:-
aps = {
alert = "Test Dev 5 startd following you ";
"content-available" = 1;
"link_url" = "https://raywenderlich.com";
message = {
friend = {
email = "abc#gmail.com";
name = "Test Dev 5";
photo = "";
"user_id" = 27;
};
id = 3;
"is_business_sent" = 0;
message = "Test Dev 5 startd following you ";
};
sound = default;
}
Thanks in advance
Silent push notifications do not trigger user interactions. When a silent notification payload includes keys for user interaction things go wrong - iOS can't reason about wether the intent is to present something to the user, or to keep the notification silent and handled without user interaction. Sometimes the silent notification may work, other times it may be presented like a normal notification with user interaction. It can be one or the other, not both.
If the silent push key content-available is present in the aps payload the keys alert, sound, or badge should not be.
You can use my Push Notification Payload Validation Tool to check the content of your notification. The payload you posted in your question has several problems - the aps key should only contain Apple keys defined in Generating Push Notifications. All of your custom keys and values should be outside the aps object.
application:didReceiveRemoteNotification:fetchCompletionHandler: will only be called for silent push notifications. If the notification payload contains both content-available and one or more of alert, sound, or badge iOS will not know which method to call and you may see inconsistent behavior.
If you are just trying to show a non-silent notification you do not need to implement application:didReceiveRemoteNotification:fetchCompletionHandler:. Instead implement application:didReceiveRemoteNotification: for iOS 9 and userNotificationCenter:willPresentNotification:withCompletionHandler: for iOS 10 and later.
As far as silent notifications and the inactive application state, there is nothing special to be done here. Silent notifications are intended to 'hint' to the application that it should refresh content. When a silent notification is received the application is required to process the content update within 30 seconds and then call the fetch completion handler. When iOS executes the fetch completion handler it take a new restoration snapshot of the updated UI. This happens even when the application is inactive.
You can add your code in this If condition.
if (UIApplication.sharedApplication.applicationState != UIApplicationStateInactive) {
//Write your code her, this will get executed when your app is not in Inactive state.
}

Set App Icon badge no in IOS

I'm Working on PushNotification, I want to manage AppIconBadge no. in IOS.
Badge no is Receiving From parse Site, Suppose Badge no. received from parse id 20. now 20 will show on App logo when the app in background and kill State. i want it show only counting that is Remaining to read in the Notification Center. please help me how can i Manage the App Icon badge no.
App is kill state,one push notification arrived , then Which Function Called by IOS. Can i Control that Function
In kill state you can't access it. You should manage this number from parse before you send the push. to change badge number from the app itself:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: wantedNumber];
you can handle you badge count in app delegate.m didReceiveRemoteNotification method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
alert = userInfo[#"aps"][#"alert"];
//Handle you badge count here
}
Badge number comes in payload. You need to increment the badge number at server and keep a track of badge number there itself. On receiving any notification if you open the app the badge count should be reset to any value that you can do # setApplicationIconBadgeNumber.
On app launch reset the counter to 0 or whatever you want at your server thru a network request, so that the next notification send a payload with badge number from 0.

Execute few lines of code when received silent push notification and app is running in background, ios

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(#"PUSH NOTIFICATION %#",userInfo);
if([userInfo[#"aps"][#"content-available"] intValue] == 1){
NSLog(#"SILENT PUSH NOTIFICATION");
//Here I'm inserting a flag value to my DB.
completionHandler(UIBackgroundFetchResultNewData);
}
else{
NSLog(#" GENERAL PUSH NOTIFICATION ");
completionHandler(UIBackgroundFetchResultNoData);
}
}
My silent notification payload is {
aps = {
"content-available" = 1;
sound = "";
};
}
To support I have added a key in info.plist in "Required background modes"
item 0 = App downloads content in response to push notifications
Also in capabilities section my Background Modes are ON with remote notification check mark is selected.
Now my question is when I run my app then I'm able to receive silent as well as general push notification and code is executing successfully in foreground mode but when I press home button (not forcefully quitting by swiping it away and also device's passcode lock is open) my app is going to background mode and then my code is not executing even I'm able to receive push notifications those are having alert and sound, but app is not launching in background mode.
I'm thinking that for foreground and for background mode whenever my app is receiving any push notification don't matter is it silent or general push notification my first log in the delegate method should have to print on the console i.e NSLog(#"PUSH NOTIFICATION %#",userInfo);
Please help I'm struggling with this since last 2-3 days.
My info.plist
I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".
#Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.
I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.
My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.
Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.
I think you need to test once with 'content-available' outside 'aps', like the following:
aps = {
alert = "This is test alert";
badge = 1;
};
"content-available" = 1;

How to update badge number when application is in background [duplicate]

This question already has answers here:
Update badge with push notification while app in background
(12 answers)
Closed 9 years ago.
I am creating one iOS Application in which I am getting badge number from server, while the application is in foreground, I am updating the badge number in
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
but when the application is in background, I have no idea which function I should call.
You may send badge number in push notification like this
{"aps":{"badge":"3","alert":"help","sound":"sound.caf"}}
When the application is in the background didReceiveRemoteNotification method never calls. For doing something when your App is in background you need to implement your logic in AppDelegate's applicationDidEnterBackground: method like.
-(void)applicationDidEnterBackground:(UIApplication *)application
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}
You can send the badge number from your server itself.
By default when u specify the badge number in your APN payload there is no need to set the badge number of your application. If you didn't receive the badge number from server then you cant do it anyway until the user open the application.
But in ios7 APN had a new feature.
By setting the flag Content-Available:1 will let your application run immediately after received the notification,there you can set your badge number.
Use this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[UIApplication sharedApplication].applicationIconBadgeNumber = X;
NSLog(#"Application Did Enter Background");
}
Send you badge number in the payload of push notification

Questions about APNS

I have implemented the APNS in my app. But I have two questions about it.
How to make the badge self-increase? In my app, it is always set to 1 now.
If a push notification arrives when the app is in foreground, as far as I know, I need to implement a altert view and play a sound by my self. In this case, is there any way to play the system default notification sound, i.e. when user change it in settings, it will change automatically.
Thanks.
you can increment the badge count as wasim described but it will only work if you app is in foreground, so for displaying correct badge count your server has to push correct badge number.
for playing default system notification sound when app is in foreground I dont think there is any APIs for that, for that you have put the sound file in your bundle and play that sound when notification receives.
In your AppDelegate.m use the following function:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
badge_value+=[[[userInfo objectForKey:#"aps"] objectForKey:#"badge"]intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber = badge_value;
}
where, badge_value is an integer that stores the badge value.
Usually in all apps, the unread notification counts are maintained in the server. When the server sends a push notification to a particular device token server sends the badge count along with the payload.
Your server logic needs to keep track of the proper badge count and send it appropriately.
{
"aps" :
{
"alert" : "Your notification message",
"badge" : badgecount ,
"sound" : "bingbong.aiff"
}
}

Resources