How could I implement push notification in Android?

1,384 views
Skip to first unread message

A ti te digo

unread,
Mar 26, 2016, 11:09:58 PM3/26/16
to back{4}app
How could I implement push notification in Android to send pushes from client side, since it keeps asking for master key?

Davi Macêdo

unread,
Mar 28, 2016, 2:26:19 PM3/28/16
to back{4}app
For security reasons, Parse Server is only supporting push notifications using master key and therefore push notifications can't be sent directly from client anymore. You can see more details here:

The best practice now is to create a cloud function to send the push notifications. So your cliente will call the cloud function that then will send the push notifications.

In order to help our customers, we developed a code example of how you can create a cloud function to send the push notifications:

To call cloud functions from client, you can use the reference below:

alexan...@googlemail.com

unread,
Apr 6, 2016, 12:43:06 PM4/6/16
to back{4}app
Hello Davi,
i can successfully call the cloud code, but the device doesnt get the Push Notification.

private void callCloudCode() {
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("channels", "The Matrix Channel");
params.put("data", "The Matrix Data");
ParseCloud.callFunctionInBackground("push", params, new FunctionCallback<String>() {


@Override
public void done(String object, ParseException e) {
if (e == null) {
// ratings is 4.5
Log.i(TAG, "Successfully called cloud code: " + object);
} else {
Log.e(TAG, "Could not call cloud code: " + e.getMessage());
}
}
});
}

Thats what i get here:
04-06 18:38:29.936 11418-11418/com.directions.sample I/MainActivity: Successfully called cloud code: Success!

i added this to my Application:

ParseInstallation.getCurrentInstallation().saveInBackground();

Should there be more GCM Configuration in AndroidManifest.xml as well?

Thank you so much!
A.G



Davi Macêdo

unread,
Apr 7, 2016, 1:49:39 AM4/7/16
to back{4}app, Bruno Picinin
Hi, Alex.

Please, change the following lines and try again (@bruno, please correct me if I am wrong):
HashMap<String, Object> params = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
params.put("channels", ["The Matrix Channel"]);
data.put("alert", "The Matrix Data");
params.put("data", data);
Message has been deleted

alexan...@googlemail.com

unread,
Apr 7, 2016, 4:50:16 AM4/7/16
to back{4}app, br...@back4app.com
Dear Davi,
thank you.
I'm getting an syntax error with this and also tried to pass the standard array types, no success:

params.put("channels", ["The Matrix Channel"]); // <-- compiler error wrong array 
params.put("channels", new String[] {"The Matrix Channel"}); // <-- ParseException wrong channel type
params.put("channels", new String[] {"The Matrix Channel"}); // <-- ParseException wrong channel type

My Cloud code is from your url:
https://gist.github.com/brunopicinin/ac38aef5665aa3208c35
here i have noticed that ONLY the file uploaded with name "main.js" is working. Push.js or something else with same code will throw an error "invalid function".

So now i did a step back and used this cloud code:

Parse.Cloud.define('pushme', function (request, response) {

    // THIS METHOD NO LONGER WORKS
    // Parse.Cloud.useMasterKey();
    Parse.Push.send({
        channels: [ "channel_name" ],
        data: {
            alert: "Alert message"
        }
    }, {
        // ADD THE `useMasterKey` TO THE OPTIONS OBJECT
        useMasterKey: true,
        success: function () {
            response.success('Success!');
        },
        error: function (error) {
            response.error('Error! ' + error.message);
        }
    });
});

The call is working:
04-07 10:35:39.262 26911-26911/com.directions.sample I/MainActivity: Successfully called cloud code: Success!

BUT i have still no notification on my device.

May i ask how the GCM Configuration in the AndroidManifest should look like?

this is mine:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.directions.sample" >
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.directions.sample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.directions.sample.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_KEY"/>

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.parse.push.notification_icon"
android:resource="@mipmap/ic_launcher" />
</application>

</manifest>

Thank you very much!
Alex

alexan...@googlemail.com

unread,
Apr 7, 2016, 11:44:21 AM4/7/16
to back{4}app, br...@back4app.com
I spend 8 hours on this, stil no Push notifications:
What i tried so far:

In your "Android push notification configuration" i added Project-ID and API - Key (Stil no idea which one, your docs is not telling whether "Browserkey", Serverkey or Android-key" to use ?????)

In the back4app logs console im now getting this:
GCM request and response {"request":{"params":{"priority":"normal","data":{"time":"2016-04-07T15:33:06.185Z","push_id":"SkMs7ZTSgi","data":"{\"title\":\"Alex 954977199129\",\"message\":\"hey msg\"}"}}},"response":{"multicast_id":5557131503309148000,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}}

Like i said i tried all API-Keys.

The user installation is saved fine with the pushType "gcm".


Do you have an advice for me?

Bruno Picinin

unread,
Apr 7, 2016, 8:26:52 PM4/7/16
to back{4}app, br...@back4app.com
Hello Alex,

I'm going to suggest a few steps to get your Android push notifications up and running:


1. Get your GCM credentials

Go to the GCM guide on how to setup an Android device, scroll down and click on the "GET A CONFIGURATION FILE" button. Follow the steps to create/choose a project and enable Google Cloud Messaging. You'll be given a Server API Key and a Sender ID. Take note of these two values, as they are your GCM Android credentials.


2. Configure your app on the back4app dashboard

Click on your app on the back4app dashboard and choose "Android Push Notification Settings". On the settings screen add the API Key and Sender ID you got on the last step.


3. Edit your AndroidManifest.xml

Your AndroidManifest.xml should look something like the following. Be careful to not just override yours, since it may have specific information. Parts to be aware of are:
  • the user permissions required by the push service
  • replacing YOUR_SENDER_ID on the "com.parse.push.gcm_sender_id" meta-data with the Sender ID you got on the first step
  • the service and broadcast receiver definitions before the application closing tag
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.directions.sample">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:protectionLevel="signature"
android:name="com.directions.sample.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.directions.sample.permission.C2D_MESSAGE" />
    <!-- Other permissions for your app... -->

<application
        android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
        <activity android:name=".Main">
            <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

        <!-- Parse setup -->
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="YOUR_
APPLICATION_ID" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="YOUR_
CLIENT_KEY" />

<!-- GCM setup -->
<meta-data
android:name="com.parse.push.gcm_sender_id"
android:value="id:YOUR_SENDER_ID" />

<!-- The following service and broadcast receiver definitions must come immediately
before the closing </application> tag -->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
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.directions.sample" />
</intent-filter>
</receiver>
</application>

</manifest>


4. Update your Installation objects

In order for an app (Installation) to receive push notifications using a custom GCM Sender ID, the respective Installation object should have the "GCMSenderId" field set to match the Sender ID you got on the first step. You can do that manually, opening the Browser on the app's Parse Dashboard and inserting the Sender ID by hand on the right column. If you would rather have that value set when the user opens the app, you could put a code like the following on your main activity:

// Parse setup
Parse.initialize(this);

// Update Installation
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "YOUR_SENDER_ID");
installation.saveInBackground();

Again, remember to replace YOUR_SENDER_ID with the value you got on the first step.


5. Create a new build and test

Please, follow these steps to generate a new app version and test to see if the app is able to receive the notification. If there are any issues still, just let us know =)


Bruno

alexan...@googlemail.com

unread,
Apr 8, 2016, 4:00:40 AM4/8/16
to back{4}app, br...@back4app.com
You are my SUPERSTAR!!!!
Its working!!!!!!!!!!!!!!!!!
Thank you so much!!!

p.s i can localize some mistakes i did:

<!-- GCM setup -->
<meta-data
android:name="com.parse.push.gcm_sender_id"
android:value="id:YOUR_SENDER_ID" />

instead of


<!-- GCM setup -->
<meta-data
android:name="com.parse.push.gcm_sender_id"
    android:value="YOUR_SENDER_ID" />

and

// Update Installation
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "YOUR_SENDER_ID");
installation.saveInBackground();

//instead of

ParseInstallation.getCurrentInstallation().saveInBackground();

Thank you Bruno!!!

Kind regards
Alex


Davi Macêdo

unread,
Apr 8, 2016, 5:31:56 PM4/8/16
to back{4}app, br...@back4app.com
You're welcome @alex

Thanks @bruno! You really are a SUPERSTAR! :)

qangari

unread,
Apr 13, 2016, 11:31:21 AM4/13/16
to back{4}app
Hi Alex, mind sharing a full working code. I have spend two days on this n no push are being received yet.
Thanks. 

Davi Macêdo

unread,
Apr 14, 2016, 2:03:26 AM4/14/16
to back{4}app
Hi qangari.

What is the error you have?

Davi Macêdo

unread,
Apr 14, 2016, 4:03:11 PM4/14/16
to back{4}app
Post notifications now available through dashboard!

jiten...@gmail.com

unread,
Apr 14, 2016, 4:49:22 PM4/14/16
to back{4}app
Could you pls provide steps on how can this be tested from back4app dashboard?

jiten...@gmail.com

unread,
Apr 16, 2016, 4:01:34 AM4/16/16
to back{4}app
I could manage to have successful Push Notification sent from Dashboard to my Nexus AVD.
The only thing missing (or not mentioned) in above post is that the Parse init and ParseInstallation object operation must be done from a class that extends Application class and that class must me specified in Application.menifest as well.

Thanks all. Now basics are taken care of so I can work on main business logic :-).

Davi Macêdo

unread,
Apr 25, 2016, 12:44:27 AM4/25/16
to back{4}app
Happy to know it!

nora...@gmail.com

unread,
Jul 12, 2016, 4:51:17 AM7/12/16
to back{4}app, br...@back4app.com
Would you give me ur sample code please. I try it and not work

Davi Macêdo

unread,
Jul 19, 2016, 8:34:05 AM7/19/16
to back{4}app, br...@back4app.com
@noramadon please choose a slot time in the link below:

One of our engineers will help you to setup push notification.

raw...@gmail.com

unread,
Aug 2, 2016, 3:26:59 PM8/2/16
to back{4}app, br...@back4app.com
Davi Macedo,

Please my problem is when push notification sent it will not reach to android devices,

Davi Macêdo

unread,
Aug 22, 2016, 4:47:34 AM8/22/16
to back{4}app, br...@back4app.com
Hi. Did you solve this issue? Best!
Reply all
Reply to author
Forward
0 new messages