如何在特定日期和时间发送gcm通知

时间:2016-05-11 11:25:53

标签: android google-cloud-messaging android-service

我有场景,其中说x为y分配一个具有截止日期和时间的任务。现在我在分配任务时发送通知,即点击按钮时发送日期&时间。现在,如果他未能在指定时间内完成,我还想发送提醒通知。 以下是我的代码

Button dialogButtonOk = (Button) dialog.findViewById(R.id.dialogButtonOk);
Button dialogButtonNo = (Button) dialog.findViewById(R.id.dialogButtonNo);
dialogButtonOk.setOnClickListener(new View.OnClickListener()
{
  @Override
    public void onClick (View v){
    changeCallStatus();
    sendGcmNotification();
    dialog.dismiss();
 }
});
dialogButtonNo.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick (View v){
    dialog.dismiss();
 }
});
dialog.show();
}
public void sendGcmNotification{
String status = "OnHold";
String url = URLMap.getSendNotificationUrl("sendnotification_url");
url = url.replace("{ename}", name).replace("{tid}", aTokenId).replace("{status}", status);
    StringRequest request=new StringRequest(Request.Method.GET,url,new    Response.Listener<String>(){
 @Override
 public void onResponse(String response){
    Log.i("GCM-MESSAGE","has been sent successfully");
     }
    },new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error){
    Log.i("GCM-MESSAGE","Failed to send the message : ComplaintDetailsSupervisor.java");
     }
    });
    request.setRetryPolicy(new VolleyRetryPolicy().getRetryPolicy());
    RequestQueue queue=((VolleyRequestQueue)getApplication()).getRequestQueue();
    queue.add(request);
      }
     }

服务器端代码

  namespace CMS.Notification
  {
    public class GCMNotification
    {
     private CMSEntities db = new CMSEntities();
     static string gcmid = "AIzaSyCDJbRbAl-mjG2am5R3gAaXmXXXXXXXXX";
     static string msg="";
     public string AssignEmpNotification(string employeeName, string TokenId,string status)
      {
        if (status.Trim() == "Open")
            msg = "value1" + employeeName;            
        else if (status.Trim() == "OnHold")
            msg = "value5" + employeeName;
        else
            msg = "value9" + employeeName;

        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
        String collaspeKey = Guid.NewGuid().ToString("n");
        String postData = string.Format("registration_id={0}&data.msg={1}&collapse_key={2}", TokenId, msg, collaspeKey);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse tResponse = tRequest.GetResponse();
        dataStream = tResponse.GetResponseStream();
        StreamReader tReader = new StreamReader(dataStream);
        String sResponseFromServer = tReader.ReadToEnd();
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return  sResponseFromServer;
    }        
}

我的接收者课程

public class NotificationService extends GcmListenerService {
String msg;
int time;
public static final String appname = "FM Ninja";
Intent myintent;
public NotificationService() {
    //    super("GcmIntentService");
}
public static int MESSAGE_NOTIFICATION_ID = 435345;
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + appname);
    Log.d(TAG, "Message: " + message);
    String msg1 = data.getString("msg");
    String name = "six30labs.io";
    sendNotification(name, msg1);
}

private void sendNotification(String title, String body) {
    Context context = getBaseContext();
    if(body.contains("value1")){                
        msg=body.replace("value1","New complaint has been Raised by \n");
        msg = msg + ". Tap to view!!";
        myintent=new Intent(this, HomeActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        myintent.setAction("item1");
    }             
    else if(body.contains("value5")) {
        msg = body.replace("value5", "Your task has been put On Hold by\n");
        msg = msg + ". Tap to view!!";
        myintent=new Intent(this, HomeActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        myintent.setAction("item5");
    }
    else{
        msg=body.replace("value9","Your task is due..Please contact your supervisor to change status..Thanks \n");
        msg = msg + ".\n Tap to view!!";
        myintent=new Intent(this, HomeActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        myintent.setAction("item9");
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)               .setContentTitle(title).setSmallIcon(R.drawable.fmn24).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).                        setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setVibrate(new long[]{1000,1000,1000,1000}).                        setAutoCancel(true).setColor(getResources().getColor(R.color.colorPrimary));
    time=(int)System.currentTimeMillis();
    PendingIntent intent = PendingIntent.getActivity(context, time,
            myintent, 0);
    mBuilder.setContentIntent(intent);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID,mBuilder.build());
    MESSAGE_NOTIFICATION_ID++;
 }
}

请建议我如何发送提醒通知.. 我想在满足截止日期和时间的情况下再次调用sendGcmNotification方法。请告诉我如何编写在满足截止日期和时间时触发sendGcmNotification方法的服务。 感谢

0 个答案:

没有答案
相关问题