0

I am currently working on a transit web app to be used with San Diego's Metropolitan Transit System. For iOS, I was planning on using a TabBar with three items, [Home] [Favorites] [Notifications]. [Home] leads to the main page (and to any other pages), [Favorites] is where you can setup your favorite bus/trolley stops.

I was planning on having [Favorites] be where you can setup a notification to be sent to you before a bus arrives at the scheduled times provided by MTS. So for example, if a bus were to come at 10:30 and you set a notification to arrive 5 minutes before to alert you, you would get one at 10:25.

I was wanting to link the accounts with the device tokens, and I read some of the questions on here such as Linking user account to device token for push notifications.

I believe it is possible to somehow link the accounts by sending the device token to the UIWebView when I login.

Hope someone can help me.

EDIT: Below is the Obj-C code in Home.m

#import "Home.h"

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *URL1 = [NSURL URLWithString:@"url"];

    NSURLRequest *Request1 = [NSURLRequest requestWithURL:URL1];

    [webView1 loadRequest:Request1];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"1");
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *deviceToken = [defaults objectForKey:@"deviceToken"];
    bool tokenIsSent = [defaults boolForKey:@"tokenIsSent"];
    NSString *newToken = [defaults stringForKey:@"newToken"];
    NSLog(@"2");
    NSString *urlString = [NSString stringWithFormat:@"url"];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSData *usernameData;
    NSURLResponse *response;
    usernameData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
    NSString *username = [[NSString alloc] initWithData:usernameData encoding:NSUTF8StringEncoding];
    NSLog(@"3");

    if (deviceToken != nil && tokenIsSent == NO) {
        NSLog(@"4");
        NSString *urlString = [NSString stringWithFormat:@"url/s=%@&u=%@",newToken, username];
        NSURL *url = [[NSURL alloc] initWithString:urlString];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        NSData *urlData;
        NSURLResponse *response;
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
        NSString *info = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
        if([info  isEqual: @"success"]){
            [defaults setBool:YES forKey:@"tokenIsSent"];
            NSLog(@"5");
        }else{
            [defaults setBool:NO forKey:@"tokenIsSent"];
            NSLog(@"6");
        }
        [defaults synchronize];
    } else {
        NSLog(@"7");
    }

}
Community
  • 1
  • 1
alexpja
  • 556
  • 2
  • 5
  • 20

3 Answers3

2

First save the token to the NSUserDefaults

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:deviceToken forKey:@"deviceToken"];
    [defaults setBool:NO forKey:@"tokenIsSent"];
    [defaults synchronize];
}

Because this is done in appDelegate, at this point the user is not logged in yet, that's why you need to track using something another value like in the example above tokenIsSent and set it to NO (false).

Now once the user authenticates, you can then send it to your sever and associated with that user.

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
bool deviceToken = [defaults boolForKey:@"deviceToken"];
bool tokenIsSent = [defaults boolForKey:@"tokenIsSent"];

if (deviceToken != nil && tokenIsSent) {
 // it is not null , and it was not sent to PHP so send it
        if(success){
            [defaults setBool:YES forKey:@"tokenIsSent"];
        }else{
            [defaults setBool:NO forKey:@"tokenIsSent"];
        }
        [defaults synchronize];
}

If the delgate is not being called you need to implement it:

@interface HomeViewController : UIViewController<UIWebViewDelegate>

And make sure you assign that delegate.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Assign the delegate to WebView1
    WebView1.delegate = self

    //Perform the request
    NSURL *URL1 = [NSURL URLWithString:@"http://clovercloud.net/app/f/"];
    NSURLRequest *Request1 = [NSURLRequest requestWithURL:URL1];
    [webView1 loadRequest:Request1];

}
meda
  • 45,103
  • 14
  • 92
  • 122
  • If you look at the comments I made in FreeGor's answer, do you think combining this answer and `NSURLRequest` that I can successfully have the account linked to the device? – alexpja Mar 24 '14 at 15:53
  • @alexpja when the device registers to push services, you don't know which account is associated with that device yet, so save it first than perform the `NSURLRequest` once user is authenticated – meda Mar 24 '14 at 15:56
  • I receive `'Use of undeclared identifier 'tokenIsSent'` in the if-then statement – alexpja Mar 24 '14 at 16:02
  • @alexpja oh I assumed you would retrieve those values, check my updated answer. `success` is just a boolean to make sure the request is successful – meda Mar 24 '14 at 16:05
  • I am inserting this into my `webView1.m`, correct? `- (void)webViewDidFinishLoad:(UIWebView *)webView1` is not working and if I place this into my `- (void)viewDidLoad`, it just does it automatically – alexpja Mar 24 '14 at 18:15
  • I got it to work, just not loading where I want it to load. That's my problem now – alexpja Mar 24 '14 at 18:17
  • I am not sure why you are using a webview, what is the use of the webview, can you clarify? – meda Mar 24 '14 at 18:38
  • It's a web app wrapped for iOS and support for APN. – alexpja Mar 24 '14 at 18:53
  • @alexpja please update your post with that part of the code, I see in the comments you tried to pass it as a query string.. It did not work for you? also remove the `1` from `- (void)webViewDidFinishLoad:(UIWebView *)webView1` – meda Mar 24 '14 at 19:12
  • Ok we will sort this out, tell me whats not working, you get any log? does the method get called, you create a new value `newToken` ? – meda Mar 24 '14 at 20:01
  • I get the `username` from session data, I get `newToken`, I do not get any output. I think `- (void)webViewDidFinishLoad:(UIWebView *)webView` is not loading. When I place the code in `- (void)viewDidLoad`, I successfully have it sent to the server. ` NSString *urlString = [NSString stringWithFormat:@"http://pja.clovercloud.net/SDTR/push/register.php?s=%@&u=%@",newToken, username];` becomes "success? [token] alexpja" as a notification on my phone. – alexpja Mar 24 '14 at 20:10
  • I placed `@interface HomeViewController : UIViewController` and it asks if I'd like to enter `@end` after `@implementation HomeController`. Where should your code (the big stuff) go, under the `@interface` or `@implementation`? – alexpja Mar 24 '14 at 20:15
  • @alexpja `@interface HomeViewController : UIViewController` should be in the interface the .h file. the code always go to .m file because you program to an implementation not an interface – meda Mar 24 '14 at 20:21
  • OK, did that. Still does not load `- (void)webViewDidFinishLoad:(UIWebView *)webView` Updated code. – alexpja Mar 24 '14 at 20:30
  • @alexpja the logs dont show in the console ? `webViewDidFinishLoad` is not being called ? – meda Mar 24 '14 at 20:36
  • It is not being called. – alexpja Mar 24 '14 at 20:39
0

The registration for push notification is done through a native app and can only be performed through a native app. For more details see answer:

How can a web application send push notifications to iOS devices?

Push notification is not associated with some kind of View.

Community
  • 1
  • 1
FreeGor
  • 615
  • 13
  • 26
  • Is it not possible to request what is currently set in a session variable such as `$_SESSION['username']` in a UIWebView and then send the username and token to a PHP script in native code such as `registertoken.php?u=USERNAME&token=TOKEN`? – alexpja Mar 24 '14 at 15:28
  • @alexpja you should get device token from APNS and then you can send it from any place what you want and it's not related with PHP. See documentation: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html – FreeGor Mar 24 '14 at 15:38
  • I currently get the device token from the device itself and at `didRegisterForRemoteNotificationsWithDeviceToken` I make a `NSURLRequest` to send it to my PHP script. To verify that it works I linked it to the script I'm using to send notifications and with a GET variable `s`. Ran the app from Xcode onto my iPad, my iPhone received a notification with the device token. Not sure what you're saying. – alexpja Mar 24 '14 at 15:41
  • If user not have userID or userName I use UUID with iCloud storage for identification token. And after user login I'm updating this row in database. – FreeGor Mar 24 '14 at 15:58
0

You can store the device token in NSUserDefaults. Then you can access it from where ever you need it within your iOS app. Therefore you can pass is to the UIWebView when you login and link the logged in user with the device token.

Eran
  • 387,369
  • 54
  • 702
  • 768