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];
}
Related
I'm having a strange issue, using Xcode 5 and testing on iOS 7, I'm sending an extra parameter with the apn request named "url", I need to open that URL in the webview when the notification is received, but the app freeze when the notification arrives if it was connected to the USB, or does nothing when is not connected.
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"Did Register for Remote Notifications with Device Token (%#)", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"Did Fail to Register for Remote Notifications");
NSLog(#"%#, %#", error, error.localizedDescription);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Register for Remote Notifications
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(#"Notification received");
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive) {
// app was already in the foreground
NSLog(#"app was already in the foreground");
} else {
// app was just brought from background to foreground
NSLog(#"app was just brought from background to foreground");
}
if ([userInfo objectForKey:#"url"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"loadNotificationUrl" object:self userInfo:userInfo];
}
}
And that's what is in the ViewerController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = #"http://example.com/?action=home";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
webView.delegate = self;
webView.opaque = NO;
webView=[[UIWebView alloc]init];
[webView setBackgroundColor:[UIColor grayColor]];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:webView];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadNotificationUrl:)
name:#"loadNotificationUrl" object:nil];
}
-(void)loadNotificationUrl:(NSNotification*) notification
{
NSDictionary* userInfo = notification.userInfo;
NSString* NotificationUrl = (NSString*)userInfo[#"url"];
NSLog (#"Successfully received: %#", NotificationUrl);
NSURL *url = [NSURL URLWithString:NotificationUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
}
As my little experience, this should load the URL when the notification is clicked, but it is either freeze or does nothing, can anybody tell me where is my mistake please?
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.. :)
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 :)
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.