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.
Related
I am using push notification in my iOS app.
I cant able to get the push notification message from the server. But i can get the message when using third party server APN Tester free.
In my app i am using backend as .net webservice (i.e asmx file). Below are the code i am using for push notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"didFinishLaunchingWithOptions");
return YES;
}
/-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString *deviceTokenString = [NSString stringWithFormat:#"%#", deviceToken];
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken");
NSLog(#"My token is: %#", deviceTokenString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:deviceTokenString
message:#"devicetoken"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
/
}*/
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
_token_data =deviceToken;
NSString *title = [NSString stringWithFormat:#"%#",_token_data];
NSString *trimmedString = [title stringByReplacingOccurrencesOfString: #" " withString:#""];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: #"<" withString:#""];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: #">" withString:#""];
_tokenkey = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
NSString *soapFormat = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http:/
"<soap:Body>\n"
"<AddAppleDeviceToken xmlns=\"http:/
"<PortalId>0</PortalId>\n"
"<ModuleId>12</ModuleId>\n"
"<strDeviceToken>sdfssdfs</strDeviceToken>\n"
"</AddAppleDeviceToken>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSURL *locationOfWebService = [NSURL URLWithString:#"http://www.mywebsite.com/push.asmx"
wsDeviceToken.asmx"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:#"%lu",(unsigned long)[soapFormat length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http:/
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
/
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
NSLog(#"soapFormat %#",soapFormat);
if (connect) {
_webData = [[NSMutableData alloc]init];
NSLog(#"Connection Establish");
}
else {
NSLog(#"No Connection established");
}
NSString * test = [NSString stringWithFormat:#"Your Device token is %#", trimmedString];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test
message:#"devicetoken"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types != UIUserNotificationTypeNone) {
NSLog(#"didRegisterUser");
[application registerForRemoteNotifications];
UIAlertView *alert33 = [[UIAlertView alloc] initWithTitle:#"didRegisterUser"
message:#"didRegisterUser"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert33 show];
}
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/
/
NSString *message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
/
}
#pragma - NSURLConnection delegate method
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webData setLength: 0];
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
NSLog(#"HTTP error %zd", (ssize_t) httpResponse.statusCode);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{ NSLog(#"DONE. Received Bytes: %lu", (unsigned long)[_webData length]);
self.xmlParser = [[NSXMLParser alloc]initWithData:_webData];
[self.xmlParser setDelegate: self];
[self.xmlParser parse];
}
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"AddAppleDeviceTokenResult"]) {
NSString *tmpstr = [[NSString alloc] init];
tmpstr = [tmpstr stringByAppendingString:soapResults];
[soapResults setString:#""];
NSLog(#"AddAppleDeviceTokenResult %#",tmpstr);
if([tmpstr isEqualToString:#"DeviceToken Added in Database"])
{
NSString * test = [NSString stringWithFormat:#"Your Device token is added in database"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test
message:_tokenkey
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
NSLog(#"Token added successfully");
[[NSUserDefaults standardUserDefaults]setValue:#"1" forKey:#"isDeviceTokenAdded"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (!soapResults) {
/
soapResults = [[NSMutableString alloc] init];
NSLog(#"soapResults %#",soapResults);
}
if([string length] > 0){
NSString *cleanString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([cleanString length] >0){
[soapResults appendString: string];
}
}
}
Any suggessions Please.
Thanks in advance.
jejai Replace This Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
TO
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSString *message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
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
My application used notify of APNS. When server send a notify to client with a link. I click to notify on notification bar, application will open link in notify on webview. My problem is, when application run active or Background, it run normal and load link OK. But when application don't active, i click to notify, it will don't load link in notify, it only load old link in NSUserDefaults or link "http://staging.nhomxe.vn". This is my code:
APPDELEGATE.m
- (void)application:(UIApplication*)application
didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
NSDictionary *data = [ userInfo objectForKey:#"aps"];
for(NSString *key in data) {
NSString *info = [data objectForKey:key];
NSLog(#"thong tin nhan dc: %# : %#", key, info);
}
NSString *message = [userInfo valueForKey:#"link"] ;
//NSArray *info = [message componentsSeparatedByString:#"&#"];
//NSString *body = [info objectAtIndex:0];
//NSString *link = [info objectAtIndex:1];
NSLog(#"Thong tin Link: %#",message);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:message forKey:#"LINK"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Warning"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alertView show];
ViewController *vc = (ViewController *)self.window.rootViewController;
NSURL *url = [NSURL URLWithString:message];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[vc.webView loadRequest:urlRequest];
[vc.webView3 loadRequest:urlRequest];
}
MYVIEWCONTROLLER.m
- (void)viewDidLoad
{ NSString *link = NULL;
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
link = [data objectForKey:#"LINK"];
NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://staging.nhomxe.vn"] encoding:NSUTF8StringEncoding error:nil];
if(connect == NULL)
{
NSLog(#"Server hiện tại đang bảo trì. Ứng dụng sẽ đóng ngay bây giờ.!");
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Warning"
message:#"Server hiện tại đang bảo trì. Ứng dụng sẽ đóng ngay bây giờ."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
alert.tag = 1;
[alert show];
}else
{ if(link == NULL)
{
NSString *linkWeb = #"http://staging.nhomxe.vn";
NSURL *url = [NSURL URLWithString:linkWeb];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
[self.webView3 loadRequest:urlRequest];
}else{
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
[self.webView3 loadRequest:urlRequest];
//[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"LINK"];
//[[NSUserDefaults standardUserDefaults] synchronize];
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setValue:NULL forKey:#"LINK"];
}
}
// Schedule the runScheduledTask in 5 seconds
aTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(runScheduledTask) userInfo:nil repeats:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
When App is not running in Background
- (void)application:(UIApplication*)application
didReceiveRemoteNotification:
(NSDictionary*)userInfo
Would not be called you Should handle your data something like this..
In your AppDelegate.m within didFinishLaunchingWithOptions method do something like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary* userInfo = [launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary * data = [userInfo objectForKey:#"aps"];
for(NSString *key in data) {
NSString *info = [data objectForKey:key];
NSLog(#"thong tin nhan dc: %# : %#", key, info);
}
//.…COntinue with your Execution So on…. You will get the data in the Data Dictionary which you are looking for
}
I suspect you don't have code to start app from notification.
Take a look at - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. If app starts from notification, you'll have launchOptions.
If you have remote notification, you'll have UIApplicationLaunchOptionsRemoteNotificationKey key in launchOptions.
UIApplicationLaunchOptionsRemoteNotificationKey
The presence of this key indicates that a remote notification is available for the
app to process. The value of this key is an
NSDictionary containing the payload of the remote notification. See
the description of application:didReceiveRemoteNotification: for
further information about handling remote notifications.
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 :)
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];
}