I am facing issue in launch of particular application page when user will get push notification in ios.
is this possible to do?
if yes, can you please help me in that.
You could send a notification to yourself when you get any remote notification and by registering the UIViewController to this notification, you could open a particular UIViewController once notification is received.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[[NSNotificationCenter defaultCenter] postNotificationName:#"pushNotification" object:nil userInfo:userInfo];
}
In your FirstViewController.m register for listening to this notification.
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pushNotificationReceived) name:#"pushNotification" object:nil];
}
Inside the method you could open particular viewController
-(void)pushNotificationReceived{
[self presentViewController:self.secondViewController animated:YES completion:nil];
}
Finally un-register current viewController from notification in dealloc method
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Let me know if you face any problem with this.
Related
My view controller registers to keyboard notifications (keyboardWillShow, keyboardWillHide).
I launch my app. It is showing the viewcontroller that is registered to keyboard notifications. The keyboard is not visible.
I switch to the sms app and start writing text. While I'm writing, my app gets a notification. The notification is displayed as a banner on the top of the screen.
When I click the banner, my app is opened and immediately gets a keyboard notification.
As far as I can tell, this keyboard notification is related to the keyboard of the SMS.
How do I identify if the keyboard event came from my app or not?
you can remove listening to observers (keyboard notifications) in viewWillDisappear and can start listening to observer again in viewWillAppear, this might solve the problem
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterBackground:)
name:UIApplicationWillResignActiveNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillDisappear:animated];
[self registerForKeyboardNotifications];
}
- (void)deregisterForKeyboardNotifications {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self deregisterForKeyboardNotifications];
}
The NSNotification observer is in didFinishLaunchingWithOptions
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newShipCome:) name:kNewShipNotifaction object:nil];
I would like to remove observer in another ViewController.
I've done this in one of my ViewController
[[NSNotificationCenter defaultCenter] removeObserver:[UIApplication sharedApplication] name:kNewShipNotifaction object:nil];
but still not work. Do anyone know how to do this?
thanks
You need to remove observer of AppDelegate, you are passing incorrect object in removeObserver: method.
Instead of :
[[NSNotificationCenter defaultCenter] removeObserver:[UIApplication sharedApplication] name:kNewShipNotifaction object:nil];
Use:
[[NSNotificationCenter defaultCenter] removeObserver:[[UIApplication sharedApplication] delegate] name:kNewShipNotifaction object:nil];
I tried to set badgeValue for UITabBarItem when push notification is received.I am using this code. Here the UITabBarController is not a rootViewController. I tried the same thing in resign active method but there also its not working.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UITabBarController *tabBarController = (UITabBarController *)[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"tabBarController"] ;
[[tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:#"1"];
}
I think that you can use NSNotificationCenter to post notification when you received a remoteNotification
In your UITabBarController initialize method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myNotificationReceived:) name:#"pushNotification" object:nil];
And
In myNotificationReceived:
[[self.tabBar.items objectAtIndex:2] setBadgeValue:#"1"];
When you receive a remote notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"pushNotification" object:nil userInfo:userInfo];
In this way, you can get whole RemoteNotification information
So I've got a UIActivityViewController that takes up half the screen when my app runs, what happens is when I click the half it doesn't take up then it'll disappear and my original blank ViewController appears.
Is it possible to permanently keep the UIActivityView up until the user clicks on one of the sharing options (such as Facebook, or Mail)? Otherwise I'll just work around it.
I load the ActivityView with:
-(void)viewDidAppear:(BOOL)animated{
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:#[#""] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
}
Yes, it is possible, you can try something like
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showAViewController) name:#"showAViewController" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dissmissAViewController) name:#"dissmissAViewController" object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"showAViewController" object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"dissmissAViewController" object:nil];
}
-(void)showAViewController{
self.view.userInteractionEnabled=NO;
}
-(void)dissmissAViewController{
self.view.userInteractionEnabled=YES;
}
When you are showing a view controller on top of another view controller just call
[[NSNotificationCenter defaultCenter] postNotificationName:#"showAViewController" object:nil];
After you dismiss it or remove it, just call:-
[[NSNotificationCenter defaultCenter] postNotificationName:#"dissmissAViewController" object:nil];
I'm working with UILocalNotification and I would like to notify one of my controller that the notification has been received even if the app has been terminated.
In my appDelegate I implemented this function:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"localNotificationReceived" object:notification.userInfo];
}
}
In my UIViewController I implemented the observer on the viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didlocalNotificationReceived:) name:#"localNotificationReceived" object:nil];
}
It works perfectly when the app run in background. Since the viewDidLoad method has already been called and the Observer is waiting..
The issue is when I kill the app. Then the observer of my controller is gone and the didlocalNotificationReceived method is never called.
I think that it's because when I receive the localNotification and run the app again. The didReceiveLocalNotification: method is called before the viewDidLoad of my UIViewController. Then the observer is created after the PostNotificationName then the observer receives nothing.
I would like to know if there is some best practices or pattern to handle this kind of issue.
I know that the didFinishLaunchingWithOptions method is called before didlocalNotificationReceived so there is probably something to do there.
UPDATE :
I also discovered that when is app is terminated. Once you tap the notification, It opens the app, call the function didFinishLaunchingWithOptions but never call didReceiveLocalNotification. So I think that I'm gonna handle both cases differently.
Ok I found the answer.
I actually, initialize manually my storyboard, and be cautious that I initialize my main view before posting the NSNotification
My didFinishLaunchingWithOptions: method in my appDelegate looks like that:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController]; //call the initWithCoder: method of my controller
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
[[NSNotificationCenter defaultCenter] postNotificationName:#"localNotificationReceived" object:localNotification.userInfo];
}
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
Then in my UIViewController I create the NSNotification observer in the initWithCoder: method instead of in viewDidLoad:
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didlocalNotificationReceived:) name:#"localNotificationReceived" object:nil];
}
return self;
}
- (void)didlocalNotificationReceived:(NSNotification *)notification
{
//Execute whatever method when received local notification
}
And when the app is not killed I still use the didReceiveLocalNotification: method:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"localNotificationReceived" object:notification.userInfo];
}
}
I'm not sure if it's the best practice. But it works well !
Hope it'll help :)