Reputation: 83
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
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];
}
}
Upvotes: 1
Views: 1761
Reputation: 651
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.
Upvotes: 0