Use Cordova to push notification to gcm

1,cordova create foldername packagename Appname

  eg: cordova create pushservice com.version.pushservice PushServiceDemo

2,cordova platform add android

3,cordova plugin add  cordova-plugin-device

4,cordova plugin add cordova-plugin-file

5,cordova plugin add cordova-plugin-media

6 cordova plugin add https://github.com/phonegap-build/PushPlugin.git

7.create a project on https://console.developers.google.com,and enable api (gcm)

8.generate api key and Autth client ID

9. copy javascrip code from pushplugin in folder example to demo ,and edit it.

    pushNotification.register(successHandler, errorHandler, {"senderID":"565080771215","ecb":"onNotification"});

    "565080771215" is a front part of client ID

10.if you want to custom notification audio,please add some patch in file platforms\android\src\com\plugin\gcm\GCMIntentService.java

import android.content.res.Resources;

import android.net.Uri;

String soundName = extras.getString("sound");

if (soundName != null) {

Resources r = getResources();

int resourceId = r.getIdentifier(soundName, "raw", context.getPackageName());

Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);

//defaults &= ~Notification.DEFAULT_SOUND;

//mBuilder.setDefaults(defaults);

mBuilder.setSound(soundUri);

}

11,send message to gcm

lib:httpclient4.5

public class PushDemo {

public static String REQUEST_URL = "https://android.googleapis.com/gcm/send";

    private static String GCM_ID = "APA91bF21aYAZmM6Uc0ckX0YDlTnO5e4_1zkgx1D2TWvX3gE8UwfqZn7qzPDG6abRb2o7YnXwT-WU4KFV-gpRyHULufVNKjxtZ5OJRA-Pzwmv15XVM03pMj0ZPP6dg";

public static void main(String[] args) {

  List<NameValuePair> formparams = new ArrayList<NameValuePair>();

   formparams.add(new BasicNameValuePair("registration_id", GCM_ID));

   formparams.add(new BasicNameValuePair("data.message", "testing"));

   formparams.add(new BasicNameValuePair("data.msgcnt", "1"));

   formparams.add(new BasicNameValuePair("data.sound", "beep"));

  

   HttpClient httpclient = HttpClientBuilder.create().build();

   HttpPost httpPost = new HttpPost(REQUEST_URL);

   HttpResponse response;

   httpPost.setHeader("Authorization",

           "key=AIzaSyC6pHODuXeUMO6_iEgin0e8");

   httpPost.setHeader("Content-Type",

           "application/x-www-form-urlencoded;charset=UTF-8");

   try {

       httpPost.setEntity(new UrlEncodedFormEntity(formparams, "utf-8"));

       httpclient.execute(httpPost);

       //Get the response

       response = httpclient.execute(httpPost);

       int responseCode = response.getStatusLine().getStatusCode();

        String responseText = Integer.toString(responseCode);      

        System.out.println("HTTP POST : " + responseText);

        /*Checking response */

        if(response!=null){

            InputStream in = response.getEntity().getContent(); //Get the data in the entity

            System.out.println("HTTP POST : " + in.toString());

        }

       //Print result

       System.out.println(response.toString());

   } catch (UnsupportedEncodingException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

   } catch (ClientProtocolException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

   } catch (IOException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

   }

        

}

}

相关推荐