Parse Push Notification with Phonegap Android

Parse.com provides a great service to setup push notification for your android/iphone apps, features for which can be seen here https://www.parse.com/products/push

We will see in this blog post how to integrate parse push notification with phonegap for your android application.

Before starting you need to first create an account on Parse.com and generate application app_id and client_key.
Next install the phoengap module at https://github.com/manishiitg/parse-push-plugin
This plugin uses a newer version of parse library which used Google GCM and Parse Broadcast Receiver.

Step1
To install run this command

cordova plugin install https://github.com/manishiitg/parse-push-plugin

Step2
After the plugin gets installed, run “cordova build android” too see if the build completes successfully.
You may get an error like this

"com.android.dex.DexException: Multiple dex files define Lbolts/AggregateException;"

The reason for this might be your using facebook android library in your app which has the “bolts-android” library and the current parse plugin also has the same library. To fix this error, open folder “platforms\android\libs” and remove the “bolts-android” jar file.

Step3
Next step, is to add Parse.initialize code. For this go to directory “platforms\android\src\com\package\app” and create a file MainApplication.java. Write this code

package com.company.app; //replace with your package name

import android.app.Application;
import org.apache.cordova.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;
import com.parse.ParsePush;
import com.parse.ParseCrashReporting;

//// Track app opens.
//ParseAnalytics.trackAppOpened(getIntent());
// in cordova activty
public class MainApplication extends Application {

    @Override
    public void onCreate() {
      super.onCreate();
      ParseCrashReporting.enable(getApplicationContext()); //this only if you want to use crash reporting
      Parse.initialize(this, "app_id", "client_key");
      PushService.setDefaultPushCallback(this, CordovaApp.class);
      ParsePush.subscribeInBackground("Broadcast"); // if you want all app users to subscribe to a channel
      ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}

Open your CordovaApp.java file and put in line “ParseAnalytics.trackAppOpened(getIntent());” in the onCreate method. Also don’t forgot to get add “import com.parse.ParseAnalytics;” in your CordovaApp.java file.

Now open “AndroidManifest.xml” located in “platforms/android” find the “application” tag and add “android:name=”com.company.app.MainApplication” as attribute to the application tag.

Step5
Run your app using

If you get error link

error: cannot access Task

this means the bolts library is missing from android/platform/libs

cordova run android

At this point, if you send push notification from parse your app should receive it.

Step6
To be able to receive notification inside your phonegap application use code

ParsePushPlugin.register({
    appId:"PARSE_APPID", clientKey:"PARSE_CLIENT_KEY", eventKey:"myEventKey"},
    function() {
        alert('successfully registered device!');
    }, function(e) {
        alert('error registering device: ' + e);
    });

ParsePushPlugin.on('receivePN', function(pn){
        alert('yo i got this push notification:' + JSON.stringify(pn));
    });

Following above steps, you should be able to successfully start receiving notification from parse.