应用程序关闭时Quickblox推送

时间:2015-12-28 19:02:42

标签: android push quickblox

我正在使用Quickblox开发应用以与用户建立聊天。我已经设法在应用程序中获得推送服务,即使它在后台但是,一旦应用程序强行关闭,就不会收到推送。 我的Android设备正在GMC QB服务上注册,令牌出现并且它注册没有问题。 此外,我甚至尝试在Quickblox上使用Push Admin,但Push也无法使用。

源代码:

 <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" />

我的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);
            }
        }
    }

}

我还创建了一个意向服务类:

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);
        }
//        }
    }
}

谢谢

2 个答案:

答案 0 :(得分:1)

很抱歉,但是我放的是令牌而不是deviceID,因此它在QB管理员身上显示为新注册的设备,但信息错误,这就是问题所在。我正在使用QB的新方法

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

而不是

new SendPushTokenToQBTask(deviceId, token).execute()

谢谢你

答案 1 :(得分:0)

发布您的权限可能也会有所帮助,但您没有将接收者中的名称更改为您的包名称:

android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"

尝试将其更改为:

android:name="com.myapp.app.QBGcmBroadcastReceiver"

如果无法正常编辑您的问题,请添加您的权限。希望它有所帮助。