Android Open Source - Android-SDK Baas Cloud Messaging Service






From Project

Back to project page Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.baasbox.android;
//from www . j a  va 2 s . c o  m
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import com.baasbox.android.impl.GCMWrapper;
import com.baasbox.android.json.JsonArray;
import com.baasbox.android.json.JsonObject;
import com.baasbox.android.json.JsonStructure;
import com.baasbox.android.net.HttpRequest;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Andrea Tortorella on 14/04/14.
 */
public final class BaasCloudMessagingService {

    public static final int DEFAULT_PROFILE=1;
    public static final int PROFILE2=2;
    public static final int PROFILE3=3;


    private static final String MESSAGING_PREFS_NAME = ".BAAS_MESSAGING_PREFS";
    private static final String REGISTRATION_KEY = "registration_key";
    private static final String REGISTRATION_VERSION_KEY ="registration_version_key";
    private static final String USER_KEY = "user_key";

    private final BaasBox box;
    private final String packageName;
    private final int packageVersion;
    private final SharedPreferences messagingPreferences;

    BaasCloudMessagingService(BaasBox box){
        this.packageName = box.context.getPackageName();
        this.packageVersion= getPackageVersion(box.context);
        this.box=box;
        this.messagingPreferences=box.context.getSharedPreferences(packageName+MESSAGING_PREFS_NAME,Context.MODE_PRIVATE);
    }

    private static int getPackageVersion(Context context){
        PackageManager pm = context.getPackageManager();
        try {
            return pm.getPackageInfo(context.getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Cannot get package name",e);
        }
    }

    public RequestToken enable(BaasHandler<Void> handler){
        BaasBox box=BaasBox.getDefaultChecked();
        RegisterMessaging req= new RegisterMessaging(true,this,box, RequestOptions.DEFAULT,handler);
        return box.submitAsync(req);
    }

    public RequestToken disable(BaasHandler<Void> handler){
        BaasBox box = BaasBox.getDefaultChecked();
        RegisterMessaging req=new RegisterMessaging(false,this,box, RequestOptions.DEFAULT,handler);
        return box.submitAsync(req);
    }

    public BaasResult<Void> enableSync(){
        BaasBox box = BaasBox.getDefaultChecked();
        RegisterMessaging req=new RegisterMessaging(true,this,box, RequestOptions.DEFAULT,null);
        return box.submitSync(req);
    }

    public boolean isEnabled(){
        return !getRegistrationId(true).isEmpty();
    }

    public BaasResult<Void> disableSync(){
        BaasBox box = BaasBox.getDefaultChecked();
        RegisterMessaging req=new RegisterMessaging(false,this,box, RequestOptions.DEFAULT,null);
        return box.submitSync(req);
    }


    private String registerWithGoogle() throws IOException {
        return GCMWrapper.registerGCM(box.context,box.config.senderIds);
    }


    private void unregisterWithGoogle() throws IOException {
        GCMWrapper.unregisterGCM(box.context);
    }


    private boolean storeRegistrationId(String registrationId) {
        return messagingPreferences.edit().putString(REGISTRATION_KEY,registrationId)
                                   .putInt(REGISTRATION_VERSION_KEY,packageVersion)
                                   .commit();
    }


    private boolean clearRegistration() {
        return messagingPreferences.edit().clear().commit();
    }

    private String getRegistrationId(boolean checkVersion){
        String regKey =messagingPreferences.getString(REGISTRATION_KEY,"");
        if (regKey.isEmpty()){
            return "";
        } else if(checkVersion){
            int appVersion = messagingPreferences.getInt(REGISTRATION_VERSION_KEY,Integer.MIN_VALUE);
            int currentVersion = packageVersion;
            if (appVersion!=currentVersion){
                return "";
            }
        }
        return regKey;
    }

    public MessageBuilder newMessage(){
        return new MessageBuilder(box);
    }

    public static class MessageBuilder{
        private static final int MAX_TTL =2419200;
        private static final String MESSAGE = "message";
        private static final String USERS = "users";
        private static final String PROFILES = "profiles";
        private static final String SOUND = "sound";
        private static final String BADGE = "badge";
        private static final String ACTION_LOCALIZED_KEY="actionLocalizedKey";
        private static final String LOCALIZED_KEY="localizedKey";
        private static final String LOCALIZED_ARGUMENTS="localizedArguments";
        private static final String CUSTOM="custom";
        private static final String COLLAPSE_KEY="collapse_key";
        private static final String TTL="time_to_live";
        private static final String CONTENT_AVAILABLE="content-available";
        private static final String CATEGORY="category";

        private BaasBox box;

        private String message;
        private String sound;
        private Integer badge;
        private String actionLocalizedKey;
        private String localizedKey;
        private String collapseKey;
        private int ttl =-1;
        private String[] localizedArguments;
        private JsonStructure body;
        private Set<Integer> profiles = new HashSet<Integer>();

        private List<String> users = new ArrayList<String>();
        private boolean contentAvailable = false;
        private String category = null;

        private MessageBuilder(BaasBox box){
            this.box=box;
        }

        public MessageBuilder text(String message){
            this.message=message;
            return this;
        }

        public MessageBuilder sound(String sound){
            this.sound=sound;
            return this;
        }


        public MessageBuilder badge(int badge){
            this.badge=badge;
            return this;
        }

        public MessageBuilder profiles(int ... profiles){
            for (int p:profiles){
                this.profiles.add(p);
            }
            return this;
        }

        public MessageBuilder timeToLive(int ttl){
            if (ttl<0){
                ttl = -1;
            } else if(ttl>MAX_TTL){
                ttl = MAX_TTL;
            }
            this.ttl = ttl;
            return this;
        }

        public MessageBuilder collapseKey(String key){
            this.collapseKey=key;
            return this;
        }

        public MessageBuilder extra(JsonArray body) {
            this.body = body;
            return this;
        }
        public MessageBuilder extra(JsonObject body){
            this.body=body;
            return this;
        }

        public MessageBuilder to(BaasUser user){
            users.add(user.getName());
            return this;
        }

        public MessageBuilder to(BaasUser ... users){
            for (BaasUser u: users){
                this.users.add(u.getName());
            }
            return this;
        }

        public MessageBuilder to(String user){
            this.users.add(user);
            return this;
        }

        public MessageBuilder contentAvailable(boolean isAvailable){
            this.contentAvailable = isAvailable;
            return this;
        }

        public MessageBuilder category(String category){
            this.category = category;
            return this;
        }


        public MessageBuilder actionLocalizedKey(String key){
            this.actionLocalizedKey=key;
            return this;
        }

        public MessageBuilder localizedKey(String key){
            this.localizedKey=key;
            return this;
        }

        public MessageBuilder localizedArguments(String ... args){
            this.localizedArguments=args;
            return this;
        }


        public RequestToken send(BaasHandler<Void> handler){
            return send(RequestOptions.DEFAULT,handler);
        }

        public RequestToken send(int flags,BaasHandler<Void> handler){
            JsonObject message = prepareMessage();
            Send send = new Send(box,message,flags,handler);
            return box.submitAsync(send);
        }

        public BaasResult<Void> sendSync(){
            JsonObject message = prepareMessage();
            Send send = new Send(box,message,RequestOptions.DEFAULT,null);
            return box.submitSync(send);
        }


        private JsonObject prepareMessage() {
            if (users.size()==0) throw new IllegalStateException("cannot send message without users");
            if (message==null) throw new IllegalStateException("missing required text message");
            JsonArray to = new JsonArray();
            for (String u:users){
                to.add(u);
            }
            JsonObject m = new JsonObject();
            m.put(MESSAGE, message)
             .put(USERS, to);
            if (body!=null){
                if (body instanceof JsonObject){
                    m.put(CUSTOM,(JsonObject)body);
                } else if (body instanceof JsonArray){
                    m.put(CUSTOM,(JsonArray)body);
                }
            }
            if (sound!=null){
                m.put(SOUND, sound);
            }
            if (actionLocalizedKey!=null){
                m.put(ACTION_LOCALIZED_KEY, actionLocalizedKey);
            }
            if (localizedKey!=null){
                m.put(LOCALIZED_KEY, localizedKey);
            }
            if (localizedArguments!=null && localizedArguments.length>0){
                JsonArray args = JsonArray.of((Object[])localizedArguments);
                m.put(LOCALIZED_ARGUMENTS, args);
            }
            if (profiles.size()>0){
                JsonArray profiles = new JsonArray();
                for (int i: this.profiles){
                    profiles.add(i);
                }
                m.put(PROFILES, profiles);
            }
            if (badge!=null){
                m.put(BADGE, badge.longValue());
            }
            if (ttl!=-1){
                m.put(TTL,(long)ttl);
            }
            if (collapseKey!=null){
                m.put(COLLAPSE_KEY,collapseKey);
            }
            if (contentAvailable){
                m.put(CONTENT_AVAILABLE,1);
            }
            if (category!=null){
                m.put(CATEGORY,category);
            }
            return m;
        }

    }

    private static final class Send extends NetworkTask<Void>{
        private JsonObject message;

        protected Send(BaasBox box, JsonObject message,int flags, BaasHandler<Void> handler) {
            super(box, flags, handler,true);
            this.message=message;
        }

        @Override
        protected Void onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            return null;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            RequestFactory f = box.requestFactory;
            String endpoint = f.getEndpoint("push/message");
            return f.post(endpoint,message);
        }

    }
    private static final class RegisterMessaging extends NetworkTask<Void>{
        private static final String ENABLE_ENDPOINT = "push/enable/android/{}";
        private static final String DISABLE_ENDPOINT = "push/disable/{}";

        private final boolean mRegister;
        private String mRegistrationId;
        private final BaasCloudMessagingService service;

        protected RegisterMessaging(boolean register,BaasCloudMessagingService service,BaasBox box, int flags, BaasHandler<Void> handler) {
            super(box, flags, handler);
            mRegister = register;
            this.service=service;
        }

        @Override
        protected Void asyncCall() throws BaasException {
            if(BaasUser.current()==null){
                throw new BaasException("A user needs to be logged in");
            }
            //todo fix behaviour with multiple user
            if (mRegister){
                String registrationId = service.getRegistrationId(true);
                if (registrationId.isEmpty()){
                    try {
                        registrationId =service.registerWithGoogle();
                    }catch (IOException e){
                        throw new BaasIOException("Error while contacting google cloud messaging",e);
                    }
                }
                mRegistrationId = registrationId;
                super.asyncCall();
                service.storeRegistrationId(registrationId);
            } else {
                String registrationId = service.getRegistrationId(false);
                if (!registrationId.isEmpty()){
                    try {
                        service.unregisterWithGoogle();
                    } catch (IOException e) {
                        throw new BaasIOException("Failed to unregister with google cloud messaging",e);
                    }
                    mRegistrationId = registrationId;
                    super.asyncCall();
                    service.clearRegistration();
                }
            }
            return null;
        }


        @Override
        protected Void onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            return null;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            final String endpoint=mRegister?ENABLE_ENDPOINT:DISABLE_ENDPOINT;
            return box.requestFactory.put(box.requestFactory.getEndpoint(endpoint, mRegistrationId));
        }
    }

}




Java Source Code List

com.baasbox.android.AsyncStream.java
com.baasbox.android.BaasACL.java
com.baasbox.android.BaasApiException.java
com.baasbox.android.BaasAssetId.java
com.baasbox.android.BaasAsset.java
com.baasbox.android.BaasBox.java
com.baasbox.android.BaasCancellationException.java
com.baasbox.android.BaasClientException.java
com.baasbox.android.BaasCloudMessagingService.java
com.baasbox.android.BaasCredentialManager.java
com.baasbox.android.BaasDocument.java
com.baasbox.android.BaasException.java
com.baasbox.android.BaasFile.java
com.baasbox.android.BaasHandler.java
com.baasbox.android.BaasIOException.java
com.baasbox.android.BaasInvalidSessionException.java
com.baasbox.android.BaasLink.java
com.baasbox.android.BaasObject.java
com.baasbox.android.BaasQuery.java
com.baasbox.android.BaasResult.java
com.baasbox.android.BaasRuntimeException.java
com.baasbox.android.BaasServerException.java
com.baasbox.android.BaasStream.java
com.baasbox.android.BaasUser.java
com.baasbox.android.Cache.java
com.baasbox.android.DataStreamHandler.java
com.baasbox.android.ExceptionHandler.java
com.baasbox.android.FixedByteArrayOutputStream.java
com.baasbox.android.Grant.java
com.baasbox.android.HttpUrlConnectionClient.java
com.baasbox.android.JsonWrapper.java
com.baasbox.android.NetworkTask.java
com.baasbox.android.Plugin.java
com.baasbox.android.RequestFactory.java
com.baasbox.android.RequestOptions.java
com.baasbox.android.RequestToken.java
com.baasbox.android.RestImpl.java
com.baasbox.android.Rest.java
com.baasbox.android.Role.java
com.baasbox.android.SaveMode.java
com.baasbox.android.StreamBody.java
com.baasbox.android.StreamRequest.java
com.baasbox.android.impl.Base64.java
com.baasbox.android.impl.Constants.java
com.baasbox.android.impl.DiskLruCache.java
com.baasbox.android.impl.Dispatcher.java
com.baasbox.android.impl.GCMWrapper.java
com.baasbox.android.impl.ImmediateDispatcher.java
com.baasbox.android.impl.Logger.java
com.baasbox.android.impl.StrictLineReader.java
com.baasbox.android.impl.Task.java
com.baasbox.android.impl.Util.java
com.baasbox.android.impl.package-info.java
com.baasbox.android.json.JsonArray.java
com.baasbox.android.json.JsonException.java
com.baasbox.android.json.JsonObject.java
com.baasbox.android.json.JsonReader.java
com.baasbox.android.json.JsonScope.java
com.baasbox.android.json.JsonStructure.java
com.baasbox.android.json.JsonToken.java
com.baasbox.android.json.JsonWriter.java
com.baasbox.android.json.StringPool.java
com.baasbox.android.json.package-info.java
com.baasbox.android.net.HttpRequest.java
com.baasbox.android.net.OkClient.java
com.baasbox.android.net.RestClient.java
com.baasbox.android.plugins.glide.BaasAssetModelLoader.java
com.baasbox.android.plugins.glide.BaasFileModelLoader.java
com.baasbox.android.plugins.glide.BaasImageFetcher.java
com.baasbox.android.plugins.glide.GlidePlugin.java
com.baasbox.android.samples.aloa.Aloa.java
com.baasbox.android.samples.aloa.ApplicationTest.java
com.baasbox.android.samples.aloa.Configuration.java
com.baasbox.android.samples.aloa.Data.java
com.baasbox.android.samples.aloa.activities.CreateChannelFragment.java
com.baasbox.android.samples.aloa.activities.LoginActivity.java
com.baasbox.android.samples.aloa.activities.MainActivity.java
com.baasbox.android.samples.aloa.activities.OnTargetSelectedListener.java
com.baasbox.android.samples.aloa.activities.SendMessageActivity.java
com.baasbox.android.samples.aloa.activities.SendTargetFragment.java
com.baasbox.android.samples.aloa.activities.UserStatusFragment.java
com.baasbox.android.samples.aloa.activities.loaders.BaasUsersLoader.java
com.baasbox.android.samples.aloa.activities.loaders.ChannelsLoader.java
com.baasbox.android.samples.aloa.receive.NewMessageNotification.java
com.baasbox.android.samples.aloa.receive.PushReceiver.java
com.baasbox.android.samples.aloa.utils.BaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseActivity.java
com.baasbox.android.samples.aloa.utils.BaseBaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseFragment.java
com.baasbox.android.samples.phrasebook.Configuration.java
com.baasbox.android.samples.phrasebook.Phrasebook.java
com.baasbox.android.samples.phrasebook.ServerContract.java
com.baasbox.android.samples.phrasebook.common.BaseActivity.java
com.baasbox.android.samples.phrasebook.common.BaseFragment.java
com.baasbox.android.samples.phrasebook.loaders.BaasDocumentsLoader.java
com.baasbox.android.samples.phrasebook.ui.HomeActivity.java
com.baasbox.android.samples.phrasebook.ui.PhraseOfTheDay.java
com.baasbox.android.samples.phrasebook.utils.ThreadLocalRandom.java
com.baasbox.android.package-info.java