没有 gcm 之类的简单推送通知
Simple push notification without gcm like things
我想在没有 gcm 的 android 应用程序中实现推送通知,例如 things.I 想从我这边处理所有操作(android)。
直到现在我有一个 json response.Which 我正在尝试在推送中使用 notification.and 我的推送通知在点击时有效 event.But 我不知道 它如何自动(定期)检查或执行 json 响应 。这是我当前的 json 响应代码(使用 volley 库)。
public class MainActivity extends AppCompatActivity {
// Log tag
int total_time = 1000 * 60 * 60 * 24; // total one day you can change
int peroid_time = 5000; // one hour time is assumed to make request
private static final String TAG = MainActivity.class.getSimpleName();
private static String url = "http://my_url/Service.asmx/GetNotifications";
private ProgressDialog pDialog;
private List<Notifications> noteList = new ArrayList<Notifications>();
private ListView listView;
private Custom_Adapter_N adapter;
EditText ed1,ed2,ed3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_note);
adapter = new Custom_Adapter_N(this, noteList);
listView.setAdapter(adapter);
new CountDownTimer(peroid_time, total_time) {
public void onTick(long millisUntilFinished) {
// make request to web and get reponse and show notification.
MakingWebRequest();
Toast.makeText(MainActivity.this, " Tesitng the data", Toast.LENGTH_LONG).show();
}
public void onFinish() {
//
}
}.start();
// pDialog = new ProgressDialog(this);
// // Showing progress dialog before making http request
// pDialog.setMessage("Loading...");
// pDialog.show();
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);
}
// //on create
// @Override
// public void onDestroy() {
// super.onDestroy();
// hidePDialog();
// }
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
public void MakingWebRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
// Notifications note = new Notifications();
// note.setNotificationId(obj.getString("NotificationId"));
// note.setNotification(obj.getString("Notification"));
String excep = obj.getString("NotificationId");
String message1 = obj.getString("Notification");
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
Notification notify=new Notification(R.drawable.push,message1,System.currentTimeMillis());
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
notify = builder.setContentIntent(pending)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
.setSmallIcon(R.drawable.push).setTicker(excep).setWhen(System.currentTimeMillis())
.setAutoCancel(true).setContentTitle(message1)
.setContentText(message1).build();
// notif.notify(NOTIFICATION, notify);
notif.notify(0, notify);
// String id = obj.getString("Exception");
// String message1 = obj.getString("Message");
// Toast.makeText(Notification1.this, id.toString(), Toast.LENGTH_LONG).show();
// Toast.makeText(Notification1.this, message1.toString(), Toast.LENGTH_LONG).show();
// adding movie to movies array
// noteList.add(note);
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
//.... you rest code
// add your notification here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
}
)
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
此解决方案称为短期解决方案,因为它在 UI thread
上 运行。您可以在 service
中对 background working
执行相同的操作。
对于运行它在服务中看这里如何service start and works.
public class MainActivity extends Activity {
int total_time = 1000 * 60 * 60 * 24; // total one day you can change
int peroid_time = 1000 * 60 * 60; // one hour time is assumed to make request
// reduced the time while testing
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CountDownTimer(peroid_time, total_time) {
public void onTick(long millisUntilFinished) {
// make request to web and get reponse and show notification.
MakingWebRequest();
}
public void onFinish() {
//
}
}.start();
}
void MakingWebRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
//.... you rest code
// add your notification here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
我想在没有 gcm 的 android 应用程序中实现推送通知,例如 things.I 想从我这边处理所有操作(android)。
直到现在我有一个 json response.Which 我正在尝试在推送中使用 notification.and 我的推送通知在点击时有效 event.But 我不知道 它如何自动(定期)检查或执行 json 响应 。这是我当前的 json 响应代码(使用 volley 库)。
public class MainActivity extends AppCompatActivity {
// Log tag
int total_time = 1000 * 60 * 60 * 24; // total one day you can change
int peroid_time = 5000; // one hour time is assumed to make request
private static final String TAG = MainActivity.class.getSimpleName();
private static String url = "http://my_url/Service.asmx/GetNotifications";
private ProgressDialog pDialog;
private List<Notifications> noteList = new ArrayList<Notifications>();
private ListView listView;
private Custom_Adapter_N adapter;
EditText ed1,ed2,ed3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_note);
adapter = new Custom_Adapter_N(this, noteList);
listView.setAdapter(adapter);
new CountDownTimer(peroid_time, total_time) {
public void onTick(long millisUntilFinished) {
// make request to web and get reponse and show notification.
MakingWebRequest();
Toast.makeText(MainActivity.this, " Tesitng the data", Toast.LENGTH_LONG).show();
}
public void onFinish() {
//
}
}.start();
// pDialog = new ProgressDialog(this);
// // Showing progress dialog before making http request
// pDialog.setMessage("Loading...");
// pDialog.show();
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);
}
// //on create
// @Override
// public void onDestroy() {
// super.onDestroy();
// hidePDialog();
// }
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
public void MakingWebRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
// Notifications note = new Notifications();
// note.setNotificationId(obj.getString("NotificationId"));
// note.setNotification(obj.getString("Notification"));
String excep = obj.getString("NotificationId");
String message1 = obj.getString("Notification");
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
Notification notify=new Notification(R.drawable.push,message1,System.currentTimeMillis());
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
notify = builder.setContentIntent(pending)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
.setSmallIcon(R.drawable.push).setTicker(excep).setWhen(System.currentTimeMillis())
.setAutoCancel(true).setContentTitle(message1)
.setContentText(message1).build();
// notif.notify(NOTIFICATION, notify);
notif.notify(0, notify);
// String id = obj.getString("Exception");
// String message1 = obj.getString("Message");
// Toast.makeText(Notification1.this, id.toString(), Toast.LENGTH_LONG).show();
// Toast.makeText(Notification1.this, message1.toString(), Toast.LENGTH_LONG).show();
// adding movie to movies array
// noteList.add(note);
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
//.... you rest code
// add your notification here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
}
)
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
此解决方案称为短期解决方案,因为它在 UI thread
上 运行。您可以在 service
中对 background working
执行相同的操作。
对于运行它在服务中看这里如何service start and works.
public class MainActivity extends Activity {
int total_time = 1000 * 60 * 60 * 24; // total one day you can change
int peroid_time = 1000 * 60 * 60; // one hour time is assumed to make request
// reduced the time while testing
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CountDownTimer(peroid_time, total_time) {
public void onTick(long millisUntilFinished) {
// make request to web and get reponse and show notification.
MakingWebRequest();
}
public void onFinish() {
//
}
}.start();
}
void MakingWebRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
//.... you rest code
// add your notification here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}