0

I have tried so many tutorials for sending Push notification service for my app.When i'm testing it on device it works. but if i'm testing it on live after my app launches on App store and i installed it on my device by downloading from store and i run the php file for sending the Push Notification i'm not getting it.Here is the code which i used for Push notification and the tutorial i learnt from .

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];




return YES;
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}

Is there any tutorial which can teach me to have it on live.I changed the environment of my php file from sandbox to live.Guidance please.

Irfan
  • 4,301
  • 6
  • 29
  • 46
Vishnu
  • 2,243
  • 2
  • 21
  • 44

2 Answers2

2

I solved this on my own.

The reason for not working it on Live is because of the

(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

in this method the device token not id properly registered with my php server database. The following code will help to register the device token in php server

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);

NSString* newToken = [deviceToken description];

newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"%@",newToken);
NSString *urlString = [NSString stringWithFormat:@"http://sample.com/registerDevice.php?appId=xxx&deviceToken=%@",newToken];

NSURL *url = [[NSURL alloc] initWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(@"data send");

}
Vishnu
  • 2,243
  • 2
  • 21
  • 44
1

First, I don't think installing an application with a development push notification certificate (or ad-hoc certificate), getting the deviceToken, installing the app store application and sending the push notification using the previous device token will work.

This link confirms it : iPhone APNS Device Tokens in sandbox vs. production

This probably explains why you cannot send a push notification to your application.

Also, you need to send the device token to your server, cause it's the only way for your server to know all the device tokens.

I advise you to review the apple documentation.

Community
  • 1
  • 1
Xval
  • 938
  • 5
  • 17