2015-12-28 3 views
0

Я разрабатываю приложение с помощью Quickblox для создания чата с пользователями. Мне удалось получить push-сервисы в приложении, даже когда он находится в фоновом режиме, но, как только приложение закрыто, никакое нажатие не получено. Мои устройства Android регистрируются в сервисах GMB QB, появляется токен, и он регистрируется без проблем. Кроме того, я даже попытался использовать Push-администратор QuickBlox, но Push тоже не работают.Quickblox 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); 
     } 
//  } 
    } 
} 

Спасибо вам

ответ

1

Извините, но я помещал токен вместо идентификатора deviceID, поэтому он отображается как новое зарегистрированное устройство в QB admin, но с неправильной информацией, и это была проблема. Я использовал новый метод от QB

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

вместо

new SendPushTokenToQBTask(deviceId, token).execute() 

Спасибо в любом случае

0

Проводки ваших Допустов Ионы также могут помочь, но вы не изменили имя в приемнике, чтобы ваше имя пакета:

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

попытаться изменить его на:

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

, если он не работает редактировать ваш вопрос надстройкой ваши разрешения. Надеюсь, поможет.

+0

Спасибо за вашу помощь. Я попытался изменить это, но проблема в QBGcmBroadcastReceiver - это пакет в моем приложении, поэтому я не могу изменить его так, как вы предложили. Я отредактировал свои разрешения, чтобы вы могли взглянуть. Надеюсь это поможет – Adrideh

Смежные вопросы