Search code examples
androidpushquickblox

Quickblox Push when App is Closed


I'm developing an app using Quickblox to create a chat with users. I've managed to get push services within the app, even when its in background but, once the app is force closed no push is received. My android devices are registering on GMC QB services, the token appears and it registers with no problem. Moreover I've even tried using the Push Admin on Quickblox but Push are not working either.

sourcecode:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <permission
        android:name="com.myapp.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.myapp.app.permission.C2D_MESSAGE" />
<receiver      
        android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
             <action android:name="com.google.android.c2dm.intent.RECEIVE" />
             <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.myapp.app" />
            </intent-filter>
        </receiver>
        <service android:name="com.quickblox.chat.push.QBGcmIntentServiceNewVersion"/>
        <!-- CUSTOM -->

        <service
            android:name="com.myapp.app.push.CPInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <service
            android:name="com.myapp.app.push.CPRegistrationIntentService"
            android:exported="false" />

And my BroadcastReceiver:

public class QBGcmBroadcastReceiver extends WakefulBroadcastReceiver {

    public static int QBNotificationID = 4444;

    @Override
    public void onReceive(Context context, Intent intentReceived) {

        Log.e(CPApplication.TAG, "QBGcmBroadcastReceiver -> Push RECIBIDA");

        Log.e(CPApplication.TAG, "APP is ON ?() -> " + CPApplication.appIsON);

        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(), QBGcmIntentServiceNewVersion.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intentReceived.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);

        // App is not running
        if (!CPApplication.appIsON) {

            // Is New Message Push enabled ?
            boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);

            // Is the user registered
            boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);

            if (rememberPassword && isPushNewMessage) {

                // Notification
                NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
                builder.setSmallIcon(R.drawable.push_icon)
                        .setTicker("Nuevo mensaje")
                        .setAutoCancel(true)
                        .setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
                        .setContentText("Tiene un mensaje nuevo");

                // Is New Message Sound enabled ?
                boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
                if (isSoundNewMessage) {

                    builder.setDefaults(Notification.DEFAULT_SOUND);
                }

                // Intent
                Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(contentIntent);

                // Notification
                Notification note = builder.build();
                // Will show lights and make the notification disappear when the presses it
                note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
                NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
                manager.notify(QBNotificationID, note);
            }
        }
    }

}

I've also created an intent service class:

public class QBGcmIntentServiceNewVersion extends IntentService {

    public static int QBNotificationID = 4444;

    public QBGcmIntentServiceNewVersion() {
        super(QBPushConstants.GCM_INTENT_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intentReceived) {
        Log.i(CPApplication.TAG, "QBGcmIntentServiceNewVersion :: onHandleIntent");

        // App is not running
//        if (!CPApplication.appIsON) {

        // Is New Message Push enabled ?
        boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);

        // Is the user registered
        boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);

        if (rememberPassword && isPushNewMessage) {

            // Notification
            NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
            builder.setSmallIcon(R.drawable.push_icon)
                    .setTicker("Nuevo mensaje")
                    .setAutoCancel(true)
                    .setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
                    .setContentText("Tiene un mensaje nuevo");

            // Is New Message Sound enabled ?
            boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
            if (isSoundNewMessage) {

                builder.setDefaults(Notification.DEFAULT_SOUND);
            }

            // Intent
            Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);

            // Notification
            Notification note = builder.build();
            // Will show lights and make the notification disappear when the presses it
            note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
            NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(QBNotificationID, note);
        }
//        }
    }
}

Thank you


Solution

  • Sorry, but I was putting the token instead of the deviceID, so it appears as a new registered device on QB admin, but with the wrong info, and that was the problem. I was using the new method from QB

    new SendPushTokenToQBTask(token, deviceId).execute();
    

    instead of

    new SendPushTokenToQBTask(deviceId, token).execute()
    

    Thank you anyways