Network push notification - iphone

I am wondering if there is a simple way to push a notification, to all your users currently using your application.
So the next time they launch the application and they are connected to Wi-Fi, they receive a alert telling them that, i.e., "An update is available".
EDIT:
To explain in greater detail what I am looking for. I am developing an application that should only be used when it is the most recent version of the software, so I would like to be able to send out a message (notification) for when an update is available in the AppStore. This is seen in some games such as AngryBirds and Cut the Rope.
Maybe even change a BOOL in the code to TRUE, leaving a red flag if{} BOOL is TRUE. (Of course first I'd like an answer to the more basic version. This would be helpful though as well)
Hope this clears things up.
With regards,
SirKaydian

Local Notification Solution
Essentially what Oscar said previously, you could make this really simple. If you wanted to check after every launch of the application you could easily call some sort of local API (or similar) call from your application to a web service you might host. So for example you have a PHP file:
<?php
$currentVersion = 1.3;
echo $currentVersion;
?>
That simple PHP script can be updated by you whenever you release an update. The iPhone can recognize this by querying that PHP file on every startup (check NSURLConnection delegate methods to get the response from any particular URL). Now from previous checks by your application it can store those in an NSUserDefaults value by the following:
NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
NSString *loadedVersion = [defaults valueForKey:#"MYAPP_CURR_VERSION"];
Now we need to compare the two from your NSURLConnection delegate method that receives the string back from the PHP file on your web service.
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.website.com/folder/version.php"]];
[urlRequest setHTTPMethod:#"GET"];
[urlRequest setHTTPBody:[postParams dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
[connection start];
The above code will send the request to your server URL that you tell it to. Now when we get the response we'll do it like this:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *responseVersionFromServer = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(![loadedVersion isEqualToString:responseVersionFromServer]) {
//execute code here to show a local notification or UIAlertView
}
}
That will do all the comparison for you.
NOTE: You must add NSURLConnectionDelegate in your .h Header file!
Push Notifications Solution
There are services out there for allowing your application to have push notifications such as Urban Airship that you can log into and send a push notification to all of your users when an update becomes available. It's really simple to use, check out their website. (www.urbanairship.com)

You could consume a webservice and compare versions of your app (maybe save the version to NSUserDefaults), I'm sure there are other solutions but this one comes to mind. You could show an alert that links to the app store if the versions are different.

Related

Checking if user enabled push notification on iPhone using phonegap 1.7

I'm trying to determine whether or not the user enabled push notification for my app using phonegap (ver 1.7).
So far I only came up with this plugin which doesn't have a method specifically to check that issue, but does have a method to register push notification, which may return an error, which may be an indication to the user disabling push notification. But as you can see, that's very vague.
So my question is - is there a plugin (or any other way) to determine if the user enabled push notification for my app?
You can check if any type of push notifications are enabled by using this :
if([UIApplication sharedApplication].enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)
this status can be accesd by call a simple plugin after device ready.
UPDATE :Write a plugin and inside that plugin please check the status and return to javascript
if you need to save it on server first thing you need one server side api and some native side code, after registering push notification on ios there are two callback functions
didFailToRegisterForRemoteNotificationsWithError
didRegisterForRemoteNotificationsWithDeviceToken
so if success you can send a request to your server with the device id or any unique ID so you can save the registered devices.
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:#"< "withString:#""] stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
[self postUpdateRequest:token]; // request to your server
}
for sending POST request use
NSURL *aUrl = [NSURL URLWithString:uRI];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *postString =#"Your Data";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
if(connection) {
// success
} else {
//error
}
if registration was failed (didFailToRegisterForRemoteNotificationsWithError) you can send a requset to server with error data OR Not.
from phonegap side after device ready send one ajax request to you server and check whether your device is registerted or not. for getting unique id you may use simple plugin. for writing simple plugin please follow this http://docs.phonegap.com/en/edge/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide
Unfortunately you can't enable or disable the Push Notifications for your app from the app code. check this

Response for Registering on Wordpress Site through iPhone

I am writing an app that displays content from a Wordpress Site, and also allows reading of comments as well as posting comments. I am handling logging in to leave a comment and posting a comment via XML-RPC. All that is working quite well. However, this particular site does not allow anonymous commenting. So, I need to allow Registering for an account through the app.
Currently, I take the desired "username" and "email" and submit via POST as follows:
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.lamebook.com/wp-signup.php"]];
[request setPostValue:#"example" forKey:#"user_name"];
[request setPostValue:#"example#test.com" forKey:#"user_test"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(registerFinished:)];
[request setDidFailSelector:#selector(registerFailed:)];
[request startAsynchronous];
This works in that it will create the account. However, my issue is that in my registerFinished method:
- (void)registerFinished:(ASIFormDataRequest *)request {
NSString *response = [[NSString alloc] initWithData:[request responseData] encoding:NSASCIIStringEncoding];
NSLog(#"response %#", response);
}
The response is simply the HTML of the registration page. The HTML contains no information about the success or failure of the registration.
When using the webform the returned HTML has entries if any error occurred, for example:
<p class="error">Username must be at least 4 characters</p>
However, I do not seem to get these elements in the HTML I receive on the phone. Is there a proper way to do registration on the phone?
If you have access to the site, which I guess you do, you should be able to write a small plugin that let's you perform the registration by posting data to an URL specified by your plugin. This would be fairly simple, just hook up a function to the init action and check for the $_POST variable for any input.
Then simply use username_exists to check for existing users and wp_create_user to perform the registration. These functions will give return values that you in turn can send as a JSON reponse (or whatever is appropriate) back to you application.
In fact, my experience with XML-RPC is that it's somewhat limited, and not really up to date with the rest of WordPress, so I often make these little mini API's to handle situations like this. All that might have changed in the latest releases, however.

Newsletter and registration on iphone

I'd like to know if it was possible, if a user wishes to subscribe to updates of my applications, take a form that is automatically subscribed to this newsletter at this address http://www.gseo.it/lists/?p=subscribe&id=2 (this is my mailing list with double opt in) but I'd like to know that a user can subscibe this newsletter directly from my iphone app.
Thanks
You could do an HTTP POST to that form using ASIFormDataRequest.
This isn't working code, but it might look something like:
NSURL *url = [NSURL URLWithString:#"http://www.gseo.it/lists/?p=subscribe&id=2"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"someone#example.com" forKey:#"email"];
[request startSynchronous];
You can get the library here.
Yes of course you can, open up a UIWebview with the the url provided. Don't forget that this may look not good in the iphone browser so providing a custom html code depending on the user agent may improve things.

What's the simplest way to check the current version of your app against the latest version in the app store?

If the iOS SDK doesn't have functionality for this, then what if I have a basic (static) website, and somewhere on that website I manually set a piece of data that specifies the latest version of my app in the app store every time I release an update? How can I make my app query the website for that version data and check it against the version running on the iOS device?
You are on the right track. You need to make an HTTP request to your static version web page. To do this you can use an NSURLConnection object. So something like:
NSURL * url = [NSURL URLWithString:#"http://yourweb.com/version.txt"];
NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Then in your delegate implementation:
(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
{
if(response.statusCode != 200)
// you got an error
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
// again with the errors ...
}
// you got some data ... append it to your chunk
// in your case all the data should come back in one callback
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[mData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// your request finished ... check the version here
}
So in your connectionDidFinishLoading you have a look at mData that you have collected. Parse out the version number and compare it to your bundle version number:
[self infoValueForKey:#"CFBundleVersion"];
You can make a query like http://itunes.apple.com/en/lookup?bundleId=com.easi6.doorsndots to AppStore.
Returning JSON has an version information (currently on AppStore) which can be compared the other in bundle.
I had used the same solution #RedBlueThing recommended in many of my apps. I have scaled it to a service that other app developers can use at CleverStork - an Update Manager for Apps. Hope you guys like it :)

Tracking iPhone on Yahoo Web Analytics using ASIHTTPRequest

I'm trying to track an event in my app using Yahoo Web Analytics. The code I am using looks like
ASIHTTPRequest *yahooTrack = [ASIHTTPRequest requestWithURL:
[NSURL URLWithString:#"http://s.analytics.yahoo.com/p.pl?a=xxxxxxxxxxxxx&js=no&b=yyyyyyyyyyyy&cf6=zzzzzzzzzzz"]];
yahooTrack.didFinishSelector = #selector(statisticsFinished:);
yahooTrack.delegate = self;
[yahooTrack startAsynchronous];
Then the statisticsFinished looks like:
NSLog(#"Cookies: %#", request.requestCookies);
NSLog(#"Redircount: %d", [request redirectCount]);
NSLog(#"Responsecode %d %#\nMsg: %#", request.responseStatusCode,
request.responseStatusMessage, [request responseString]);
And all the information I get back looks correct. Cookies are set, redirectcount is 1 the first time (as it redirects to s.analytics.yahoo.com/itr.pl?.... a normal browser does). Then the redirectcount is 0 for subsequent request until the app is restarted and session cleared. The responseString returns GIF89a.
Even if the data looks correct, Yahoo still won't track. As soon as I call the tracking url directly in my browser it works as expected.
I realize Flurry is a better option, but I'm forced to use Yahoo in this case. Also, using a UIWebView probably would work, but I'm against putting in a webview just for tracking purposes.
Is there any difference in how ASIHTTPRequest and Safari would handle a call to a simple URL as this? Or do you see anything else that could explain why the tracking isn't working?
I finally found the problem. ASIHTTPRequest creates a user-agent based on your applications name, and requests from this user agent is ignored by Yahoo somehow (bug?). As stated in the documentation, you can override the user-agent as follows:
[request addRequestHeader:#"User-Agent" value:#"My-User-Agent-1.0"];
I used the user-agent string of Safari on iPhone, and it worked immediately! BTW; the same problem applies for Android, and the same fix works.