简体   繁体   中英

I need suggestions on sending push notifications

I'm at the point in my android app that I need to implement push notifications, I already have a Google API key and all and have enabled push notifications on the key.

I understand that notifications are to be sent from the server, downstream to the app.

Essentially what I am trying to do is whenever my database gets updated, I want a send a message to specific devices with no message in return.

But what I don't particularly understand is sending the message downstream to the phone. How is this done, I've only ever sent HTTP messages upstream to my server and am somewhat confused on how to get this done. Is there any server applications that can do this, if so what am I looking for.

There are several ways of sending downstream message from server.One being using PHP.You can write PHP script for sending message to the GCM(which actually sends notification to phone).

This is what I came up with for my app:

I have used curl

    $url = 'https://android.googleapis.com/gcm/send';
$receive_id=$_POST['receiver_key'];
$message=$_POST['message'];

$registrationIDs = array( $receive_id);


    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => array( "message" => $message ),
    );
    $headers = array(
        'Authorization: key=' . $api_key,
        'Content-Type: application/json'
    );

   $ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );

 $curl_result = curl_exec( $ch );

What I actually did is let the app trigger this PHP script to run when required.This is what called sending downstream message from server.

You don't handle the downstream to the phone yourself. You have several parts to the process that look like this.

Set yourself up with GCM (sounds like you've done already)

Have the app register that device for GCM and set up the handler for receiving push messages. This is usually done on app launch, so yes you will need prior communication with a device. You will also typically want to give them the option to opt out of push messages. Most of the app side of things are outlined here

Have a script that fires when your database is updated. You can then make the call to GCM to send the message by passing the message and specific devices' registration IDs as your payload.

A little more searching would get you this :

Push Notifications in Android using Google App Engine

It should be what you're looking for. Here's the link to Google's site also giving some added insight on it : http://developer.android.com/google/gcm/http.html

Since you are looking for a specific server functionality with push notifications, I would suggest to go with some software that does it for you instead of writing all the code for the server.

One such software I know is IBM MobileFirst (previously known as Worklight)... Its got push notification embed in that... There could be many more server softwares that do the same

Look at my dev push server . All you need just paste your google api key and registration id in corresponding fields and send push to your client. Also you can look source code of that push server on the Bitbucket

I just want to update this post just in case this information can be useful to others. So essentially what I've done is when a user installs the app, it will register with google to get the registration id then it will send it to my database for when i need it later. Now when a user updates the database by adding a post or lets say adding a comment, it will fire off a php script that runs my web servlet on tomcat, from there my tomcat servlet queries my database and figures out who(registration ID) to send a message to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM