当两个用户订阅推送通知Quickblox时,订阅消失

时间:2017-04-08 05:13:39

标签: android notifications google-cloud-messaging push quickblox

我有两个Android应用:一个用于管理员,一个用于使用。两者都使用Quickblox作为后端。 我正在使用推送通知功能。 Everthing运作良好,但我有问题。 当我在同一台设备上的两个应用中登录两个不同的用户时。其中只有一个订阅了推送通知。我希望两者都是订阅推送通知。

详情:

用户1订阅了推送deviceA

然后用户2订阅了推送deviceA(同一设备)

此后只有用户2接收推送,而不是用户1

注意:我在之前的代码中为用户创建了会话,因此此代码只显示推送通知部分。

 public class PushNotificationHelper {
    public static void sendPushToUserById(final int Id,final String username, final String UrlAvatar,
                                          final String message)
    {
        QBUser user = SharedPrefsChatUtils.getQbUser(HabfitApplication.getAppContext());
        createEventToPush(user.getId(),Id, username, UrlAvatar, message);
    }

    private static void createEventToPush(int user_id, int Id,String username, String UrlAvatar, String message)
    {
        Log.d("message_id", user_id+"");
        Log.d("message_username", username);
        //Log.d("message_urlAvatar", UrlAvatar);
        Log.d("message_message", message);
        Log.d("id_opponent", Id+"");
        QBEvent event = new QBEvent();
        StringifyArrayList<Integer> userIds = new StringifyArrayList<Integer>();
        userIds.add(Id);
        event.setUserIds(userIds);
        event.setEnvironment(QBEnvironment.DEVELOPMENT);
        event.setNotificationType(QBNotificationType.PUSH);
        event.setPushType(QBPushType.GCM);
        JSONObject jsonData = new JSONObject();
        try {
            // standart parameters
            // read more about parameters formation http://quickblox.com/developers/Messages#Use_custom_parameters
            jsonData.put("message", message);
            // custom parameters
            jsonData.put("user_id", user_id);
            jsonData.put("username", username);
            jsonData.put("url_avatar", UrlAvatar);
        } catch (Exception e) {
            e.printStackTrace();
        }
        event.setMessage(jsonData.toString());
        QBPushNotifications.createEvent(event, new QBEntityCallback<QBEvent>() {
            @Override
            public void onSuccess(QBEvent qbEvent, Bundle args) {
                Log.d("create_event", "oke");
            }

            @Override
            public void onError(QBResponseException errors) {
                Log.d("create_event", "failed");
            }
        });
    }

    public static void subscribeToPushNotification()
    {
        GetRegistrationId getRegistrationId = new GetRegistrationId();
        getRegistrationId.execute();
    }

    private static void subscribe(final String registrationId)
    {
        QBUser user = SharedPrefsChatUtils.getQbUser(HabfitApplication.getAppContext());
        QBSubscription subscription = new QBSubscription(QBNotificationChannel.GCM);
        subscription.setEnvironment(QBEnvironment.DEVELOPMENT);
        //
        String deviceId;
        final TelephonyManager mTelephony = (TelephonyManager) HabfitApplication.getAppContext().getSystemService(
                        Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            deviceId = mTelephony.getDeviceId(); //*** use for mobiles
        } else {
            deviceId = Settings.Secure.getString(HabfitApplication.getAppContext().getContentResolver(),
                    Settings.Secure.ANDROID_ID); //*** use for tablets
        }
        subscription.setDeviceUdid(deviceId);
        subscription.setRegistrationID(registrationId);
        //
        QBPushNotifications.createSubscription(subscription, new QBEntityCallback<ArrayList<QBSubscription>>() {
            @Override
            public void onSuccess(ArrayList<QBSubscription> subscriptions, Bundle args) {
                Log.d("subscribe", "oke");
            }

            @Override
            public void onError(QBResponseException error) {
                        Log.d("subscribe", "failed");
                    }
        });
    }

    public static void unSubscribeNotification()
    {
        unregisterSubscribe();
    }

    private static void unregisterSubscribe()
    {
        QBPushNotifications.getSubscriptions(new QBEntityCallback<ArrayList<QBSubscription>>() {
            @Override
            public void onSuccess(ArrayList<QBSubscription> subscriptions, Bundle args) {

                String deviceId;
                final TelephonyManager mTelephony = (TelephonyManager) HabfitApplication.getAppContext().getSystemService(
                        Context.TELEPHONY_SERVICE);
                if (mTelephony.getDeviceId() != null) {
                    deviceId = mTelephony.getDeviceId(); //*** use for mobiles
                } else {
                    deviceId = Settings.Secure.getString(HabfitApplication.getAppContext().getContentResolver(),
                            Settings.Secure.ANDROID_ID); //*** use for tablets
                }

                for(QBSubscription subscription : subscriptions){
                    if(subscription.getDevice().getId().equals(deviceId)){
                        QBPushNotifications.deleteSubscription(subscription.getId(), new QBEntityCallback<Void>() {
                            @Override
                            public void onSuccess(Void aVoid, Bundle bundle) {
                                Log.d("unsubscribe", "success");
                            }

                            @Override
                            public void onError(QBResponseException e) {
                                Log.d("unsubscribe", "failed");
                            }
                        });
                        break;
                    }
                }
            }

            @Override
            public void onError(QBResponseException errors) {

            }
        });
    }

    private static class GetRegistrationId extends AsyncTask<String,String,String>
    {
        @Override
        protected String doInBackground(String... params) {
            InstanceID instanceID = InstanceID.getInstance(HabfitApplication.getAppContext());
            String senderId = HabfitApplication.getAppContext().getResources().getString(R.string.sender_id);
            String device_token = "";
            try {
                device_token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("device_token", device_token);
            return device_token;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            subscribe(s);
        }
    }
}

0 个答案:

没有答案