I am new to android development. I couldn’t find and so posting here.
I want to send firebase push notification to all user through using admin app when implementation is done i am getting error getApplicationContext()
on a null object reference both class Push_Notification_All
and class MySingleton if someone know the solution please add here
getApplicationContext()’ on a null object reference
In Activity Push_Notification_All
and class MySingleton
public class Push_Notification_All extends AppCompatActivity {
private final String FCM_API = "https://fcm.googleapis.com/fcm/send";
String NOTIFICATION_MESSAGE;
String NOTIFICATION_TITLE;
final String TAG = "NOTIFICATION TAG";
String TOPIC;
private final String contentType = "application/json";
final Context context = this;
EditText edtMessage;
EditText edtTitle;
Dialog loading;
ProgressBar progressBar;
private final String serverKey = "key=ABBBAdfgfgzyw:APA91bFfdgdfgAFGveSeXnCNpglbKvpbd9-zu538Z1kA-QdMA41gRb2bgSXcu-2jlk1vhnY8ajXor-PYglN5H68ECTNqHeabfACfD8amSFRIghvGXsmIgEi6Ykw6Jlv2ihhdghfhhgBPr";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_push__notification__all);
edtTitle = (EditText) findViewById(R.id.edtTitle);
edtMessage = (EditText) findViewById(R.id.edtMessage);
loading = new Dialog(context);
loading.setContentView(R.layout.loading);
progressBar = (ProgressBar) loading.findViewById(R.id.spn_1);
((Button) findViewById(R.id.btnSend)).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!AppStatus.getInstance(Push_Notification_All.this).isOnline()) {
Toast.makeText(Push_Notification_All.this, "Internet is not Available", Toast.LENGTH_SHORT).show();
return;
}
Push_Notification_All.this.loading.show();
Push_Notification_All.this.loading.setCancelable(false);
Push_Notification_All.this.progressBar.setIndeterminateDrawable(new Circle());
Push_Notification_All push_Notification_All = Push_Notification_All.this;
push_Notification_All.TOPIC = "/topics/userABC";
push_Notification_All.NOTIFICATION_TITLE = push_Notification_All.edtTitle.getText().toString();
Push_Notification_All push_Notification_All2 = Push_Notification_All.this;
push_Notification_All2.NOTIFICATION_MESSAGE = push_Notification_All2.edtMessage.getText().toString();
JSONObject jSONObject = new JSONObject();
JSONObject jSONObject2 = new JSONObject();
try {
jSONObject2.put("title", Push_Notification_All.this.NOTIFICATION_TITLE);
jSONObject2.put("message", Push_Notification_All.this.NOTIFICATION_MESSAGE);
jSONObject.put("to", Push_Notification_All.this.TOPIC);
jSONObject.put("data", jSONObject2);
} catch (JSONException e) {
Push_Notification_All.this.loading.dismiss();
Log.e(TAG, "onCreate: " + e.getMessage());
}
Push_Notification_All.this.sendNotification(jSONObject);
}
});
}
public void sendNotification(JSONObject jSONObject) {
MySingleton.getInstance(Push_Notification_All.this).addToRequestQueue(new JsonObjectRequest(FCM_API, jSONObject, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject jSONObject) {
Log.i(TAG, "onResponse: " + jSONObject.toString());
Push_Notification_All.this.edtTitle.setText("");
Push_Notification_All.this.edtMessage.setText("");
Push_Notification_All.this.loading.dismiss();
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError volleyError) {
Push_Notification_All.this.loading.dismiss();
Toast.makeText(Push_Notification_All.this, "Request error", Toast.LENGTH_LONG).show();
Log.i(TAG, "onErrorResponse: Didn't work");
}
}) {
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap hashMap = new HashMap();
hashMap.put("Authorization", serverKey);
hashMap.put("Content-Type", contentType);
return hashMap;
}
});
}
}
MySingleton
public class MySingleton {
private static MySingleton instance;
private Context ctx;
private RequestQueue requestQueue = getRequestQueue();
private MySingleton(Context context) {
ctx = context;
}
public static synchronized MySingleton getInstance(Context context) {
MySingleton mySingleton;
synchronized (MySingleton.class) {
if (instance == null) {
instance = new MySingleton(context);
}
mySingleton = instance;
}
return mySingleton;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request) {
getRequestQueue().add(request);
}
}
2
Answers
Update the
MySingleton
class constructor to receive aContext
parameterUpdate
Push_Notification_All
Another way to make a singleton class in Android is to extend the
Application()
class and define that child class insideAndroidManifest.xml
under the tag android:name="Your singleton class," and you can get context inside theonCreate()
method. You don’t need to pass context fromActivity
.