I have a push notification that I am sending to a user and I want to be able to take an action when they tap on it. I know that if the app is in the foreground, background, or if the user taps on the alert from the notification center that the following method is called in the app delegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
However, if the app is not launched and the user taps on a notification banner as soon as the notification arrives, this method does not seem to get called. Is their a different method that I need to impliment in this situation? Are their other cases where other methods should be implemented as well?
If you app is not launched when clicking on a notification banner, then you will receive an NSDictionary in your application:didFinishLaunchingWithOptions:.
Then you can just do something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushDict)
{
[self application:application didReceiveRemoteNotification:pushDict];
}
}
Additionally, in your application:didReceiveRemoteNtification: method, you can test if your application was inactive at the time the notification was received, like this:
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([app applicationState] == UIApplicationStateInactive)
{
NSLog(#"Received notifications while inactive.");
}
else
{
NSLog(#"Received notifications while active.");
}
Related
I am now using parse to set up Facebook login, but it will jump into Safari when I click PFLogInFieldsFacebook button. I want to have a in APP login page(UIWebView or something). I want to do that because I noticed that IOS 9 will give a back to Safari when I am back to my APP. Here is the code I am using:
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self]; // Set ourselves as the delegate
// Create the sign up view controller
logInViewController.fields = PFLogInFieldsFacebook;
// Present the log in view controller
[self presentViewController:logInViewController animated:YES completion:NULL];
logInViewController.logInView.layer.contents = (id)[UIImage imageNamed:#"allow.png"].CGImage;
[logInViewController.logInView setLogo:nil];
I also find that latest Facebook SDK can do the job, but I am still getting used to using Parse. So please help me out.
After reading through parse doc, I figured out that AppDelegate.m file should be updated like this for IOS 9:
There's also two code changes you'll need to make. First, add the
following to your application:didFinishLaunchingWithOptions: method,
after you've initialized the Parse SDK.
// AppDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <ParseFacebookUtilsV4/PFFacebookUtils.h>
#implementation AppDelegate
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Parse setApplicationId:#"parseAppId" clientKey:#"parseClientKey"];
[PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions];
}
Next, add the following handlers in your app delegate.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
There are two main ways to use Facebook with your Parse users: (1) to
log in (or sign up) as a Facebook user and creating a PFUser, or (2)
linking Facebook to an existing PFUser.
I've created an iphone app with push notification using Azure notification hub. But I'm having a hard time getting the push to work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/******** code for Push notification **********/
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"Entered appDelegate didFinishLaunchingWithOptions");
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString:#"<my azure listening connection string>" notificationHubPath:#"myhub"];
NSLog(#" DeviceToken: %#",deviceToken);
[hub registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError* error) {
if (error != nil) {
NSLog(#"Error registering for notifications: %#", error);
}
else {
[self MessageBox:#"Registration Status" message:#"Registered"];
}
}];
}
-(void)MessageBox:(NSString *)title message:(NSString *)messageText {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:messageText delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
NSLog(#"%#", userInfo);
[self MessageBox:#"Notification" message:[[userInfo objectForKey:#"aps"] valueForKey:#"alert"]];
}
I've followed the steps form this document:
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-ios-get-started/
I've created an explicit app id on the Member center and it matches my bundle id. I'm using developer cert and chose "sandbox" under Azure.
When I first launched my app on to my iphone (not simulator) I get the prompt to accept push notifications - I chose yes. Then my code gets a token from apple and registers that with Azure NH, which is successful.
From Azure NH debug tab, I did a broadcast send which was successfully sent from Azure to APNS. But I never received any notifications. Here is the body of the push notification message on Azure:
{"aps":{"alert":"Notification Hub test notification"}}
I have tried this multiple times and even created an isolated project just to test push notification (separate certs, provisioning profile etc) and it still does not work.
When I intentionally malform the payload of the message for Push, I do get an error as expected So it seems that the connection from NH to APNS is good.
My requirement is to send around 100 million pushes a month.
If anyone has used Azure notification for push notifications then please advice how to fix this?
If anyone has had poor experience with Azure NH then please share what you ended up migrating to.
Thanks in advance.
I was able to get this to work.
I deleted all certificates on Apple developer center and my mac. I recreated the certificates and then followed the process again. It all worked out this time.
Thanks.
how can store apns(apple push notification service) message on my local variable and i need to display the message on my alert view.
any idea for apns(push notification) registration with out using application delegate?
i am using this method on application delegate for register apns
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
i need some other way to register apns. help me please....
What you want is just do something not in the application delegate.
You can declare an interface in your handler object(e.g. your view controller), or you can post a notification, and handle that notification yourself.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[NSNotificationCenter defaultCenter] postNotificationName:YourOwnNotificationName object:deviceToken];
}
in your - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)note you can do the similar thing.
How do I make the interface for an application that has 'Application is agent (UIElement)' set to yes reappear?
The interface shows up the first time I start the app, but if I close the window, and the click on the app's icon then nothing happens. I guess that it's because OS X is trying to start the app again, and there is some mechanism preventing that. What I would like is this:
The first click on the app's icon should launch the app and show the interface.
If the interface has been closed down (but the app is still running in the background) a subsequent click on the icon should just show the interface.
If the interface is already shown a click on the icon should simply move the window to the foreground.
Here is a way you can do it:
1) add + initialize method to your app delegate
+ (void)initialize
{
// check if there is a running instance of your app
NSArray * apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
if ([apps count] > 1)
{
//post notification to it to update inteface
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:#"updateInterface" object:nil];
//quit current instance of the app, coz you don't need two apps running continiously
exit(0);
}
}
2) Register your app for the notification
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(updateInterface:) name:#"updateInterface" object:nil];
}
3) Add updateInterface method
- (void)updateInterface:(NSNotification *)aNotification
{
// handle your interface here
// ....
// move your app forward
[NSApp activateIgnoringOtherApps:YES];
}
I found the answer here: Closing Mac application (clicking red cross on top) and reopening by clicking dock icon.
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
hasVisibleWindows:(BOOL)flag
{
[self.window makeKeyAndOrderFront:self];
return YES;
}
I have a simple application, not document-based. I want to have a login window that allows people to login or add a user, and when they logged in successfully I want it to load the main page. If from the main page you click log out, it should destroy the main page and take you back to login page.
sounds like a simple plan, but for some reason I have a problem.
The way I have it right now, I check if the customer logged in or not in the main file AppDelegate and load different window controller. When customer logs in, I send a notification back to AppDelegate from Login Conntroller and load another window controller for main window.
Something like this:
if([[settings get:#"isLoggedIn"] isEqualToString:#"Yes"])
{
MainController *tmpMainController = [[MainController alloc] initWithWindowNibName:#"MainWindow"];
self.mainController = tmpMainController;
NSWindow *mainWindow = [tmpMainController window];
[mainWindow makeKeyAndOrderFront:self];
[tmpMainController release];
} else {
LoginController *tmpViewController = [[LoginController alloc] initWithWindowNibName:#"LoginWindow"];
self.loginController = tmpViewController;
loginWindow = [tmpViewController window];
[loginWindow makeKeyAndOrderFront:self];
[tmpViewController release];
}
Everything works fine, it displays the correct window. But the weird part happens when I log out from the main page, log in again and log out again. If I do it several times, instead of showing me 1 login window, it draws 2. If I continue the login process, on the second try I get 2 main windows. If I log out again, I see 4 cascade login windows, then I see 5 or 7 main windows. After all windows gets loaded all extra windows start getting destroyed one-by-one. It looks like when new window gets created it draws all old windows, then the new one and then destroys all old ones. I don't know why it happens. Would like some help.
Here is the code from my main controller when customer clicks log out:
-(IBAction)logOutClick:(id) sender
{
[settings set:#"isLoggedIn" value:#"No"];
[[self window] orderOut:self];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NSUserLoggedOutNotification" object: self userInfo: nil];
}
the same thing for login controller:
if ([users verifyUser]) {
[settings set:#"isLoggedIn" value:#"Yes"];
[loginView removeFromSuperview];
[[self window] orderOut:self];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NSUserLoggedInNotification" object: self userInfo: nil];
}
I have "Released when closed" checked off for both windows.
I added new nsnotification center observer every time I log out.
That was the problem.