How to send Push notification using Rest API

714 views
Skip to first unread message

noorul...@gmail.com

unread,
Apr 11, 2016, 8:18:42 AM4/11/16
to back{4}app
Hi,

In the Parse, I used to use the URL https://api.parse.com/1/push api to send push notifications via REST.

eg:
    
  
"{" +
       
"        \"where\": {" +
       
"          \"userrid\":  \"" + userRID + "\"" +
       
"        }," +
       
"        \"data\": " + dataString +
       
"      }";


What is the equivalent URL in back{4}app?

Thanks,
Noorul

Bruno Picinin

unread,
Apr 11, 2016, 2:23:49 PM4/11/16
to back{4}app
Hello Noorul,

In order to send push notifications via REST, there are basically two changes needed, in comparison to Parse:


1) Change the URL

The new URL is:


Instead of the old one:



2) Use the Master Key

The back4app service is based on the open-source platform developed by Parse itself (parse-server). This framework aims to maintain as much compatibility as possible with Parse, but at the same time, solve some security issues. One of the changes introduced is that, in order to send push notifications, you now need to use the Master Key of your app. That is valid even on the REST API.

So, what you'll need to do is replace the "X-Parse-REST-API-Key" header with the "X-Parse-Master-Key", updating the value accordingly.

If you don't use the Master Key, you'll get an error like the following:


HTTP/1.1 403 Forbidden

{"error":"unauthorized: master key is required"}


Example

In case it helps, I'm sending an example request bellow (using channels instead of the where clause).


POST /push HTTP/1.1
X-Parse-Application-Id: YOUR_APP_ID
X-Parse-Master-Key: YOUR_MASTER_KEY
Content-Type: application/json

{
    "channels": ["general"],
    "data": {
        "alert": "The Giants won against the Mets 2-3."
    }
}


If there are any issues still, just let us know =)

Bruno

Noorul Farhan Ahmed

unread,
Apr 12, 2016, 3:55:43 AM4/12/16
to back{4}app
Dear Bruno,

Many thanks, I get the confirmation that message is sent successfully.

{
"result": true
}


Now, how to configure the receiver in the Android app?

With parse, we configured it like the following :


<!-- Parse settings -->
<service android:name="com.parse.PushService" />

<receiver
 
android:name=".receiver.ParseReceiver"
 
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.rayat.pricewiz" />
 
</intent-filter>
</receiver>

And then in the `ParsePushBroadcastReceiver` we used to overwrite the `onReceive` method to receive the notification.

@Override
public void onReceive(Context context, Intent intent) {
 
String action = intent.getAction();
 
Log.d("TEST", action.toString());
 
if (action.equals(ParsePushBroadcastReceiver.ACTION_PUSH_RECEIVE)) {
 
String json;
 json
= intent.getStringExtra(ParsePushBroadcastReceiver.KEY_PUSH_DATA);
 generate_notification
(context,json);
 
}
}

Can you please advise how to do it in back4app?

Thanks,
Noorul 

Bruno Picinin

unread,
Apr 12, 2016, 12:07:50 PM4/12/16
to back{4}app
Sure,

Take a look at this answer I gave to a similar question:


Over there, I detailed all the steps to get your app to receive push notifications.

Bruno

andrei.a...@gmail.com

unread,
Jul 25, 2016, 10:19:36 AM7/25/16
to back{4}app
The anwer linked is using the com.parse.GcmBroadcastReceiver.

But we would like to use a custom PushReceiver, and we used to do that on Parse.com by extending the ParsePushBroadcastReceiver.

Can you please guide us on how to use a custom receiver? (I don't care if we are going to extend GcmBroadcastReceiver or ParsePushBroadcastReceiver, I just want to see a working example).

Thanks,
Andrei

Davi Macêdo

unread,
Jul 26, 2016, 5:15:13 PM7/26/16
to back{4}app
Hi, Andrei.

First of all, you have to extend ParsePushBroadcastReceiver. Something like this:

public class MyParsePushBroadcastReceiver extends ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) { ... }
}

Then, in manifest file, use the name of your custom class, instead of the default one:
...
<!-- 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.directions.sample.MyParsePushBroadcastReceiver"
            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>

If you have any problem, let me know.

Best!

emst....@gmail.com

unread,
Jan 11, 2017, 12:50:50 AM1/11/17
to back{4}app
 Hi  Bruno Picinin 

How to set schedule push notifications. And what will be its format.




Thank you.



emst....@gmail.com

unread,
Jan 11, 2017, 12:51:58 AM1/11/17
to back{4}app
Hi  Bruno Picinin 

How to set schedule push notifications. And what will be its format. in Back4App




Thank you.

Davi Macêdo

unread,
Jan 11, 2017, 8:07:36 AM1/11/17
to back{4}app
Hi, Naufal.

Parse Server does not have yet scheduled push yet.

The best way to do it now is use a Scheduled job to do this.

Best.

Reply all
Reply to author
Forward
0 new messages