当GCM服务器向收件人发送邮件时,如何知道哪个用户发送了邮件

时间:2015-04-26 17:57:30

标签: java php android google-cloud-messaging google-play-services

我已经成功实现了GCM服务器,我收到消息以及收件人方面的通知。但我怎么知道哪个客户端发送了该消息。这是因为从服务器向GCM发送消息时我们从不包含密钥发送邮件的客户。

我有什么方法可以知道发件人密钥。

我实际做的是:             1.将消息从客户端应用程序发送到第三方服务器。             2.在第三方服务器中存储邮件。             3.从服务器向GCM发送消息。

这就是我实现服务器端代码以向GCM发送消息的方式。

$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 );

我在客户端使用了HTTP Post请求来发送收件人密钥和消息。

以下是我用来接收消息的IntentService。 此IntentService从广播接收器触发。

  import android.app.IntentService;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMIntentService extends IntentService{

    public GCMIntentService(String Iservice) {
        super(Iservice);
        // TODO Auto-generated constructor stub
    }

    public GCMIntentService()
    {
        super("MyIntentService");

    }
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        Bundle b=intent.getExtras();
        GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
        String mType=gcm.getMessageType(intent);   //Message received here.
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this);

        String msg = intent.getStringExtra("message");
        Log.e("Message recieved",msg+" ");          


        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);   //using notification manager to send notification
        Intent i = new Intent(this, Chats.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                               

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);              
        Builder builder  = new Notification.Builder(this);
        builder.setContentTitle("New Message Arrived!");
        builder.setContentText(msg+"");
        builder.setSmallIcon(R.drawable.ic_launcher);                       
        builder.setContentIntent(pIntent);
        mNotificationManager.notify(0, builder.build());                   

    }

}

receipent正在收到消息,但我怎么知道哪个用户发送了该消息。

0 个答案:

没有答案
相关问题