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

Could not get Push Notifications to work even if library says it worked #11

Closed EnricoOri closed 8 years ago

EnricoOri commented 8 years ago

On my onCreate method i use this static method to register for push (using GCM), which i configured following the guide on the website.

My Manifest (package name is COM.MYAPP) has:

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="COM.MYAPP.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="COM.MYAPP.permission.C2D_MESSAGE" />

<receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="COM.MYAPP" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.pusher.android.notifications.gcm.PusherGCMListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name="com.pusher.android.notifications.gcm.GCMInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>

        <service
            android:name="com.pusher.android.notifications.gcm.GCMRegistrationIntentService"
            android:exported="false">
        </service>

Then in the onCreate of my Main activity i call this method.

public static void registerForPush(Activity activity){
        try {
            PusherAndroid pusher = new PusherAndroid(CONF.PUSHER_API_KEY);
            final PushNotificationRegistration nativePusher = pusher.nativePusher();
            nativePusher.registerGCM(activity, CONF.SENDER_ID, new PushNotificationRegistrationListener() {
                @Override
                public void onSuccessfulRegistration() {
                    Log.d("REGPUSH", "OK!");
                    nativePusher.subscribe(CONF.PUSH_SUBSCRIPTION, new InterestSubscriptionChangeListener() {
                        @Override
                        public void onSubscriptionChangeSucceeded() {
                            Log.d("SUBSCRIBE", "OK");
                            nativePusher.setGCMListener(new GCMPushNotificationReceivedListener() {
                                @Override
                                public void onMessageReceived(String from, Bundle data) {
                                    Log.d("PUSH", "PUSH ARRIVED!!!!");
                                }
                            });
                        }

                        @Override
                        public void onSubscriptionChangeFailed(int statusCode, String response) {
                            Log.d("SUBSCRIBE", "FAIL!");
                        }
                    });
                }

                @Override
                public void onFailedRegistration(int statusCode, String response) {
                    Log.d("REGPUSH", "FAIL");
                }
            });
        } catch (ManifestValidator.InvalidManifestException e) {
            e.printStackTrace();
        }
    }

All callbacks result are ok but i can't retreive any push notification, the onMessageReceived is never fired up. It looks like it doesen't subscribe to the topic "CONF.PUSH_SUBSCRIPTION" We have also an iOS app configured the same way (with the same "interest") and there notifications arrive without any problem. Even tried configuring with Firebase, registration goes all right but I can get any push notification.

LaurieScheepers commented 7 years ago

Hi, I'm having the same issue. @dlockh3ll Why was this issue closed?

LaurieScheepers commented 7 years ago

Mine does subscribe successfully to the interest though. onMessageReceived is just never called

LaurieScheepers commented 7 years ago

@dlockh3ll Please respond, I'm really struggling with this issue. Did you get it to work in the end?

EnricoOri commented 7 years ago

@LaurieScheepers Sorry for the late response, i was not paying attention to Github these days. I got it to work changing the push method to Firebase (so not GCM) and by NOT using the "onMessageReceived" method but receiving the messages on a custom receiver that could get the "firebase message" intent filter. I think this approach will work also with GCM, just don't use the "onMessageReceived" but override a BroadcastReceiver and replace in the manifest the one i wrote above (with the same intent-filters, permissions etc.)

this android:name="com.google.android.gms.gcm.GcmReceiver" to android:name="com.yourapp.YourGCMReceiver"

This approach works with Firebase (even if the actual push delivery is handled differently compared to GCM), so I think you can use it also with GCM. I suggest you anyway to check GCM docs to properly handle received messages.

Hope it will help, and sorry for my bad english :-)

LaurieScheepers commented 7 years ago

@dlockh3ll Thanks so much! The custom broadcast receiver approach works, but I can't believe that this is necessary for it to work! It seems the setGcmListener() only works when the app is active (which totally destroys the point of Pusher push notifications). I guess there's a reason why this library is only v0.5. Cmon Pusher guys, get your act together.

Here are the things I tried before you replied: Stripped out Localytics and Parse libraries (which also contain a GcmListenerService), made a custom GcmListenerService, smashed some windows, swore at my neighbour etc. all without success.

Thanks again!

EnricoOri commented 7 years ago

@LaurieScheepers You are welcome 👍 Yes, it's pretty useless to have a push library which receives push notifications only when the app is active XD.

LaurieScheepers commented 7 years ago

Agreed. WTF