Push Notifications on iPhone App - iphone

I have successfully implemented push notifications to my app. The next step is, if the user receives a notification about this certain document, he/ she clicks it and it brings them to this document.
At the moment, I can receive the notification, but it only brings me to the app.
How can I do this?
Thanks.

I am not clear with your question. But you should handle push notifications in the following methods,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Handle here
}
and
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *tmpDic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (tmpDic != nil) {
//Handle here also
}
}

Related

Redirecting the user to a specific screen of my app after sliding over a notification to unlock

In my app , local notifications are displaying correctly. But,my doubt is,i need to redirect to specific screen in app while i'm slide the unlock to notification.
Do the below in app delegate.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state != UIApplicationStateActive)
{
// redirect to the next screen.
}
}
Hope it will help you.
Try this out,
You will get local notification in this method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
or for remote notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
and after that you can push your screen i.e.
if(you are in another screen)
push your required screen
else
do nothing.

Disable UrbanAirship alerts

I want to ignore push notifications when the app is active. I am handling notifications as follows:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState != UIApplicationStateActive)
{
[[PushHelper shared] processPush: userInfo];
}
}
But when app is active and device receives push notification, the UIAlertView with notification message appears. How can I disable default handling from UA?
I had the same problem and found solution.
If define the delegate method displayNotificationAlert: of UAPushNotificationDelegate protocol with empty body, for example, then the automatic alerts will not be shown:
{
...
[[UAPush shared] registerForRemoteNotifications];
[UAPush shared].pushNotificationDelegate = self;
...
}
- (void)displayNotificationAlert:(NSString *)alertMessage
{
}
If you don't need to do anything with the push notification itself just remove the [[PushHelper shared] processPush: userInfo] from your code
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//nothing to do here
}
The didReceiveRemoteNotification method is only called when the app is running.

How to get notification.alertBody in my app from a remote push notification?

I just followed this tutorial Push notification and I successfully implemented the push notification for my iPhone app. Im able to get now the notification Details. However, I wanted to put the notification alertBody on a Label provided for notification alertBody.
I have a code in displaying the notification alertBody from a local Notification. But I know it is different from a push notification because it is used only for local notification.
on my AppDelagate.m
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(#"Recieved Notification %#",notif);
NSString *_stringFromNotification = notif.alertBody;
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:_stringFromNotification];
}
on my ViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:#"Notification" object:nil queue:nil usingBlock:^(NSNotification *note)
NSString *_string = note.object;
//Do something with the string--------
}];
}
It works perfectly on Local Notification but for push notification, It doesn't work. How to Implement this? Need your help please. I need to put the notification alert body at the Label or String.
first of all register for remote notifications in AppDelegate.m in method,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Invoke APNS.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
And then use following delegate method to recieve remote notification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received =%#",userInfo);////userInfo will contain all the details of notification like alert body.
}
Remote notifications run outside the sandbox the app is running in, so you can't capture the notification in the same way as local notifications, i.e. application:didReceiveLocalNotification. However, if the app is launched via the remote notification, you can capture the notification via the application:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// do something with the notification.alertBody
} else {
// from the springboard
}
}
If your application is already running when it receives a remote notification, your application delegate's – application:didReceiveRemoteNotification: method will be called; if the application is not currently running and is launched in response to the notification, the remote notification info will be put into the launchOptions dictionary in your – application:didFinishLaunchingWithOptions: method.
The method which you are implementing is for local notification only. If you want to handle push notification then you have to use method
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSLog(#"Received notification: %#", userInfo);
}
for the same. This method will be called if the app is on background only. If the app is not in background then you can fetch data in the following manner
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *notificationData = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if(!notificationData){
NSLog(#"App launched by tapping on app icon");
}else{
NSLog(#"Notification data -> %#",notificationData);
}
}

localNotification issues

I am sending a local notification once a process is finished and it acts just fine.
This is my code for didReceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
CounterViewController *counterVC = [CounterViewController sharedInstance];
[counterVC notificationArrived];
}
But when the iPhone is locked those lines aren't called… What can I do to make them run in the background?
There are two method to receive local notification one is already you have implemented which is invoked while app is running & 2nd is in didFinishLaunchingWithOptions which is invoked while your app is running background you have add some code for receive local notification .....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
}
return YES;
}

when application become active then how we load the another view or invoke the root view controller

i have load the mailcomposer on reciving the localnotification
is it possible??
You should code as follows,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
//code for opening mail composer
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//code for opening mail composer
}