Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app?

How can I use Parse.com to send Push Notifications to my Cordova 3.5.0 Android app.

Most posts seem to cover some aspects of my problem but not the full range (Parse/Android or Phonegap/Parse)

I've actually solved this, but am putting the complete solution up here because I've had to use a variety of fragmented solutions and forums to find the answer, and I think Cordova/Phonegap and Parse are in increasingly popular combination, and seems to be a lot of people with similar problems.

like image 886
Pete Avatar asked Jul 30 '14 13:07

Pete


1 Answers

I've asked a variety of questions similar to this and done lots of googling, and I've managed to piece together a solution from a variety of places.

I've built my app with Cordova 3.5.0, via the command line. I believe these steps will work just as well with Phonegap 3.5.0, and probably earlier versions of both as long as it's post 3 (CLI). I'm using Eclipse and the ADT tools from Google

This wont work with Phonegap Build as you have to edit the Java and XML files in the Android project

For those that don't know, Phonegap is Adobe's distribution of Cordova, and is very similar but has some additional functionality on top, mostly for Phonegap Build I think.

For the purposes of this post you can swap Cordova for Phonegap, at least in the following steps. You'll need to do all these steps after creating the project with Cordova CLI

Cordova/Parse plugin

I've used the following plugin to connect Parse and Cordova. There are multiple versions of the plugin, it's been forked a few times but the version by Github user benjie's version does the most amount of automation for you, minimizing the need to get your hands dirty with the source code. Instructions for installing are found on the Github page:

  • https://github.com/benjie/phonegap-parse-plugin (no longer maintained - try a different fork)

Updating the Activity

You will now need to start editing the source code.

Find the main Activity class for your app, in the src section of the Eclipse navigator it'll be in your main package, something like com.company.myapp and then the Example.java file (assuming Example is your project name). It will have been generated for you by Cordova.

In the file add this import, it can just go after the rest of the import statements:

import com.parse.ParseAnalytics;

And then at the end of the onCreate method add this, to track in Parse when the user opens the app from a PN

ParseAnalytics.trackAppOpened(getIntent());

Extending the application

This final part I've got from the old Parse help forums, and has taken me the longest amount of time to solve.

If you leave the app as it is currently, you can receive Push notifications. In fact you should test that to make sure you've done it right so far.

...but if you force close the app (e.g. on the Galaxy S2 hold down the home button, and swipe the app away) you stop the app from receiving the Push Notifications.

I believe this is because you're killing every aspect of the app by forcing it closed including the listener for the PNs.

Using the following posts I managed to get the app to receive PNs after forcing it closed:

  • https://www.parse.com/questions/push-notification-in-android-while-app-closed
  • https://www.parse.com/questions/cannot-send-push-to-android-after-app-is-closed-until-screen-unlock

The actual solution for me was to do the following 2 steps:

1: Add a new file called ExampleApplication.java alongside your Example.java in com.company.myapp in the src section of Eclipse. The file needs the following content, updated accordingly for your project (e.g. your package and Parse keys):

package com.company.myapp;

import android.app.Application;
import android.content.Context;

import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;

import com.company.myapp.Example;

public class ExampleApplication extends Application 
{
    private static ExampleApplication instance = new ExampleApplication();

    public ExampleApplication() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // register device for parse
        Parse.initialize(this, "APP_KEY", "CLIENT_KEY");
        PushService.setDefaultPushCallback(this, Example.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}

2: Update your AndroidManifest.xml, so that you <application> tag has the following property, alongside those it already has:

android:name="com.company.myapp.ExampleApplication"

Summary

Once you've done that, you should be able to Push Notifications to your Android application.

To summarise:

  • Install the Phonegap/Parse plugin
  • Update the main Activity class
  • Extend the main Application class

This is probably transferable to non-eclipse projects, most of the steps will remain almost identical and if any one has any feedback regarding Android Studio or building without an IDE then we could update this answer to reflect that.

like image 152
2 revs, 2 users 99% Avatar answered Nov 16 '22 01:11

2 revs, 2 users 99%