cannot receive push notification when app is not running - ios

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!!

Related

Set NSUserDefault When app is canceled or inactive

I receive a push notification when the my app is closed or canceled. Is there a way to set an nsuserdefault when this happens? I know if a user taps the notification or opens the app from the notification you can check if the app was inactive or canceled but what if they don't open the app from the notification but rather just launch the app by clicking on the icon?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
I know this method is called but it seems like I can not save to nsuserdefaults if the app is terminated or canceled.
If the app is closed or inactive and user opens the app through the icon ,
you will get the remote notification in "didFinishLaunchingWithOptions" method if any notification is available , there you can set you userdefault if you want .
Here is the code to get the remote notification in "didFinishLaunchingWithOptions"
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions) { //launchOptions is not nil
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
if (apsInfo) { //apsInfo is not nil
[self performSelector:#selector(postNotificationToPresentPushMessagesVC)
withObject:nil
afterDelay:1];
}
}
return YES;
}
For more help please visit this link #staticVoidMan have answered it very well .
Hope this Helps!

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.

didReceiveRemoteNotification is not being called in background mode until I do not tap on notification in ios

I have searched for hours but somewhere is saying that,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandlerNotification
is called, regardless of application is in active state or inactive state. Somewhere it says when application is not active it push notification calls your didFinishLaunchingWithOptions and there you can detect notification like this :
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]`
In my case, both are working if user tap on notification and everything is fine, but the problem occurs when user does not tap on notification and direct open app from its icon, then neither didFinishLaunchingWithOptions detect it has notification nor didReceiveRemoteNotification get called. I need to save messages in database, received from push notification, but without tapping on notification how to call method?
to get notification data in didFinishLaunchingWithOptions do this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(launchOptions != nil)
{
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[remoteNotif objectForKey:#"aps"] objectForKey: #"badge"] integerValue];
//do something here to process notification
}
}
}

Push notification not taking to respective category if app is killed

I have an app where I have push notification.
What I have is I go to respective category when push is clicked (from the push notification list).
All is working perfectly, except when the app is killed.
If the app is minimized and push comes, if I click on push, it goes to respective category.
However if I forcefully kill the app and push comes and click on the push, it just open the app and no transition occurs.
Is this natural behavior in iPhone or I am doing something wrong?
In didReceiveRemoteNotification I go to specific category based on the data I received.
didReceiveRemoteNotification not call app will not run, that time push notification open app newly in device, you manually check if any notifications are bending in application:didFinishLaunchingWithOptions: try this for check bending notifications in app.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...............
...............
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"load notifiation *******");
isLoadNotification=YES;
}
return YES;
}
Below is what I used.
-(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"];
// do all transition here....
}else if (launchOptions == nil){
NSLog(#"launch ,,, nil");
}
...//code something
}

Add multiple Push notifications to Global Array ios

I have a problem handling more than one Push Notifications. When I unlock the iphone and there is (for example) 10 push notifications of my app, my app just register some:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
[self addMessageFromRemoteNotification:dictionary updateUI:YES];
}
}
}
If my app is open there is no problem and all received notifications are added to array.
Thank you

Resources