I need to show a universal custom view whenever i received a notification in a active app.
I have created a view , but i am not getting how to show that!
Can anyone help me out.
You have to handle the notification in the AppDelegate methods (usually application(_:didReceiveRemoteNotification:fetchCompletionHandler:)) and launch the view in the form it can be shown in any screen of your app.
You can read the docs for UIApplicationDelegate here, section Responding to Notifications and Events.
You will be receiving the push notification in AppDelegate's application(_:didReceiveRemoteNotification:fetchCompletionHandler:) from here you can handle.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
// 1. You can call your universal view from here
OR
// 2. You can post this local notification with information
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:#"info"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"showNotification"
object:nil
userInfo:userInfo];
}
For 2nd Option, you will need add observer and receiving method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"showNotification"
object:nil];
-(void)receiveNotification:(NSNotification *)notification {
// call your view
}
Related
I have implemented everything correctly in my app to receive push notifications, i'm receiving the notifications just fine, but how can i do something when the user clicks on it?
Here's my code regarding the matter:
AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
NSLog(#"Hello from appDelegate");
}
It's working, i'm getting the userInfo and the other message in my Xcode log.
Now i want to do something (go to a specific segue) when the user clicks on the notification. I have seen the docs but it's very complicated and hard to follow.
I just need to know what function to use in the MainViewController.m
Any hints? thanks in advance.
You can post notifications (Using NSNotificationCenter) from AppDelegate on different events and then add observer in particular classes where ever you want to perform a particular operation.
Code Implementation
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *aStrEventType = userInfo[#"eventType"];
if ([aStrEventType isEqualToString:#"callWebService"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"callWebService" object:nil];
}else{
// Implement other notification here
}
}
Now in your particular class you can handle notification as follows.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:#"" selector:#selector(callMyWebService) name:nil object:nil];
}
-(void)callMyWebService{
//Perform your action.
}
I have some problems with NSNotificationCenter and didReceiveRemoteNotification. I want to open my ViewController when i receive new notification from APNS. Inside body notification i have objectId - it's key.
I try to open my ViewController into didReceiveRemoteNotification but it's not working ((
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter]
postNotificationName:kDidReceiveRemoteNotification
object:userInfo];
}
NewsDetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(didReceiveRemoteNotification:)
name:kDidReceiveRemoteNotification
object:nil];
}
- (void)didReceiveRemoteNotification:(NSNotification *)notification
{
NSLog(#"%s %#",__func__,[notification.userInfo description]);
}
Const.h
#define kDidReceiveRemoteNotification #"UIApplicationDidReceiveRemoteNotification"
ViewController not loaded. i dont know what to do.
The current flow of the sample code you've attached is:
When receiving push notification, post a notification to NSNotificationCenter (something completely different and unrelated to push notifications).
When another view is loaded (someone needed to load this view before the push notification was received), subscribe to this NSNotificationCenter notification.
When the notification is posted, print it to log.
It was hard for me to understand this from your question, but if the view controller you're trying to launch as a result of receiving the push notification is NewsDetailViewController, then your code doesn't do that. What you're code does is prints out the notification to log (assuming that somebody else made sure that NewsDetailViewController is loaded before the push notification was received).
In order to load the NewsDetailViewController when the push notification is received, you don't need to post the notification to NSNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NewsDetailViewController *newsVC = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
[self.window.rootViewController.view addSubview:newsVC.view];
}
Or any other loading logic that works better for you. But in the current code you posted, there is no connection between receiving the push notification and loading the ViewController.
I hope this helps. Good luck!
I have a class which will make NSNotificationCenter to observe whether the app is active or it went to background and it will do a set of actions according to that.
I want to set a NSNotificationCenter to observe whether the app is opened through URL scheme .
This is what I tried:
[[NSNotificationCenter defaultCenter] addObserver:[self class] selector:#selector(appOpenedThroughUrl:) name:UIApplicationLaunchOptionsURLKey object:nil];
But its not getting called when the app opens through URL please provide me some idea to do the same.
I don't want to use the delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
+(void)appOpenedThroughUrl:(NSNotification *)notification {
NSLog(#"%#",notification.userInfo);
}
you need to post notification whenever it open through custom url scheme
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationLaunchOptionsURLKey object:self];
In my app, there's a collection view displaying a set of images retrieved from a web service. Each image has tags. So the app has the ability to filter images using tags as well.
Now I'm trying to add push notifications to this app. A push notification is sent when new images have been added to the server. These images are tagged say, latest. I'm passing that tag as the message via a push notification and what I need is when the user taps on the push notification to open the app, it should load the latest new images to the collection view.
I'm half way done. I receive the push notification with the message successfully to the didReceiveRemoteNotification method in the AppDelegate.m file. Now I need to pass it on to the view controller where the collection view is. I'm stuck at this point. I can't figure out how to send it over to the view controller.
I tried declaring a property in the App delegate, assign the message value to it and referring it from the view controller but it didn't work. I tied delegates, notification center, user defaults but nothing worked.
Can anyone please tell me how to accomplish this?
Thank you.
Edit:
Here's my code. The last method I tried was the local notifications.
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"PushNotificationMessageReceivedNotification" object:nil userInfo:userInfo];
}
ViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(remoteNotificationReceived:) name:#"PushNotificationMessageReceivedNotification"
object:nil];
}
- (void)remoteNotificationReceived:(NSNotification *)notification
{
NSLog(#"Notification: %#", notification.userInfo);
NSString *msg = [[notification.userInfo valueForKey:#"aps"] valueForKey:#"alert"];
self.label.text = msg;
}
Case 1: if your app is background and user launches app with notification click then you have the check if app launched form notification or normal
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSDictionary *remoteNotificationPayload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotificationPayload) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"notification" object:nil userInfo:remoteNotificationPayload];
}
return YES; }
Case2: If your app is in forground notification will be received in didReceiveRemoteNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"userinfo %#",userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:#"notification" object:nil userInfo:userInfo];
}
Now you and add a observer in any controller with Local notification and do what you wand to do
I don't know it will work or not,its only my suggestion. I did't tried it before but in your case may be it work
user NSUserDefaults for this.
in your appDelegate.m
[[NSUserDefaults standardUserDefaults] setObject:imageArrayFromServer forKey:#"MyAppSpecificGloballyUniqueString"];
in your viewController.m
NSArray *myArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"MyAppSpecificGloballyUniqueString"];
Can i use below method somewhere other than AppDelegate ?if yes how?
- (void)applicationDidEnterBackground:(UIApplication *)application
No, But you can have other objects register for the UIApplicationDidEnterBackgroundNotification notification. These objects will then be notified at the same time applicationDidEnterBackground: is called.
This is a method of the UIApplicationDelegate protocol, and can only be implemented by classes that conform to it.
You can set up a notification to other objects in your app from your app delegate by using the NSNotificationCenter object:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:#"didEnterBackground" object:self];
}
There is also the UIApplicationDidEnterBackgroundNotification notification that you can listen for instead of doing the above.
Register the objects that you want to listen for the notification like this:
[[NSNotificationCenter defaultCenter] addObserver:someObject selector:#selector(someMethod:) name:#"UIApplicationDidEnterBackgroundNotification" object:nil];