get device token inside -(BOOL)application:didFinishLaunchingWithOptions: - iphone

I don't know how to get device token (APN) inside
- (BOOL)application:didFinishLaunchingWithOptions:
the only place I can get the device token in AppDelegate is in
- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:
where application:didRegisterForRemoteNotificationsWithDeviceToken will run asynchronous or after didFinishLaunchingWithOptions. So, is there any way to get device token in didFinishLaunchingWithOptions?? Because I want to pass it into my ViewController that pushed from didFinishLaunchingWithOptions.
Here is my sample code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
for (id key in launchOptions) {
NSLog(#"Key: %#, Value %#", key, [launchOptions objectForKey: key]);
}
AddViewController *avc = [[AddViewController alloc] init];
avc.managedObjectContext = self.managedObjectContext;
avc.pushToken = self.pushToken;
UINavigationController *uinav = [[UINavigationController alloc] init ];
[uinav pushViewController:avc animated:YES];
[_window addSubview:uinav.view];
[avc release];
[self.window makeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
self.pushToken = [NSString stringWithFormat:#"%#", deviceToken];
}
Thanks a lot for any help.

application:didRegisterForRemoteNotificationsWithDeviceToken: is your one and only chance to get the current device token. However, you can store the result in NSUserDefaults and read it from there on launch. Most of the time, if the app hasn't been uninstalled/reinstalled, the device token remains the same between launches.

Related

AppDelegate is not listening to APNS Push Notification

When my app enters background, AppDelegate is not listening to the new push notification from APNS. But strange thing is that it doesn't happen all the time. Sometimes it works, sometimes it does not. I can't figure out why.
Here is my sample code for listening APNS Notification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
}
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Received notification background: %#", userInfo);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
// Check result of your operation and call completion block with the result
completionHandler(UIBackgroundFetchResultNewData);
});
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
In background/closed mode,no code will execute and none of the delegate method will be invoked.This time push notification is handled by OS itself.The app badge is set by push notification payload value coming from server side.
If you are not getting app badge in background,it is a badge number issue from server side.Check whether the push notification payload contains application badge field and set to values greater than 0.
This link have same issue and resolved it.
Try to change your line..
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
in iOS 8 you have to use "UIUserNotificationTypeBadge"...
Hope this will work for you..

startMonitoringSignificantLocationChanges if can terminated my app

i am want to use startMonitoringSignificantLocationChanges in my app,but i have some question,i hope get some help:
for a common app if app enter background,after 10mins ,system can kill the app.if i used startMonitoringSignificantLocationChanges ,when i enter background two hours,my app not terminated,because i click the icon,app will enter the last quit page 。if app is killed,when you click the icon you will first see the default page. so my question is use startMonitoringSignificantLocationChanges my app not killed by system after enter background 10 mins?
if i close mobile,and restart the mobile,when significant location change,my app if can be activate ,i look apple develop document it answer yes.so i test:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if([CLLocationManager significantLocationChangeMonitoringAvailable]){
[self log:#"sigAvailable=YES"];
}
// Override point for customizatio-n after application launch.
id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
if (locationValue)
{
// create a new manager and start checking for sig changes
[self log:#"didFinishLaunchingWithOptions location key"];
m_locManager = [[CLLocationManager alloc] init];
[self log:#"didFinishLaunchingWithOptions created manager"];
m_locManager.delegate = self;
[self log:#"didFinishLaunchingWithOptions set delegate"];
[m_locManager startMonitoringSignificantLocationChanges];
[self log:#"didFinishLaunchingWithOptions monitoring sig changes"];
// do send local notification
return YES;
}
[self log:#"didFinishLaunchingWithOptions"];
return YES;
}
my question: when restart mobile and local notification,above code is run and log "didFinishLaunchingWithOptions location key" and so on,if i send local notification at the above code, if user will receive?
in ios 6 there is new feature for maps, which stops location updates
It helps to wake app to recieve location updates.link
locationManager.pausesLocationUpdatesAutomatically
Also see all others.
Your app can start on device boot if its VOIP.
see this apple doc
for local notification add to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// creating local notification
Class cls = NSClassFromString(#"UILocalNotification");
if (cls)
{
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
NSLog(#"notification text:%#",reminderText);
// [viewController._msgTextView setText:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
and you can handle them in
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"application:didReceiveLocalNotification:");
}
you can schedule your local notification in
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = notifyDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = AlertMsg;
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
notif.alertAction = #"Show me";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}

App with playerduel functionality doesn't get push notification

I am developing application which support playerduel framework. In which two players can play with each other. Person can send challenge to another one.
i follow the documentation following. https://docs.urbanairship.com/display/DOCS/Getting+Started%3A+iOS%3A+Push
I can get notification when i send it from command line (for testing) as describe in above documentation. But when i play game. Playerdual can't send notification when some one send challenge to another.
appdelegate code :-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Init Airship launch options
NSMutableDictionary *takeOffOptions = [[[NSMutableDictionary alloc] init] autorelease];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please populate AirshipConfig.plist with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
// Register for notifications
[[UAPush shared]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Override point for customization after application launch.
self.appStarted = YES;
UIImage *bgImage= [UIImage imageNamed:#"default.png"];
[PlayerDuel initializeWithGameKey:#"gamekey" andBackground:bgImage
andDelegate:[navigationController.viewControllers objectAtIndex:0] andOrientation:UIInterfaceOrientationPortrait];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"deviceToken:- %#",deviceToken);
// Updates the device token and registers the token with UA
[[UAPush shared] registerDeviceToken:deviceToken];
[PlayerDuel registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
NSLog(#"remote notification: %#",[userInfo description]);
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);
NSString *itemName = #"my app";
NSString *messageTitle = [apsInfo objectForKey:#"alert"];
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive){
AudioServicesPlaySystemSound(1007);
[self _showAlert:messageTitle withTitle:itemName];
}
else{
UIViewController *viewController = navigationController.visibleViewController;
// NSLog(#"Controller Name:- %#",viewController);
[viewController.view reloadInputViews];
[viewController playerDuelStartGame:nil];
}
NSString *badge = [apsInfo objectForKey:#"badge"];
NSLog(#"Received Push Badge: %#", badge);
}
If the push notifications work directly through Urban Airship and not through PlayerDuel, You probably didn't specify the right urban airship details in PlayerDuel's developers website. Make sure you put Urban Airship's Master Secret and not the App Secret in PlayerDuel's website.
I have used App Secrete as Urban Airship Key: . This is wrong. When i change it value as App Key . It works fine.

how to handle push payload when the app closed?

i am sending push payload to my users with the follow content :
{"aps": {"alert": "Go To Google", "sound": "Default","url":"http://www.google.com"}}
everything goes well when the pp is running but in the background.
if i am receiving the push and the app is closed i am open it and nothing happen.
i am trying to redirect to this url in the payload.
again when the app is running from the background it goes well.
this is the implementation so far AppDelegate.m:
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RedirectedUri = [[userInfo objectForKey:#"aps"] objectForKey:#"url"];
NSLog(#"%#",RedirectedUri);
[self Redirect:RedirectedUri];
}
need some help please.
Additionally, add the following to your code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self applicationDidFinishLaunching:application];
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
RedirectedUri = [[dictionary objectForKey:#"aps"] objectForKey:#"url"];
[self Redirect:RedirectedUri];
}
}
return YES;
}

Get device token to push notify the second time on iPhone

I need to obtain the device token on my iPhone to test the push notify.
On my iPhone I had already agreed to notify push permissions. I try to remove and reinstall the app but nothing. I try to put a breackpoint in the didRegisterForRemoteNotificationsWithDeviceToken method but nothing.
Any suggestion?
This is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/**** PUSH NOTIFY ****/
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSMutableString *str = [[NSMutableString alloc] initWithFormat:#"http://www.mysite.com/storeToken.php?task=register&token=%#", [self stringWithDeviceToken:deviceToken]];
//NSLog(#"%#",str);
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setObject:[self stringWithDeviceToken:deviceToken] forKey:#"token"];
[pref synchronize];
NSURL *url = [NSURL URLWithString:str];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
[str release];
}
- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {
const char* data = [deviceToken bytes];
NSMutableString* token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:#"%02.2hhX", data[i]];
}
return [[token copy] autorelease];
}
This is the error that it print:
Error: Error Domain=NSCocoaErrorDomain Code=3000 "nessuna stringa di autorizzazione 'aps-environment' valida trovata per l'applicazione" UserInfo=0x296e80 {NSLocalizedDescription=nessuna stringa di autorizzazione 'aps-environment' valida trovata per l'applicazione}
It is good to have another delegate method for error handling:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"Fail to register for remote notifications: %#", [error localizedDescription]);
}
After question update it's more clear that problem is in wrong provisioning profile (generic or without 'aps-evironment'). So:
Remove all expired profiles and all profiles for that app from both XCode and device
Check push notifications are enabled for your app (in provisioning portal)
Download provisioning profile from portal, install it to XCode
Check that selected profile (in build settings / codesigning identity) matches the app, and is not the generic/wildcard one (sometimes autoselection goes wrong)
As usual (XCode caching "magic"), it's better to restart XCode and remove the application from device before build
Build & Pray