Background mode remote notification not calling delegate method - ios

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary
*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Push info= %#",userInfo);
if([userInfo[#"aps"][#"content-available"] intValue] == 1)
{
NSLog(#"silent push notification received");
NSString *strQuery = [NSString stringWithFormat:#"UPDATE staff_list SET is_update =
'YES'"];
[[DatabaseManager sharedInstance] updateDatabaseForQuery:strQuery];
completionHandler(UIBackgroundFetchResultNewData);
return;
}
else
{
completionHandler(UIBackgroundFetchResultNoData);
return;
}
}
My payload for silent push notification is {
aps = {
"content-available" = 1;
sound = "";
};
}
Also unabled background mode for remote notification.
This will work perfect when app is running in foreground but when I press home button then app is running in background and after that not executing any method when receiving a new silent push notification.
Please help me I have stuck with this since last 3 days.
Thanks in advance.

Related

Push notification - alert missing from payload

This is the userInfo I am getting in didReceiveRemoteNotification
aps = {
badge = 2;
sound = default;
};
u = "{\"custom\":{\"redirectlink\":\"groupdetail.html?groupid=314416&selectedtabid=5\"}}";
}
Clearly alert field is missing here. This is what I tried to make to handle missing alert :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
NSMutableDictionary *pushDict = [NSMutableDictionary dictionaryWithDictionary:[userInfo objectForKey:#"aps"] ];
BOOL isSilentPush = [[pushDict objectForKey:#"content-available"] boolValue];
[pushDict setObject:#"You have a notification" forKey:#"alert"];
NSMutableDictionary * mut = [NSMutableDictionary dictionaryWithDictionary:userInfo]; //[userInfo copy];
[mut setObject:pushDict forKey:#"aps"];
//[mut setObject:#"5#" forKey:#"p"];
if (isSilentPush) {
NSLog(#"Silent push notification:%#", userInfo);
//load content here
// must call completionHandler
completionHandler(UIBackgroundFetchResultNewData);
}
else {
[[PushNotificationManager pushManager] handlePushReceived:mut];
// must call completionHandler
completionHandler(UIBackgroundFetchResultNoData);
}
}
But this does not generate a banner notification. Only badge, sound and alert is generated.
I know this should be handled from server side and not from iOS application, but the server guy will be back in 2 days and I have a demo to show.

Handling push notifications when app is not running (App is Killed)

My problem seems to be duplicate of this one,but it's not. While application is killed and not running in the background, if I receive push notification and clicked the notification banner, it works fine. "userInfo" isn't empty and application handles the notification. BUT if i dismiss the notification banner and open the app via clicking the application icon, this "userInfo" returns nil.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
NSDictionary* userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo != nil){
//Handling notification
}
}
and also
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
if([application applicationState] == UIApplicationStateActive) {
NSLog(#"...1");
}else if([application applicationState] == UIApplicationStateInactive){
NSLog(#"...2");
}else if([application applicationState] == UIApplicationStateBackground){
NSLog(#"...2");
}
completionHandler(UIBackgroundFetchResultNoData);
}
Is there any way to handle these notifications or should I handle them by my own ?
No your app is only informed about the notification that is used to open/launch your app.
There is no way to detect of there are any notification in the notification center for your app. You need to build this yourself in your apps server.

How to handle normal and background silent push notification?

I am trying to implement the silent push notification for my background fetch.
Now i am facing an issue while implementing the necessary code in my AppDelegate.
Before i had the following function in order to handle the push notification:
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Just Received a push!");
...
}
I added mow the below function to handle the background fetch silent notification:
//backgroundDownloadTask
- (void) application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Just Received a push!");
....
}
The weird thing is that when i receive a normal notification, it also entered the second function and the first one (old) is seems top be useless now, so I added the following to differenciate between the normal and silent one:
//backgroundDownloadTask
- (void) application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Just Received a push!");
NSDictionary *dict = [userInfo objectForKey:#"aps"];
if([dict objectForKey:#"content-available"])
{
//silent
}
else
{
//normal push
}
}
but the issue is when i am in the foreground and I receive a push, No function is executing as no push is received(Maybe because the second function is just for background).
So how am I able to handle both pushes? any idea ?
Thank you,

UINotification ios not received when app is in background

I am using notification in one of my app ,when app is active , notification is received , i process data and everything is fine , but I do not receive any notification when app is in background or killed.What is the issue can any one plz help me ?
Thank you!
Here is what I doing so far
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
tokenstring = [[NSString alloc] initWithFormat:#"%#",deviceToken];
tokenstring = [tokenstring stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
tokenstring = [[NSString alloc]initWithFormat:#"%#",[tokenstring stringByReplacingOccurrencesOfString:#" " withString:#""]];
NSLog(#"TokeinID:>> %#",tokenstring);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"didReceiveRemoteNotification: %#",userInfo);
AudioServicesPlaySystemSound(1002);
//Some code or logic
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"didFailToRegisterForRemoteNotificationsWithError: %#",err.description);
}
When you receive remote notifications, -application:didReceiveRemoteNotification: is only called when your app is in the foreground. If your app is in the background or terminated, then the OS may display an alert or play a sound (depending on the aps dictionary in the notification), but the delegate method is not called.
The remote notification received in the background will only passed to your application if it is launched with that notification's action button, and then you need to look at the launch options dictionary on -application:didFinishLaunchingWithOptions: to see the content of the notification.
If you're looking at new content fetching/remote notification background support with iOS7, check Will iOS launch my app into the background if it was force-quit by the user? and see if that helps, as there are very specific circumstances that those functions work in.
little code ..
When app is not running
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
is called ..
where u need to check for push notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(#"app recieved notification from remote%#",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
}

cannot receive push notification when app is not running

I've created an iphone app with push notification feature. The server works well and I could receive the notification when app is running in foreground. The function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
is called then notification arrives.
However, when I press the home key, brought the app into background or kill it in the task bar. I cannot receive any notification, neither in the notification area or any popup.
Anyone knows the reason? Thanks
You can use...
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
if (launchOptions != nil) {
NSMutableDictionary *dic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSMutableDictionary *dicItem = [dic objectForKey:#"aps"];
NSString *itemNotification = [dicItem objectForKey:#"alert"];
}else if (launchOptions == nil){
NSLog(#"launch ,,, nil");
}
...//code something
}
itemNotification is an item in Notification barge. Have fun!!

Resources