Notifications

Local Notifications

Push Notifications with Firebase

You can send push notfications to Android devices using the self hosted Parse Server. The process is rather involving. You will need to fist get the API keys and Sender ID from Firebase Cloud Messanging.

Create an App on Firebase Console

  1. Navigate to Firebase Console and create a new project, https://console.firebase.google.com

  2. Once the project is created, create an App, choose Android App

  3. Enter the app’s package name, in my case its, com.peruzal.parsesdkdemo . You can find the package name in the AndroidManifest.xml file.

  4. Give an App nickname, which is optional.

  5. The debug signature is found on a Mac but open the terminal and running the following commands :


  $ cd ~/.local\share\Xamarin\Mono\ for\ Android\

  $ keytool -v -list -keystore debug.keystore

On Windows, run the following command :


keytool.exe -list -v -keystore "%LocalAppData%\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

If you get the error, command not found, make sure the location to the keytool is in your path. The keytools command is found in the Java JDK folder, for Java 32-bit it found in C:\Program Files (x86)\Java\jdkVERSION\bin\keytool.exe

You will get the following output, copy the SHA1 hash and paste into onto the Firebase console page.


$ keytool -v -list -keystore debug.keystore -storepass android -keypass android

Keystore type: JKS

Keystore provider: SUN

Your keystore contains 1 entry

Alias name: androiddebugkey

Creation date: 30 Sep 2016

Entry type: PrivateKeyEntry

Certificate chain length: 1

Certificate[1]:

Owner: CN=Android Debug, O=Android, C=US

Issuer: CN=Android Debug, O=Android, C=US

Serial number: 3dbdef7d

Valid from: Fri Sep 30 08:02:52 SAST 2016 until: Sun Sep 23 08:02:52 SAST 2046

Certificate fingerprints:

 MD5: A3:AC:3F:72:D2:A6:67:14:EE:E8:A5:5D:09:5F:5E:8B

 SHA1: E1:8E:FC:9F:B8:74:C3:CA:79:A9:AE:BA:A0:DB:82:64:48:6E:FE:35

 SHA256: 26:79:84:84:D4:99:4C:86:EF:AD:57:A1:E6:4B:3F:98:83:41:3D:49:21:5D:99:53:F3:B1:75:AA:35:87:A8:FB

 Signature algorithm name: SHA256withRSA

Completed Form

Navigate to the Cloud Messaging tab

Copy the Server Key and Sender Key

Take note of those as you will need them to configure your Parse Server.

Configure Parse Server

Open the index.js file on your Parse server file and add the entry for push :


    push: {

      android: {

        senderId: process.env.SENDER_ID,

        apiKey: process.env.GCM_API_KEY

      }

    }

So the complete entry with the other configs would look like this :

index.js


var api = new ParseServer({

  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',

  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',

  appId: process.env.APP_ID || 'myAppId',

  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!

  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed

  filesAdapter: new S3Adapter(

      process.env.S3_ACCESS_KEY,

      process.env.S3_SECRET_KEY,

      process.env.S3_BUCKET,

      {

        directAccess: true

      }

  ),    

  liveQuery: {

    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions

  },

    push: {

      android: {

        senderId: process.env.SENDER_ID,

        apiKey: process.env.GCM_API_KEY

      }

    }     

});

Start the Server

Start the server again by running


npm start

Register the Application Installation with Parse

You need to register the application with the GCM server before you can send push notification to the device.

The best place to register is within a class that extends the Android Application class, but for now we will just put all the code in a button click event handler for testing. Add the below code on the OnCreate method

MainActivity.cs


Button button = FindViewById<Button>(Resource.Id.myButton);

button.Click += delegate

			{

	ParseClient.Initialize(new ParseClient.Configuration

				{

		ApplicationId = "<REPLACE WITH APP_ID>",

		Server = "REPLACE WITH PARSE SERVER URL"

				});

	//ParseClient.Initialize("o4XPhIPImTU2d0ZPBOvKnqNs9ZprNxFNiaE6h4ct", "WWJyH3PbWkhftD27aISBoxCQCGYVeojjxpFUrZ63");

	ParseInstallation.CurrentInstallation.SaveAsync();

	ParsePush.ParsePushNotificationReceived += (object sender, ParsePushNotificationEventArgs e) =>

				{

		Log.Debug(TAG, "****************Push Received****************");

		Log.Debug(TAG, "****************Push Received****************");

		Log.Debug(TAG, "****************Push Received****************");

		Log.Debug(TAG, "****************Push Received****************");

		Log.Debug(TAG, "****************Push Received****************");

				};

			};

The layout file should have an entry a button like this :

Main.axml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <Button

        android:id="@+id/myButton"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" />

</LinearLayout>

Add Permission to the AndroidManifest.xml file

You will need make a couple of changes to the AndroidManifest.xml file, in order to receive the push.

Add the following permissions above the <application></application>


<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

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

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="com.peruzal.parsesdkdemo.permission.C2D_MESSAGE" android:protectionLevel="signature" />

<uses-permission android:name="com.peruzal.parsesdkdemo.permission.C2D_MESSAGE" />

Notice we are using the package name, com.peruzal.parsesdkdemo on creating this permission :


<permission android:name="com.peruzal.parsesdkdemo.permission.C2D_MESSAGE" android:protectionLevel="signature" />

Add these lines between the <application></application> tags :


  <service android:name="parse.ParsePushService" />

  <receiver android:name="parse.ParsePushBroadcastReceiver"

  android:permission="com.google.android.c2dm.permission.SEND">

    <intent-filter>

      <action android:name="com.google.android.c2dm.intent.RECEIVE" />

      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

      <category android:name="com.peruzal.parsesdkdemo" />

    </intent-filter>

  </receiver>

Also notice we are using the package name in the intent-filter category section


<category android:name="com.peruzal.parsesdkdemo" />

The complete AndroidManifest.xml file should look something like this

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.peruzal.parsesdkdemo">

	<uses-sdk android:minSdkVersion="10" />

	<uses-permission android:name="android.permission.INTERNET" />

	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

	<uses-permission android:name="android.permission.VIBRATE" />

	<uses-permission android:name="android.permission.WAKE_LOCK" />

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

	<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

	<permission android:name="com.peruzal.parsesdkdemo.permission.C2D_MESSAGE" android:protectionLevel="signature" />

	<uses-permission android:name="com.peruzal.parsesdkdemo.permission.C2D_MESSAGE" />

	<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">

	  <service android:name="parse.ParsePushService" />

	  <receiver android:name="parse.ParsePushBroadcastReceiver"

	  android:permission="com.google.android.c2dm.permission.SEND">

	    <intent-filter>

	      <action android:name="com.google.android.c2dm.intent.RECEIVE" />

	      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

	      <category android:name="com.peruzal.parsesdkdemo" />

	    </intent-filter>

	  </receiver>

	</application>

</manifest>

Run the App

Run the app and click the button. Go to the Parse Dashboard or you the API and the Installation endpoint.

If everything went well, the device should be registered and also if you check the output from the Device Log, you will notice the result of the GCM registration


parse.GcmRegistrar: GCM registration successful. 

Registration Id: <very long string here> 

Send Test Notification

Now we are ready to send a test notification. Parse Dashboard does seem ready as yet to send push notifications. We will send the message directly from Firebase Console.

  1. Open Firebase Console

  2. Navigate to the Notifications under the Grow section

  1. Fill the send message form and send the test message

Notice on the Device Log, you should see the message we logged when a new notification is received.

If you made it far, congratulations, you can now move your code to the Application class and implement your own receivers for notifications.