Push notification and view button action[iphone sdk APNS] - iphone

I am developing a Push Notification enabled application for Iphone.
In My application I have two List View (UITableView)
1st for Category List and the 2nd is Contents List.
User clicks the desired category then the contents related to that category will be displayed then user will choose the contents and the contents will be displayed in detail view(generally a UIWebView).
Push notification is successfully coming in my application.
My requirement is:-
After VIEW button of Push alert is clicked application will directly display a particular
detail view (UIWebView)[Omitting category and contents list].
I have a unique ID for category and contents.
So will you please guide me how to relate a particular content with Push Notification and directly display of that content.
Thanks and regards.

HI,
I have solved the problem.
This is what I have done.
When application received push notification, it stored notification in launchOptions NSDictionary.
/* Push notification received when app is not running */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *params=[[launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:#"contTag"];
if ([params length] > 0 ) {//app launch when VIEW button of push notification clicked
//do some processing
........
WebViewController *webViewController =
[[WebViewController alloc] initWithNibName:#"WebView" bundle:[NSBundle mainBundle]];
// Put your custom code
[[self navigationController ] pushViewController:webViewController animated:YES];
[window addSubview:navigationController.view];
/* Remote Notification Received while application was open. */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"remote notification: %#",[userInfo description]);
NSString *contentsInfo = [userInfo objectForKey:#"contTag"];
NSLog(#"Received contents info : %#", contentsInfo);
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alert = [apsInfo objectForKey:#"alert"];
NSLog(#"Received Push Alert: %#", alert);
NSString *sound = [apsInfo objectForKey:#"sound"];
NSLog(#"Received Push Sound: %#", sound);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//-----------------------APNS HANDLE----------------
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive){
NSLog(#" It is in active state");
application.applicationIconBadgeNumber = [[apsInfo objectForKey:#"badge"] integerValue];
}
else {
if ([contentsInfo length] > 0 ) {
// Do whatever u want for push notification handle
}
NOTE:
Here contTag is a key set in server side for pay load of push notification.
U can set any key in server side.
Hope it will help some body.
Thanks

Related

How to show remote push notification as a banner style in active state of app?

I am making an app in which I am using apple push notification. When my app is in background state then I am able to receive notification as a banner but when my app is in active state then I am able to show an alert that you have received a notification through this code : -
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
} else {
//Do stuff that you would do if the application was not active
}
}
but I want to show notification as a banner. What I'll do for it? Please suggest.
You can't, if your app is active(running in the foreground) the push notification is directly send to your app. The notification center will not handle the notification.
You will have to implement some kind of banner your self.

Can UILocalNotification fire a custom method when App is in background mode?

Well the title is self explained. I want to create an App that can manage programmed local notifications when App is in background mode. Notifications works smoothly but I want to fire a custom method when the alert is fired.
Is it possible?
Thank you.
Yes it can be done. You can do somethng like this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:#selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}
-(void)makeNotificationRequest:(NSTimer *)timer
{
CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
#ifdef _DEBUG
[paramDic setValue:[NSString stringWithFormat:#"77.586"] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"12.994"] forKey:#"long"];
#else
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.latitude] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.longitude] forKey:#"long"];
#endif
WNetwork *mNetwork = [[WNetwork alloc] init];
[mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
[mNetwork autorelease];
NSLog(#"URL HIT%#",paramDic);
[paramDic autorelease];
}
And to customize your action on alert you can use this:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
;
}
}
If the app were in the foreground then you would want the - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification method of UIApplicationDelegate, however the documentation suggests that this will not be called when the app is in the background.
You can get a list of local notifications by calling scheduledLocalNotifications on your UIApplication instance - you can then poll these for their times and schedule a function to call at that time in your background mode. This won't necessarily 100% match up with when the Local Notification fires though, but I think it's as close as the App Store guidelines will let you get.
You can also present your own Local Notifications when you are in background mode by calling the presentLocalNotificationNow: method:
https://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/presentLocalNotificationNow:
so you could work around this by just presenting your own notifications and not scheduling them for the OS to deliver.
If you are trying to access Local Notifications from other applications then I don't think this is allowed.
No ..no custom methods are defined while app is running in background..Once an notification is fired we can;t change the alert message also. But nice when we show the alert message then by clicking the yes button to the alert take you to the one method called app is Running in Background which is in AppDelegate.m file

iphone: Set badge on a tabbarItem when receiving a PUSH message, when the app is inactive

I have a application that uses PUSH. But I have one problem when the application is inactive/in the background.
When the PUSH messages come and the user clicks on Close, the badge is set on the application-icon.
But I also want to set a badge on a tabBarItem.
I have this code that saves the PUSH
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
//Save the PUSH until the app is active.
newPush = [userInfo copy];
}
}
And in:
- (void)applicationDidBecomeActive:(UIApplication *)application
I have the following code:
//Check if there is new PUSH messages.
if (newPush!=nil) {
//There is a new PUSH!
NSInteger badge = [[[newPush objectForKey:#"aps"] objectForKey:#"badge"] intValue];
if (badge > 0) {
//Set badge-numbers to 'badge'
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:[NSString stringWithFormat:#"%d",badge]];
}
else {
//Set badge-numbers to zero
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:nil];
}
}
My code for handling the PUSH when the application is active works fine and the badges are set both on the application-icon and on the tabBarItem.
Someone know what's wrong?
Thanks in advance!
If the application is inactive, didReceiveRemoteNotification is not executed. The only way the notification data can reach your app in this case is if the user taps on the notification to open the app. Then, when the app is launched, you can get the notification data in application:didFinishLaunchingWithOptions: by using this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif];
return YES;
}
return YES;
}

Crash when handling remote notification when app not running

I receive a remote notification and according to the type of notification, change navigation controller's view controllers.
It all works fine when the app is in the foreground, or when the app is in the background but not completely closed (from multi-tasking bar).
But, when the app is closed, and receives a remote notification it crashes as soon as it opens. Am I doing wrong with the way I am setting up the ViewControllers?
Here's some code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Push required screens into navigation controller
UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif.userInfo];
return YES;
}
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
-(void) handleRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
NSMutableArray *viewControllers = [NSMutableArray array];
[viewControllers addObject:driverWaitViewController];
[viewControllers addObject:newJobsViewController];
[navigationController setViewControllers:viewControllers];
}
I got this resolved, and it has nothing to do with view controllers, as I thought.
The issue was in the following lines. I was sending in remoteNotif.userInfo rather than remoteNotif itself. Also, remoteNotif is obviously not of type UILocalNotification. It is a NSDictionary object.
Before
UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
[self handleRemoteNotification:application userInfo:remoteNotif.userInfo];
Should be
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
[self handleRemoteNotification:application userInfo:remoteNotif];
if you close the app which start from xcode debug mode, and when the app start with push notification(closed app) if the your phone connected to mac(still your phone in debug mode with xcode) it will be crash. test this senario with unplugged phone.
You aren't properly initializing your application when receiving a notification.
Change the application:didFinishLaunchingWithOptions: method to this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
// Push required screens into navigation controller
NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
//Accept push notification when app is not open
if (notif) {
[self handleRemoteNotification:application userInfo:notif];
}
return YES;
}

Apple Push Notification Service using Urban Airship in iPhone

I have implemented Apple Push Notification service using Urban Airship and successfully received the notification in my application. If i receive the notification the alert view comes, If i click the view button in alert view, it goes to start the application. Generally it happens in APNS. But my client wants, If any updates happened in the RSS feed and alert view comes, If we click the view in alert view, it should go to the particular feed in the application, doesn't start the application. SO is it possible to do that?. Is any possible to write the events for the particular alert view buttons in my application.
Here my sample code is,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
[window addSubview:viewcontrollers.view];
[window makeKeyAndVisible];
NSLog(#"remote notification2: %#",[launchOptions description]);
return YES;
}
In this method didFinishLaunchingWithOptions, i cant get the dictionary values and it always get the Null Value. Is any possible to get the dictionary value in this method(Notification comes).
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *message = [userInfo descriptionWithLocale:nil indent: 1];
NSLog(#"The message string is %#",message);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Remote Notification" message: message delegate: nil cancelButtonTitle: #"ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
In this method, i could get the dictionary value. But this method calls only,if any update happens while running in the application.
Please guide me!
Thanks
It is indeed possible to do that. In your application:didReceiveRemoteNotification method you'll be passed an NSDictionary with all of the push notification's data. What you'll want to do is send some ID or URL in the payload to Urban Airship. Something like:
{
"aps": {
"alert": "New RSS entry"
},
"entry_id": "XYZ123"
}
And then you can write code to go and fetch the proper feed entry in your application.
When the application isn't running or has been terminated by the system and the app is launched via the notification:
In this case you have to get the notification dictionary (which is itself a value of the launchOptions):
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006786-CH3-SW18
I imagine the code would be something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary *remoteNotification = (NSDictionary *) [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification != nil) {
NSString *message = [remoteNotification descriptionWithLocale:nil indent: 1];
}
}