In my application i have added notification by storing the device id in server and using php i sending notification the problem is now device token is not storing in the server previously it was working f9 but not its not working.
Previously I was using different account now I'm accessing different account after setting the new account certification in my app is not working please tell where I'm wrong.
Notification code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(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]];
}
NSString *urlString = [NSString stringWithFormat:#"url?token=%#",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSLog(#"token %#",urlString);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSLog(#"request %# ",urlRequest);
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(#"data %#",urlData);
[self clearNotifications];
}
The above same code was working fine previously now its not working I'm not able to find solution why its not storing after i changing to new account please tell me how to resolve.
Thanks.
Just for other people reading this question. We have narrowed down the issue by implementing
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
After that it turns out that the app was signed with wrong provisioning profile. Simple things like that are getting missed :)
Related
I made my app to register iPhone's device_token when app launch for the first time after installed. But sometimes that happens fail.
but app needs device_token for operating ordinarily.
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString * useruuId = [AppDelegate getUUID];
NSString * token = [NSString stringWithFormat:#"%#", deviceToken];
token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSString * postBody = [NSString stringWithFormat:#"user=%#&token=%#",useruuId,token];
NSString * endpoint = [NSString stringWithFormat: #"%#device_register.php",APNSPATH ];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:endpoint] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kRequestTimeInterval];
request.HTTPMethod = #"POST";
request.HTTPBody = [postBody dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSError * error = nil;
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//NSLog(#"statusCode(device_register.php):%ld",response.statusCode);
if(response.statusCode== 200)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kRegisteredID];
[[NSUserDefaults standardUserDefaults] synchronize];
break;
}
});
}
I know [didRegisterForRemoteNotificationsWithDeviceToken] method is called only once operating after app installed.
So If I fail to register device_token to Server in didRegisterForRemoteNotificationsWithDeviceToken , I can't get device_token by next time. so I can't register token in server.
how i get device_token when failed ?
Additionally , is there a case app fail to get device_token from apple's server?
If it is, how i get device_token ?
At first you have to enable push notification in you provisional profile. Then you have to use following code for getting device token.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *yourDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#"<>"]];
yourDeviceToken = [yourDeviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"your device token: %#", yourDeviceToken);
}
Lets try using this
i am developing a app and whenever my app is installed on a iDevice, i must register that device, in the server through a post Web service, how to do that, This is what so far i have done:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
//[application registerForRemoteNotificationTypes:
// (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
return YES;
}
and then i wrote the following,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
[self registerDeviceTokenWithServer:devToken];
}
Next this method
-(void)registerDeviceTokenWithServer :(NSString*)deviceToken{
[NSThread detachNewThreadSelector:#selector(registerDeviceInBackground:)
toTarget:self withObject:deviceToken];
}
and now i need to register the mobile through the device token in the following method through a post call Web service, how to do that,
-(void)registerDeviceInBackground :(NSString*)deviceToken
{
I need to write the code here a post call method.
}
If any code help is there, is well appreciated.
You can write this:
-(void)registerDeviceTokenWithServer :(NSString*)deviceToken{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *s = [NSURL URLWithString: #"www.apple.com/yoururl"];
NSMutableURLRequest *requestURL = [NSMutableURLRequest requestWithURL:s cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.00];
[requestURL setHTTPMethod:#"POST"];
NSError *error = [[NSError alloc] init];
if ([parameter isKindOfClass : [NSString class]]) {
[requestURL setHTTPBody:[devToken dataUsingEncoding:NSUTF8StringEncoding]];
}
NSHTTPURLResponse *response;
NSError *error1;
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error1];
dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
if ([[dictionaryData objectForKey:#"status"] isEqualToString:#"success"]) {
//Post successful
}
else if([[apiDataBack objectForKey:#"status"] isEqualToString:#"error"]){
//error
}
});
});
}
Above code will post the device token asynchronously.
Hope this helps.. :)
Hi in my application i have the push notification when user tap the notification its going to the main view controller i to view a particular view controller by tapping on the notification i have tried some methods its not working for me so please tell to make it done.
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(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]];
}
NSString *urlString = [NSString stringWithFormat:#"url?token=%#",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSLog(#"token %#",urlString);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSLog(#"request %# ",urlRequest);
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(#"data %#",urlData);
[self clearNotifications];
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
//My_specificViewController
updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"updatepoliticalViewController"];
[self.window setRootViewController:ringingVC];
}
Tthe above i have tried its not working for please tell where I'm doing worng what is the correct way to make it done.
Thanks.
Try like this
Dont set as rootviewcontroller
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"updatepoliticalViewController"];
[self.navigationController pushViewController:ringingVC animated:YES];
}
i am working on one application in which server will send me image in base64 as a push notification format as and from ios side i have to display this image with decode of base 64?I can do decoding and all but how i will display PNS as image
Is it possible to accept PNS as image(in base64)?
Code i am using as below,
#pragma mark -
#pragma mark - Push Notifications Methods
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *tokenStr = [deviceToken description];
// Separete Your device token with <,< and blanksapace
NSString *pushToken = [[[tokenStr
stringByReplacingOccurrencesOfString:#"<" withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString:#" " withString:#""];
// Save the token to server
NSString *urlStr = [NSString stringWithFormat:#"http://www.vijaywebsolutions.com/Development_FTP/webservice/webservices.php?deviceToken=%#",pushToken]; // Passing token to URL
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self]; // Support to perform URLrequest
if( theConnection )// checking connection successfull or not
{
webData = [NSMutableData data];
NSLog(#"device token is %#", pushToken);
}
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification" message:[NSString stringWithFormat:#"You Have a Notification :\n%#",userInfo[#"aps"][#"alert"]]delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
You can't. The notification to the user will present the title and message text and then, if the user triggers the notification to open the app, your app will be passed the full notification data and you can display the image.
I have both certificates created and the APP in the portal has APNS enabled for both development and production.
In development it works fine, but in production when someone downloads the app and signs in my server is updated with NAPN which stands for No Push Notifications.
This is the section of code it goes to if the user has them disabled, but I have checked with multiple users and they are completely enabled but for some reason its like it is stuck with the development APNS certificate or something, also users are not prompted to enable push notifications when they first download and launch the app.
I don't know if I need to approach apple with this questions but if anyone can, please assist with recommendations on how to get this to work that would be greatly appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//Clear notifications upon launching
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
//Register with APN
if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != 0)
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}else
{
URLSingleton *registrar = [URLSingleton sharedInstance];
[registrar setDevToken:#"NAPN"];
//Register APN with iShame Service
URLSingleton *urls = [URLSingleton sharedInstance];
NSString *authString = [NSString stringWithFormat:#"{\"user\":\"%#\",\"token\":\"%#\"}", [[NSUserDefaults standardUserDefaults] valueForKey:#"session_token"], [registrar getDevToken]];
NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSMutableURLRequest *url = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urls.updateAuth]];
[url setHTTPMethod:#"POST"];
[url setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[url setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[url setValue:#"json" forHTTPHeaderField:#"Data-Type"];
[url setValue:[NSString stringWithFormat:#"%d", [authData length]] forHTTPHeaderField:#"Content-Length"];
[url setHTTPBody:authData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:url delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[connection start];
}
return YES;
}
You should always register for remote notification types, since that call is what causes the user to be prompted to enable push notifications on the first time they launch the app . Remove that condition. I don't know how this code worked in the sandbox env. My guess is that you added this condition at a later stage, after you already ran your app without the condition, and that's how the registration worked in that env.
When you create the .ipa, instead of building with the distribution, (unless you're putting it on the store) build with the developer license, so that the app will sign it with your signature. I have had issues with apps and that was my problem. Hope this helps!