pusher / pusher-websocket-android

Library built on top of pusher-websocket-java for Android. Want Push Notifications? Check out Pusher Beams!
MIT License
36 stars 12 forks source link

FCM notifications only seem to work first time #44

Closed HelenaSewell closed 6 years ago

HelenaSewell commented 7 years ago

On the first launch of the app with the library added, the push notification was received just fine in the FCMListener. However, from this point on, no matter whether the app was in the foreground or background, I got no further notifications. The subscription seemed to work just fine, but no notifications came through. When I cleared the app's data, I got one notification again, and then nothing. I can see the notifications turning up in the Pusher dashboard, but not in the app. I also tried using a custom Service but that had the same effect.

public void initPushNotifications(){
        if (playServicesAvailable()) {
            PusherAndroid pusher = new PusherAndroid(BuildConfig.PUSHER_KEY);
            final PushNotificationRegistration nativePusher = pusher.nativePusher();
            nativePusher.setFCMListener(new FCMPushNotificationReceivedListener() {
                @Override
                public void onMessageReceived(RemoteMessage remoteMessage) {
                    Log.d("push notification", "recieved");
                }
            });
            try {
                nativePusher.registerFCM(this, new PushNotificationRegistrationListener() {
                    @Override
                    public void onSuccessfulRegistration() {
                        Log.d("push notifications", "success");
                        String interest = "INTEREST";
                        nativePusher.subscribe(interest);
                    }

                    @Override
                    public void onFailedRegistration(int statusCode, String response) {
                        Log.d("push notifications", "failure");
                    }
                });

            } catch (ManifestValidator.InvalidManifestException e) {
                e.printStackTrace();
            }
        }
    }

Apologies if my code is incorrect and is causing the issue, but I don't see how it could be causing the effects I'm seeing.

zmarkan commented 7 years ago

Hi!

Where is initPushNotifications() called? Is it run every time you run the app?

HelenaSewell commented 7 years ago

Yes, every time the logged-in section of the app is reached.

zmarkan commented 7 years ago

That's odd - I've used your code and it seems to be working fine. One thing I'd do however is to instantiate Pusher once per application and then reuse it everywhere. (Unsubscribing from interests won't work otherwise)

What kind of notifications are you sending - is it using the data or notification payload?

Also - are you seeing this kind of logs when you send pushes:

D/PFCMListenerService: Received from FCM: com.google.firebase.messaging.RemoteMessage@999ec6c

This should be our the Pusher library logging every single notification it receives. My suspicion is that your listener instance gets destroyed and then it won't log anything for you.