Showing posts with label [IT] Cordova. Show all posts
Showing posts with label [IT] Cordova. Show all posts

Friday, January 2, 2015

Use Notifications in Cordova

If you want to add notification function in your cordova app local notificaiton plugin (the de.appplant.cordova.plugin.local-notification) is good solution.

https://github.com/katzer/cordova-plugin-local-notifications/

1. Add plugin.
cordova plugin add de.appplant.cordova.plugin.local-notification && cordova prepare

remove plugin.
cordova plugin rm de.appplant.cordova.plugin.local-notification

2. Update config.xml
Add this line to config.xml

<plugin name="local-notification" 
value="de.appplant.cordova.plugin.local-notification" />

3. Add simple notification in index.html.

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
      window.plugin.notification.local.add({ message: 'Great app!' });
}

4. Scheduled local notification.
If you want to get an alarm at the same time weekly...

var now = new Date().getTime(),
_60_seconds_from_now = new Date(now + 60*1000);

window.plugin.notification.local.add({
    id:      888,
    title:   'Reminder',
    message: 'Dont forget to buy some flowers.',
    repeat:  'weekly',
    autoCancel: true,
    sound:'TYPE_ALARM',
    date:    _60_seconds_from_now
 });

5. Add event function
window.plugin.notification.local.ontrigger = function(id, state, json){
// do something... 
}
window.plugin.notification.local.onadd = function(id, state, json){
// do something... 
}
window.plugin.notification.local.onclick = function(id, state, json){
// do something...
}

Useful  but these don't work well if the app is not on.




Tuesday, December 23, 2014

Deploy cordova apps to google play store.


0. Before release
Change version in the config.xml.
<widget id="com.yourdomain.yourapp" version="0.0.2" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
Make sure the same version in AndroidManifest.xml.
And android:debuggable should be false.
<manifest android:hardwareAccelerated="true" android:versionCode="2" android:versionName="0.0.2" package="com.yourdomain.yourapp" android:windowSoftInputMode="adjustPan" android:debuggable="false" xmlns:android="http://schemas.android.com/apk/res/android">

1. create keystore file

http://developer.android.com/tools/publishing/app-signing.html#cert

$ keytool -genkey -v -keystore [Key store file name] -alias [alias name] -keyalg [key algorithm type] -keysize [key size] -validity [expire days]

<example>
$ keytool -genkey -v -keystore mysampleappkey.keystore -alias mysampleapp -keyalg RSA -keysize 2048 -validity 18250

2. Ant configuration file
Add these lines to ant.properties file under platforms/android/.
If you are using windows based path, you have to use double slashes, \\ instead of \.

key.store=C:\\mykeyfolder\\my-release-key.keystore
key.alias=app_name
3. Build app
$ cordova build android

4. Make apk in release mode
$ cd platforms/android
$ ant release
Enter keystore and alias password in the middle of the process.
You could ignore warning messages.

5. Sign your app with your private key

http://developer.android.com/tools/publishing/app-signing.html#releasemode

$ cd bin
$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application.apk alias_name

Enter keystore password in the middle of the process.

6. Verify your apk is signed.
$ jarsigner -verify -verbose -certs my_application.apk

7. Align the final apk package.
$ zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk

8. Prepare images.
a. At least two screen shots of your application.
b. One high resolution icon.(512*512, 32bit png with alpha)
c. Feature Graphic.(1024w*500h, jpg or 24bit png with no alpha)
You need google account to sign in to Google play developer console.

9. Upload to google play store.
Please refer to this instuctions.
https://developer.android.com/distribute/googleplay/developer-console.html

a. Sign in to Google Play Developer Console.
b. Go to All applications All applications and click +Add new application button.
c. Select language and add application title, then click Upload APK button.
d. Select where to upload.
You can setup beta or alpha testing to get feedbacks from limited groups before launch your app to production.

For Beta and Alpha testing, you need to invite testers via  Google Group or Google+ Community.
https://support.google.com/googleplay/android-developer/answer/3131213?hl=en

e. Upload your app.

f. Go to Store Listing menu and fill out the fields.

Fields marked with * is mandate fields and you can check any missing fields by clicking 'Why can't I publish?'.
   
Add at least two screen shots.
 Upload a high resolution icon and feature graphic.
Additional infos... 



g. Go to Pricing & Distribution menu and fill out the fields.
Select countries where you want to distibute.
Check the agreements of Content guidlines and US export laws.

h. Now, you can publish your app to google play store.
You can install your app from this path.
https://play.google.com/apps/testing/[your package name].



Friday, December 5, 2014

using contact in cordova

Please refer to this page.. http://docs.phonegap.com/en/2.9.0/cordova_contacts_contacts.md.html

1. add plugin
$ cordova plugin add org.apache.cordova.contacts
2. Permissions

Android

app/res/xml/config.xml

<plugin name="Contacts" value="org.apache.cordova.ContactManager" />

app/AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

iOS

config.xml

<plugin name="Contacts" value="CDVContacts" />
3. make codes

contacts.create

Returns a new Contact object.
var contact = navigator.contacts.create(properties);

Quick Example

var myContact = navigator.contacts.create({"displayName": "Test User"});

contacts.find

Queries the device contacts database and returns one or more Contact objects, each containing the fields specified.
navigator.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions);

Quick Example

function onSuccess(contacts) {
    alert('Found ' + contacts.length + ' contacts.');
};
function onError(contactError) {
    alert('onError!');
};
// find all contacts with 'Bob' in any name field
var options      = new ContactFindOptions();
options.filter   = "Bob";
options.multiple = true;
var fields       = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);

Contact

Contains properties that describe a contact, such as a user's personal or business contact.

Properties

  • id: A globally unique identifier. (DOMString)
  • displayName: The name of this Contact, suitable for display to end-users. (DOMString)
  • name: An object containing all components of a persons name. (ContactName)
  • nickname: A casual name by which to address the contact. (DOMString)
  • phoneNumbers: An array of all the contact's phone numbers. (ContactField[])
  • emails: An array of all the contact's email addresses. (ContactField[])
  • addresses: An array of all the contact's addresses. (ContactAddress[])
  • ims: An array of all the contact's IM addresses. (ContactField[])
  • organizations: An array of all the contact's organizations. (ContactOrganization[])
  • birthday: The birthday of the contact. (Date)
  • note: A note about the contact. (DOMString)
  • photos: An array of the contact's photos. (ContactField[])
  • categories: An array of all the user-defined categories associated with the contact. (ContactField[])
  • urls: An array of web pages associated with the contact. (ContactField[])

Methods

  • clone: Returns a new Contact object that is a deep copy of the calling object, with the id property set to null.
  • remove: Removes the contact from the device contacts database, otherwise executes an error callback with a ContactError object.
  • save: Saves a new contact to the device contacts database, or updates an existing contact if a contact with the same idalready exists.

Save Quick Example

function onSuccess(contact) {
    alert("Save Success");
};
function onError(contactError) {
    alert("Error = " + contactError.code);
};
// create a new contact object
var contact = navigator.contacts.create();
contact.displayName = "Plumber";
contact.nickname = "Plumber";            // specify both to support all devices
// populate some fields
var name = new ContactName();
name.givenName = "Jane";
name.familyName = "Doe";
contact.name = name;
// save to device
contact.save(onSuccess,onError);

Clone Quick Example

    // clone the contact object
    var clone = contact.clone();
    clone.name.givenName = "John";
    console.log("Original contact name = " + contact.name.givenName);
    console.log("Cloned contact name = " + clone.name.givenName);

Remove Quick Example

function onSuccess() {
    alert("Removal Success");
};
function onError(contactError) {
    alert("Error = " + contactError.code);
};

    // remove the contact from the device
    contact.remove(onSuccess,onError);

ContactAddress

Contains address properties for a Contact object.

Properties

  • pref: Set to true if this ContactAddress contains the user's preferred value. (boolean)
  • type: A string indicating what type of field this is, home for example. (DOMString)
  • formatted: The full address formatted for display. (DOMString)
  • streetAddress: The full street address. (DOMString)
  • locality: The city or locality. (DOMString)
  • region: The state or region. (DOMString)
  • postalCode: The zip code or postal code. (DOMString)
  • country: The country name. (DOMString)

4. Additional things...

Methods

Arguments

Objects

Cordova plugin list

  • http://cordova.apache.org/docs/en/4.0.0/guide_cli_index.md.html
  • Basic device information (Device API):
    $ cordova plugin add org.apache.cordova.device
  • Network Connection and Battery Events:
    $ cordova plugin add org.apache.cordova.network-information
    $ cordova plugin add org.apache.cordova.battery-status
  • Accelerometer, Compass, and Geolocation:
    $ cordova plugin add org.apache.cordova.device-motion
    $ cordova plugin add org.apache.cordova.device-orientation
    $ cordova plugin add org.apache.cordova.geolocation
  • Camera, Media playback and Capture:
    $ cordova plugin add org.apache.cordova.camera
    $ cordova plugin add org.apache.cordova.media-capture
    $ cordova plugin add org.apache.cordova.media
  • Access files on device or network (File API):
    $ cordova plugin add org.apache.cordova.file
    $ cordova plugin add org.apache.cordova.file-transfer
  • Notification via dialog box or vibration:
    $ cordova plugin add org.apache.cordova.dialogs
    $ cordova plugin add org.apache.cordova.vibration
  • Contacts:
    $ cordova plugin add org.apache.cordova.contacts
  • Globalization:
    $ cordova plugin add org.apache.cordova.globalization
  • Splashscreen:
    $ cordova plugin add org.apache.cordova.splashscreen
  • Open new browser windows (InAppBrowser):
    $ cordova plugin add org.apache.cordova.inappbrowser
  • Debug console:
    $ cordova plugin add org.apache.cordova.console

Friday, November 7, 2014

Basic http server authentication from mobile app via ajax

1. script for ajax 
function make_base_auth(user, password) {
  var tok = user + ':' + password;
  var hash = btoa(tok);
  return "Basic " + hash;
}
$.ajax({
type: 'POST',
beforeSend: function (xhr){  xhr.setRequestHeader('Authorization', make_base_auth("guest", "guest"));   },
xhrFields: {
withCredentials: true
}
,  crossDomain: true,
url: 'http://www.gtatrade.ca/mobiletest',
data: ({name: "aaa"}),
success: function(data){alert(data.name);},
error: function(e, x, settings, exception){alert("error!!" + "," + e.staus + ","  + e.responseText + ","  + x + ","  + settings + ","  + exception);},
dataType: 'json'
});

*** If you send header every time, use ajaxSetup like below.
$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', make_base_auth("guest", "guest"));
    }
xhrFields: {
withCredentials: true
}
,  crossDomain: true,
});

2. Server php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header("Access-Control-Allow-Origin: *"); // required for cross domain .. 
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Authorization");
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
header("Access-Control-Allow-Origin: *");  // required for cross domain ..
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Authorization");
    $output = array("name"=>$_SERVER['PHP_AUTH_USER'], "pass"=>$_SERVER['PHP_AUTH_PW']);
    echo json_encode($output);
}