示例:在我的应用程序中,我有客户端,分销商和server.client向服务器提出问题,服务器将通过Gcm推送通知回复该问题。我已通过谷歌搜索和推送通知教程来实现。
我的问题是,如果我从客户端发送问题,它会通过解析链接显示在服务器中,但我没有收到推送通知。任何人都帮我这个。我不知道我在哪里做错了。任何通过推送通知实现此功能的建议。服务器端脚本是用php写的
我已经发布了服务器部分代码...这里只有我想要推送通知
my GCM class
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService
{
private static final String TAG = "GCM Tutorial::Service";
// Use your PROJECT ID from Google API into SENDER_ID
public static final String SENDER_ID = "projectid";
//SharedPreferences prefs;
//DbHelper db;
String xx = "";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
//prefs = this.getSharedPreferences("", Context.MODE_PRIVATE);
//SharedPreferences.Editor prefsEditor = prefs.edit();
//prefsEditor.putString("registrationid", registrationId);
//prefsEditor.commit();
Log.i(TAG, "onRegistered: registrationId=" + registrationId);
xx = registrationId;
Toast.makeText(getApplicationContext(), xx, Toast.LENGTH_LONG).show();
System.out.println("Register : "+registrationId);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "onUnregistered: registrationId=" + registrationId);
}
@SuppressLint("NewApi")
@Override
protected void onMessage(Context context, Intent data) {
//db = new DbHelper(context);
//db.openToWrite();
String message;
// Message from PHP server
message = data.getStringExtra("message");
// Open a new activity called GCMMessageView
//db.insertmessage(message);
System.out.println("inside gcm class");
Intent intent = new Intent(this, MainActivity.class);
// Pass data to the new activity
intent.putExtra("message", message);
// Starts the activity on notification click
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification with a notification builder
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("New message")
.setContentText(message).setContentIntent(pIntent)
.getNotification();
// Remove the notification on click
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager manager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);
{
// Wake Android Device when notification received
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();
// Timer before putting Android Device to sleep mode.
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}
}
@Override
protected void onError(Context arg0, String errorId) {
Log.e(TAG, "onError: errorId=" + errorId);
}
}
问题类
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class prompt extends Activity
{
Button prompt_ok,prompt_cancel;
private static final String url="http://maps.googleapis.com/maps/api/geocode/json?address=";
private static final String url1="&sensor=false";
EditText zip,noofsup;
JSONArray results;
JSONObject geometry,location;
String latt,lngg,ques;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent in=getIntent();
ques=in.getStringExtra("questionfornext");
setContentView(R.layout.prompt);
zip=(EditText)findViewById(R.id.zipcode);
noofsup=(EditText)findViewById(R.id.noofsupplier);
}
public void onClick(View view)
{
String zipcode=zip.getText().toString();
String finalurl=url+zipcode+url1;
new GetData().execute();
new GetRegister().execute();
new GetQuestion().execute();
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
setContentView(R.layout.activity_main);
}
public void onCancel(View v)
{
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
setContentView(R.layout.activity_main);
}
private class GetData extends AsyncTask<Void, Void, Void>
{
String status;
String zipcode=zip.getText().toString();
String finalurl=url+zipcode+url1;
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Showing progress dialog
}
@Override
protected Void doInBackground(Void... arg0)
{
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(finalurl, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null)
{
try
{
JSONObject jsonObj = new JSONObject(jsonStr);
status=jsonObj.getString("status");
System.out.println("status"+status);
results=jsonObj.getJSONArray("results");
for(int j=0;j==0;j++)
{
JSONObject c=results.getJSONObject(0);
geometry=c.getJSONObject("geometry");
JSONObject ob=geometry.getJSONObject("location");
latt=ob.getString("lat");
lngg=ob.getString("lng");
// String latt=location.getString("lat");
//String lngg=location.getString("lng");
System.out.println("latt"+latt);
System.out.println("lng"+lngg);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if(status.equals("OK"))
{
new GetRegister().execute();
//Toast.makeText(getApplicationContext(), msg1, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Enter Valid ZIP", Toast.LENGTH_LONG).show();
}
}
}
private class GetRegister extends AsyncTask<Void, Void, Void>
{
//String android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
String weburl="register url ";//if we ask question it will automatically register
String weburl1="&latitude=";
String weburl2="&longtitude=";
String weburl3="&type=Android&author=Jers&Submit=submit";
String code,msg;
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
ServiceHandler sh=new ServiceHandler();
String finalweburl=weburl+MainActivity.androidId+weburl1+latt+weburl2+lngg+weburl3;
System.out.println("new final web url----////"+finalweburl);
String str=sh.makeServiceCall(finalweburl,ServiceHandler.GET);
if(str!=null)
{
try
{
JSONObject obj=new JSONObject(str);
code=obj.getString("code");
msg=obj.getString("msg");
System.out.println("jewellery id-----"+MainActivity.androidId);
System.out.println("code-----"+code);
}
catch(JSONException e)
{
e.printStackTrace();
}
}
else
{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if(code.equals("1"))
{
new GetQuestion().execute();
Toast.makeText(getApplicationContext(), "Registered", Toast.LENGTH_SHORT).show();
}
else
{
// Toast.makeText(getApplicationContext(),"NOT a question", Toast.LENGTH_LONG).show();
}
}
}
private class GetQuestion extends AsyncTask<Void, Void, Void>
{
EditText question1=(EditText)findViewById(R.id.question);
String success,msg1;
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
String noofsupplier=noofsup.getText().toString();
ServiceHandler sh=new ServiceHandler();
String android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
String url="request url for distributor ";
String url1="&question=";
String url2="&no_of_suppliers=";
String finalurlq1=url+android_id+url1+ques+url2+noofsupplier;
String finalurlq=new String(finalurlq1.replaceAll(" ","%20"));
System.out.println(finalurlq);
System.out.println("question---->"+ques);
String str1=sh.makeServiceCall(finalurlq,ServiceHandler.GET);
if(str1!=null)
{
try
{
JSONObject ob=new JSONObject(str1);
JSONObject ob1=ob.getJSONObject("response");
success=ob1.getString("success");
msg1=ob1.getString("msg");
System.out.println("success---->"+success);
System.out.println("msg----->"+msg1);
}
catch(JSONException e)
{
e.printStackTrace();
}
}
else
{
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
// Dismiss the progress dialog
// if (pDialog.isShowing())
// pDialog.dismiss();
/* * Updating parsed JSON data into ListView**/
if(success.equals("1"))
{
Toast.makeText(getApplicationContext(), "Question is Registered", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Question not registered", Toast.LENGTH_SHORT).show();
}
}
}
}