How to handle pushnotification service in iphone running app - iphone

I have implemented push notification service in iphone device, but running app will not get the notification service alert message, how to handle the APN service in running app.
Thanks in advance

Just implement in your application delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
and you'll be happy =)

Implement the following code in the delegate method:
(void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey];
}
Also follow the link. It must help you.
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html

Related

iOS push notifications work different in different application states

I send 10 messages in row to APNS.When application is in background, I receive all of them.
But when Application is in foreground I recieve 8 notifications.
What can be a problem here?
Here is my code which is extremely simple:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"Test push...");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeNone)];
[self checkForRegisteredUser];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
return YES;
}
Not sure will this is what you're looking for,
Here is how push notification works,
So when you application is on background, you application will never receive the push notification from the APNs but it will come to your notification tray with the message as they sent it from the server.
But if your application is in the foreground. the method in you app delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
will receive the notification and will perform the necessary action as you have defined in it.
In case if you did not receive the notification you need to check the network connection.
I have developed a similar application like yours and its works well as you are expecting..

Push notification data retrieving

I want to get data from push notification message. I successfully get the data when app is on foreground and in background. but I am unable to get data when app is quit and user press view button on push notification. I write the code in application did finish launching. This code cause the app crash when pressing on View button of push notification message. If I comment the code then app doesn't crash. Kindly help me to fetch data from push notification when app is quit and user press view button on push notification. I'll really appreciate that.
if(launchOptions != nil){
NSDictionary *tmpDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (tmpDic!=nil) {
pushedMessage=[NSString stringWithFormat:#"%#",[[tmpDic objectForKey:#"aps"] objectForKey:#"alert"]];
pushedCountry=[NSString stringWithFormat:#"%#",[tmpDic objectForKey:#"country"]];
[self saveToDatabase];
}
}
Please try this...
Add this code to appdelegate.m => didFinishLaunchingWithOptions
if ([launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
From ios7 we have the below delegate method to handle the push notification when the app is in back ground or not running
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
When you click view button
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
}
this method is called and userinfo will contain all data
Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification:
I don't totally understand what your asking but you can do stuff with what is being being push with a function in the app delegate
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
Now you can add that data to core data or sqlite. This may not be relevant to your question but it's the best I can give based on what you've asked in your question.
See my comment on the above answer.
Here is Apple's doc:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
So you need to implement:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
As well as handling the launchOptions in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Push Notifications stopped working after recent update

Hi I have an app on the App Store which used to receive Push Notifications perfectly but after a recent update it seems to have stopped.
I have not changed any of the notification code so I was wandering if there have been any changes in iOS 5.1 which may be causing the issue.
Anyone had a similar experience and know how to fix it?
In the application delegate I am registering for notifications:
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
and implementing the following methods:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

Which callback get executed when my app is openned from a push

Hi there i would like to know which app delegate method get called when my app launch from a push notification (When the app was previously in the background ?)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Also check this answer.
Implement the didReceiveLocalNotification: method in your AppDelegate.
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notification here
}
Check the documentation of UIApplicationDelegate

How to remove the cancel ApplicationIconBadgeNumber from LocalNotification?

I am using LocalNotification in my app. It is working fine but once ApplicationIconBadgeNumber is set, not able to remove it from App. How to remove it?
You need to set the application applicationIconBadgeNumber.
For example in the application didReceiveLocalNotification.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif {
application.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;
}