Push Notification Delegates not getting called - iphone

I am implementing push notification for a client. I have followed all the steps of push notifications setup on device (including certificate generation and everything).
I have used following links as reference:
Apple Push Notification Services Tutorial
Programming Apple Push Notification Services
I call this function in app delegates didFinishLaunchingWithOptions
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
After setup, i run the app i get the Blue Push notification warning dialogue but after I press Ok the delegates are not called.
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
None of these delegates are called ever. I am stuck here as i cannot proceed further as none of the delegates is called.
If any of you has faced a similar issue i would be very very thankful if you can help me out here.

I have seen this before, you need to just add [application registerForRemoteNotifications]; to your code, where application is the instance of UIApplication inside the didFinishLaunchingWithOptions method of the app delegate. Make sure you add this once you have set up your UINotificationType, and UIUserNotificationSettings

Related

Clear push notification badge after increment

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.
and its not working for me.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
I use this code in my app because the Urban Airship (UA) documentation said to
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];
but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.
EDIT #1:
I received the following response from a support technician at UA with the following suggestions, which worked great:
What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:
[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup
and also call [[UAPush shared] resetBadge]; in the following methods as well:
applicationDidBecomeActive:(UIApplication *)application
and
didReceiveRemoteNotification:(NSDictionary *)userInfo
The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.
EDIT #2:
I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
{
appState = application.applicationState;
}
[[UAPush shared] handleNotification:userInfo applicationState:appState];
[[UAPush shared] resetBadge];
}
because I was having a problem with the following scenario:
User is in app
Push notification received
No badge was displayed on app icon when returning to home screen (as expected)
Another push notification is received
Badge displayed number greater than 1
With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.
I have used Urban Airship and have had this problem before. You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number.

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

get push notification while the app is not running iPhone

I am working on one iPhone app which involves a push notification. As I have seen in many documents and tutorials it suggests to register for push notification with in
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
like the following:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
...
}
now the question is, if the app was not running (even in background), when the push come, it cant process the push message, but if I use the push message again from the notification area and lunch the app again, I can get my message.
what I need to do to make my app get the push message even when it lunch for the first time?
You might be conflating the notion of registering for and receiving notifications. It is impossible for an app to receive a push notification before the registerForRemoteNotificationTypes: method is called the first time, since this method provides the push token that is used to send push notifications in the first place.
So, you must be talking about receiving notifications under the two separate situations in which they can be delivered: upon initial app launch, and during the execution of the program.
In order to handle notifications of the first type, you must inspect the options dictionary sent to application:didFinishLaunchingWithOptions:. The following code shows how to route a notification received at launch to the delegate method that is called when a push notification arrives while the app is already running.
Place this in your application:didFinishLaunchingWithOptions: override:
NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}

Problem With PushNotification if you select No at the first time

As i said in the title of this thread, I have a problem with Push notification of my application.
There is 2 cases :
When the app asks you if you want push notification and you push OK, it's working great.
This function :
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
is called when registerForRemoteNotificationTypes is called :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add registration for remote notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
...
}
But when you select NO at the beginning, and then you change your settings to accept push notification, nothing append and this function didRegisterForRemoteNotificationsWithDeviceToken is never called.
Please help me, i'm getting crazy!
I see you register for remote notifications in:
- (void)applicationDidFinishLaunching:(UIApplication *)application
If your application is running on a iOS device with support for multitasking that might not be the right place.
The above method is called when you initially launch your app from cold. This is when you are prompted to accept or decline Push Notifications - and in your case decline.
When you later exit the application to change the settings in the device wide control panel the application is not quit - it is merely resigning as the foreground application. When you have enabled Push Notifications in the control panel and return to your application - the application is not launched again but moves from being backgrounded to the foreground.
Your application delegate will at this point receive an:
- (void) applicationDidBecomeActive:(UIApplication *)application
try registering for Push Notifications there instead using:
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
You could also prevent backgrounding by setting the “Application does not run in background” property in the application’s Info.plist file (UIApplicationExitsOnSuspend).

How to Send Device Token in APNS

I Have implemented following code but not getting the Device Token?
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"deviceToken: %#", deviceToken);
}
If you don't already, you should have a call to registerForRemoteNotificationTypes in your didFinishLaunchingWithOptions. Something along the lines of:
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
You should also have a didFailToRegisterForRemoteNotificationsWithError method that gets called if registration fails. The NSerror it gets should tell you more about why it might be failing.
Are you trying inside of the simulator ?
Push notification and related appdelagate events doesn't work on simulator.
If you are using the device,
Make sure that bundle identifier of your application must be same with your push ssl bundle identitifier which you defined during creating AppID on iphone privisioning protal.
For registering your device with APNs, you will first have to call
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
this is usually written in your AppDelegate (in didFinishLaunching ).
Then make sure you have implemented the
didRegisterForRemoteNotificationsWithDeviceToken
which provides you with the device token and
didFailToRegisterForRemoteNotificationsWithError
which gives you the error your code might have encountered.