其实我有json url,我正在开发阅读这个json url的应用程序。 如果tr_id等于我的值并且状态值更改为"付费",我想收到通知。 我做了很多搜索,但我没有任何解决方案。 我试图使广播接收器监听互联网连接并加载json url值并在所有条件都是真的时发出通知。 但它不再工作,甚至吐司的消息,这里没有显示是我的尝试。
这是我的NotificationReceiver类
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.apk4android.resturantapp.R;
import retrofit2.Call;
import retrofit2.Callback;
public class NotificationReceiver extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Toast.makeText(context, "Receiver running", Toast.LENGTH_SHORT).show();
if (isOnline(context)) {
notificationListener();
}
}
SharedPreferences sp;
private void notificationListener() {
sp = context.getSharedPreferences("current_tr_id", Context.MODE_PRIVATE);
RegisterAPI2 registerAPI = ApiClient.getApiClient2().create(RegisterAPI2.class);
Call<TrResponse> req = registerAPI.getStatus();
req.enqueue(new Callback<TrResponse>() {
@Override
public void onResponse(Call<TrResponse> call,
retrofit2.Response<TrResponse> response) {
TrResponse post = response.body();
String tr_id = post.getTr_number().toString();
String status = post.getStatus().toString();
// make sure the tr_id of current order == tr_id of json
String currentID = sp.getString("currentID", "3");
Log.d("tr_id", "Current tr_id comes from user that paid the order = " + currentID);
if (status.equals("Paid") && !tr_id.equals("3") && tr_id.equals(currentID)) {
// make notification
notifyUser();
}
}
@Override
public void onFailure(Call<TrResponse> call, Throwable t) {
Log.d("TTTT CallBack", " Throwable is " + t + " Call " + call.toString());
}
}
);
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
}
private void notifyUser() {
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContent(contentView);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(1, notification);
}
}
Minifest
<receiver android:name=".notification.NotificationReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
抱歉我的英文。 所以请我。 谢谢。