简体   繁体   中英

BackGround App refresh and apple push notifications.

In my app I am using APNS. I am getting push notifications when app is running and in foreground state. When i press home button i am unable to receive push notifications. I need help for two reasons

  1. How can I get push notifications when my app is at background state?
  2. If my app is not running or not-launched and push notification comes, I want to update data at background without user tap.

I'll be very thankful if you could provide me a brief tutorial. Thanks in advance

My code is:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

if(application.applicationState == UIApplicationStateActive)
{
    NSLog(@"Active");
    [self addData];
}
else
{
    NSLog(@"Not active");
    [self addData];
}

}

Please follow the steps

First up all you have to register for push notification in appdelegate

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound |   UIRemoteNotificationTypeAlert)];

Now check for launching option for notification.

NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{}

Now save token id of notification and send to server

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString* deviceT = [[[[deviceToken description]
                           stringByReplacingOccurrencesOfString: @"<" withString: @""]
                          stringByReplacingOccurrencesOfString: @">" withString: @""]
                         stringByReplacingOccurrencesOfString: @" " withString: @""];
    [UserDefault setDeviceToken:deviceT];
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
}

Now in device setting check Notification centre that your application is present in that or not. If your application is not in list then you will not get notifications. Push notification always come if your application is background. you can first check device setting- notification centre-your app availability.

- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
   if(app.applicationIconBadgeNumber!=0)
   {
        app.applicationIconBadgeNumber = app.applicationIconBadgeNumber - 1;
    }
if (app.applicationState == UIApplicationStateInactive)
    {

    }
    else if (app.applicationState == UIApplicationStateActive)
    {

        [self showNotificationInActiveMode:userInfo.alertBody];
    }
}

For the second point,you can not update data until and unless application not running. if you want to update data then you have to click on push notification for launch or open application in foreground. Without not running or not-launched you can not update data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM