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,
Related
It works well when app is in foreground or background but as soon as i clear the app the notifications are not received. After that when i try to open the app, notification come on the splash screen.
function for receiving notifications;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
NSLog(#"didReceiveRemoteNotification %#", userInfo);
[self checkNotification:userInfo];
}
I have set up my app to work with GCM.
I have successfully added the code to integrate the GCM in my App.
Now I have two methods to handle the Push Notification:
Default Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
}
GCM Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
}
Now I am confused where should I Handle my Notificaiton.
Where should I check application state and call my method to handle it.
Should I have to write method in both of these methods.
I'm not familiar with GCM but the two notification methods you listed standard UIApplicationDelegate methods and handle different scenarios.
application:didReceiveRemoteNotification: is called when the app is open and you receive a plain push notification. The types that you get alerted through notification center.
application:didReceiveRemoteNotification:fetchCompletionHandler: is called when the server is letting the app know there's something to download. You check the userInfo for what to download, initiate the download and call the handler(UIBackgroundFetchResult) upon NewData/NoData/Failed
Not sure what GCM does with these two methods but with that info you should be able to figure it out.
You should use GCM Method.
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
}
In this method, You can handle your notification.
For example,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// [START_EXCLUDE]
if(application.applicationState == UIApplicationStateBackground){
//app is in background
}else if(application.applicationState == UIApplicationStateInactive){
//From background to foreground (user touchs notification)
}
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}
My iPhone's screen is locked and sometimes, when I send push notifications a from PHP script, it disappears automatically. What can be the reason for this issue?
I use background fetch execution for didReceiveRemoteNotification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
NSLog(#"Notification received: %#", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
// 100 lines of code
}
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{
}
i implemented the Remote Notifications in my application! if my App is in Background and a Push Message was send to my Device, i react with this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}
This is working great when App is in Foreground or in Background State! But what if my App is not running at all?! CanĀ“t i react to Push Messages when the app is not running?I mean WhatsApp can do this, right?!
If user clicks on push notification from notification center you will have information in launchOptions with the push notification content and you can use below code to check if application was launched clicking push notification or it was there as well,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSLog(#"LaunchOptions->%#",launchOptions);
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performNotificationAction:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// NSLog(#"userInfo->%#",userInfo);
[self performNotificationAction:userInfo];
}
-(void)performNotificationAction:(NSDictionary*)userInfo{
//Do the stuf whatever you want.
//i.e. fetch the message or whatever extra information sent in push notification
}