Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Push notification issue

I am doing a project in which push notification feature is one of the key feature.
It is working fine when I am in the app, I receive notification and handle that notification.

But the issue is when I am in background and notification receives I see badge on my app icon and when I click on the icon my app is launching but the didReceiveRemoteNotification method is not called so I am unable to handle that notification.

And another issue is some times it shows the notification message in device notification list and some times it didn't .

When I am entering in my app through clicking on notification list item the didReceiveRemoteNotification calls and I am successfully able to handle notification. I write following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif != nil)
{
    NSLog(@"didFinishLaunchingWithOptions\nNotification recieved:\n%@",remoteNotif);

    notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif];
    [notif saveNotification:remoteNotif];
}

Help me to resolve this . Thanks in advance.

like image 835
Raj Avatar asked Aug 06 '13 09:08

Raj


People also ask

How do I fix Apple push notifications?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

Why are push notifications not working?

Settings > Sounds & Vibration > Do Not Disturb: if this setting is enabled, Push Notifications will not be received. Make sure this is disabled. Settings > General > Background App Refresh: this setting allows the app to run in the background and must be turned on.

How do I enable push notifications on iOS?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.


2 Answers

Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification as well. When you will come from background, didfinishlaunch won't be called.

- (void)application:(UIApplication*)application didReceiveRemoteNotification: 
(NSDictionary*)userInfo
{
         UIApplicationState state = [application applicationState];
         if (state == UIApplicationStateActive)
         { 
              //What you want to do when your app was active and it got push notification
         }
         else if (state == UIApplicationStateInactive) 
         {
              //What you want to do when your app was in background and it got push notification
         }
}
like image 156
Baby Groot Avatar answered Oct 03 '22 20:10

Baby Groot


From Apple's Local And Push Notification programming guide

Handling Local and Remote Notifications

Let’s review the possible scenarios when the system delivers a local notification or a remote notification for an application.

  1. The notification is delivered when the application isn’t running in the foreground.

    In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

    As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.

    If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

    If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

  2. The notification is delivered when the application is running in the foreground.

    The application calls its delegate’s application:didReceiveRemoteNotification: method (for remote notifications) or application:didReceiveLocalNotification: method (for local notifications) and passes in the notification payload or the local-notification object.

So in your case, when application is running in background, and when you click the notification/alert, operating system brings your app into foreground. So it falls under second point.

You can implement application:didReceiveRemoteNotification: method to get the notification payload, if action button is tapped. But when the application icon is pressed instead of action message, the notification payload is not forwarded with the method. Your only option is to contact your server and sync the data. After all as per Apple's policy, push notification only tells that there is data on server, and either way you need to connect to server and get the data.

like image 20
Krishnabhadra Avatar answered Oct 03 '22 22:10

Krishnabhadra