Android app not receiving notification from device to device

143
August 02, 2021, at 00:50 AM

I am trying to make a chatting app in android studio. All the basic functions are done but I have been struggling with a problem for a time. The problem is sending notification to a device. I have searched the web a lot and was finally successful when a error came by the code is given below.

sendNotification function

private void sendNoti(String senderId, String receiverId){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("to", "/User/"+receiverId);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("title", "Message from " + senderId);
                jsonObject1.put("body", "hello");
                jsonObject.put("notification", jsonObject1);
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e("SUCCESS", response + "");
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("ERRORS", error + "");
                    }
                }) {
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> map = new HashMap<>();
                        map.put("content-type", "application/json");
                        map.put("authorization", "key=key");
                        Log.d("Maping",map.toString());
                        return map;
                    }
                };
                RequestQueue requestQueue = Volley.newRequestQueue(this);
                int socketTimeout = 1000 * 60;// 60 seconds
                RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
                request.setRetryPolicy(policy);
                requestQueue.add(request);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull @NotNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        sendNotification(remoteMessage);
    }
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.e("newToken", s);
        getSharedPreferences("_", MODE_PRIVATE).edit().putString("fb", s).apply();
        System.out.println("Token "+s);
    }
    private void sendNotification(RemoteMessage remoteMessage) {
        String user = remoteMessage.getData().get("from");
        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");
        System.out.println("user "+user);
        System.out.println("title "+title);
        System.out.println("body "+body);
        Intent intent = new Intent(this, MainActivity.class);
        String channel_id = "notification_channel";
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder builder = new NotificationCompat
                .Builder(getApplicationContext(), channel_id)
                .setSmallIcon(R.drawable.vroo)
                .setContentTitle(title)
                .setContentText("body")
                .setAutoCancel(true)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setOnlyAlertOnce(true)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channel_id, "web_app", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        notificationManager.notify(0, builder.build());
    }
}

I get the log output that my message is sent but I never receive it on the receiver side can someone please help me?

READ ALSO
Is there a way to reset .bind(&ldquo;ended&rdquo;) for audio/video players in Javascript?

Is there a way to reset .bind(“ended”) for audio/video players in Javascript?

I am very new to javascriptI have a function that activates only when some media (audio or video) is played

64
How to create a confusion matrix for VGG16 image calssification (2 options) when using preprocessing.image_dataset_from_directory

How to create a confusion matrix for VGG16 image calssification (2 options) when using preprocessing.image_dataset_from_directory

I have been trying to create a confusion matrix to test my data on from my VGG16 classification model (python 38, using Keras)

151
I can&#39;t link the information from a database into my php document

I can't link the information from a database into my php document

I'm trying to make an online shop for my school canteen (this is a school assignment) and I'm really struggling with linking items from the database I've created into my PHP document

84
How do I turn my website into an app when adding to homescreen on an ios device?

How do I turn my website into an app when adding to homescreen on an ios device?

I have a website and I would like for it to be an app, take as an example the samsung itest which prompts you to add to the home screen on an ios device, how would I implement such a thing and then make it work as a fullscreen app?

63