0

I am working on notification but after A is sending notification B cannot receive the notification. I have added Toast to check and I can see sending was successful but it looks like onMessageReceived is not called. Could you please help where I make mistake? Thank you!!

Manifest

 <service android:name=".notifications.FirebaseService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_SERVICE"/>
            </intent-filter>
        </service>

        <service
            android:name=".notifications.FirebaseMessaging"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

This is onMessageReceived(). I could not see "message received"

    public class FirebaseMessaging extends FirebaseMessagingService
    {
        @Override
        public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            //get current user from share preference
            SharedPreferences sp = getSharedPreferences("SP_USER", MODE_PRIVATE);
            String savedCurrentUser = sp.getString("Current_USER", "None");

            Toast.makeText(this, "message received", Toast.LENGTH_SHORT).show();

            String sent = remoteMessage.getData().get("sent");
            String user = remoteMessage.getData().get("user");
            FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
            if(fUser != null && sent.equals(fUser.getUid())) {
                if(!savedCurrentUser.equals(user)){
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                       sendOAndAboveNotification(remoteMessage); 
                    }else {
                        sendNormalNotification(remoteMessage);
                    }
                }
            }


        }

This is where I send notification and I can see "Response" message

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()) {
                    Token token = ds.getValue(Token.class);
                    Data data = new Data(uid,name+":"+message,"New Message",destinationUid,R.drawable.icon_byb_final7);

                    Sender sender = new Sender(data, token.getToken());
                    apiService.sendNotification(sender)
                            .enqueue(new Callback<Response>() {
                                @Override
                                public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                                    Toast.makeText(MessageActivity.this,"Response"+response.message(),Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onFailure(Call<Response> call, Throwable t) {

                                }
                            });
                }
            }

1 Answer 1

0

It looks like code is correct and it didn't work if I use both emulator phones. If you see the same issue you can try to install app in your mobile and try one from emulator. It works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.