Android Open Source - acs-android-sdk Push Notifications Manager






From Project

Back to project page acs-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project acs-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.appcelerator.cloud.pushdemo;
//w  w  w.  j  a  v  a2  s. c o  m
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import com.appcelerator.cloud.push.CCPushService;
import com.appcelerator.cloud.push.PushServiceException;
import com.appcelerator.cloud.sdk.CCMeta;
import com.appcelerator.cloud.sdk.CCRequestMethod;
import com.appcelerator.cloud.sdk.CCResponse;
import com.appcelerator.cloud.sdk.ACSClient;
import com.appcelerator.cloud.sdk.ACSClientError;

import android.content.Context;
import android.util.Log;

/**
 * Push notifications management static class.
 * Push service can be started from here.
 */
public class PushNotificationsManager{
  public final static String LOG_TAG = PushNotificationsManager.class.getName();
  
  public static void startPush(final Context context) {
      try {
      CCPushService.getInstance().startService(context);
    } catch (PushServiceException ex) {
      Log.e(LOG_TAG, "Push Service occurs an exception: " + ex.getMessage());
    }
  }
  
  public static void stopPush(Context context) {
    CCPushService.getInstance().stopService(context);
  }
  
  public static boolean subscribePushNotifications(ACSClient sdk, String deviceToken, String channel) throws IOException,
      ACSClientError, JSONException {
    String type = "android";

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("device_token", deviceToken);
    data.put("channel", channel);
    data.put("type", type);
    CCResponse response = sdk.sendRequest("push_notification/subscribe.json", CCRequestMethod.POST, data, false);
    CCMeta meta = response.getMeta();
    if ("ok".equals(meta.getStatus()) && meta.getCode() == 200 && "SubscribeNotification".equals(meta.getMethod())) {
      return true;
    }
    throw new ACSClientError("SubscribeNotification failed. Error Message:" + meta.getMessage());
  }
  
  public static boolean unsubscribePushNotifications(ACSClient sdk, String deviceToken, String channel) throws IOException,
      ACSClientError, JSONException {
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("device_token", deviceToken);
    data.put("channel", channel);
    CCResponse response = sdk.sendRequest("push_notification/unsubscribe.json", CCRequestMethod.DELETE, data, false);
    CCMeta meta = response.getMeta();
    if ("ok".equals(meta.getStatus()) && meta.getCode() == 200 && "UnsubscribeNotification".equals(meta.getMethod())) {
      return true;
    }
    throw new ACSClientError("UnsubscribeNotification failed. Error Message:" + meta.getMessage());
  }
  
  public static boolean loggedIn(ACSClient sdk) throws IOException, ACSClientError, JSONException {
    CCResponse response = sdk.sendRequest("users/show/me.json", CCRequestMethod.GET, null, false);
    CCMeta meta = response.getMeta();
    if ("ok".equals(meta.getStatus()) && meta.getCode() == 200 && "showMe".equals(meta.getMethod())) {
      return true;
    }
    return false;
  }
  
  public static boolean loginUser(ACSClient sdk, String username, String password) throws IOException, ACSClientError,
      JSONException {
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("login", username);
    data.put("password", password);
    CCResponse response = sdk.sendRequest("users/login.json", CCRequestMethod.POST, data, false);

    CCMeta meta = response.getMeta();
    if ("ok".equals(meta.getStatus()) && meta.getCode() == 200 && "loginUser".equals(meta.getMethod())) {
      return true;
    }
    throw new ACSClientError("User login failed. Error Message:" + meta.getMessage());
  }
  
  public static boolean logoutUser(ACSClient sdk) throws IOException, ACSClientError, JSONException {
    CCResponse response = sdk.sendRequest("users/logout.json", CCRequestMethod.GET, null, false);
    CCMeta meta = response.getMeta();
    if ("ok".equals(meta.getStatus()) && meta.getCode() == 200 && "logoutUser".equals(meta.getMethod())) {
      return true;
    }

    throw new ACSClientError("User logout failed. Error Message:" + meta.getMessage());
  }
}




Java Source Code List

com.appcelerator.cloud.demo.BaloonLayout.java
com.appcelerator.cloud.demo.CheckinAdapter.java
com.appcelerator.cloud.demo.DemoApplication.java
com.appcelerator.cloud.demo.DemoSession.java
com.appcelerator.cloud.demo.Explore.java
com.appcelerator.cloud.demo.MyDlgCustomizer.java
com.appcelerator.cloud.demo.PlaceAdapter.java
com.appcelerator.cloud.demo.PlaceView.java
com.appcelerator.cloud.demo.SignUp.java
com.appcelerator.cloud.demo.TabView.java
com.appcelerator.cloud.demo.UserView.java
com.appcelerator.cloud.pushdemo.ArrivalActivity.java
com.appcelerator.cloud.pushdemo.CustomReceiver.java
com.appcelerator.cloud.pushdemo.ExtendedReceiver.java
com.appcelerator.cloud.pushdemo.GCMUtility.java
com.appcelerator.cloud.pushdemo.PushActivity.java
com.appcelerator.cloud.pushdemo.PushNotificationsManager.java
com.appcelerator.cloud.sdk.ACSClientError.java
com.appcelerator.cloud.sdk.ACSClient.java
com.appcelerator.cloud.sdk.CCConstants.java
com.appcelerator.cloud.sdk.CCMeta.java
com.appcelerator.cloud.sdk.CCMultipartEntity.java
com.appcelerator.cloud.sdk.CCObject.java
com.appcelerator.cloud.sdk.CCPagination.java
com.appcelerator.cloud.sdk.CCRequestMethod.java
com.appcelerator.cloud.sdk.CCResponse.java
com.appcelerator.cloud.sdk.CCUser.java
com.appcelerator.cloud.sdk.SerializableCookie.java
com.appcelerator.cloud.sdk.oauth2.ACSClientDialog.java
com.appcelerator.cloud.sdk.oauth2.DialogError.java
com.appcelerator.cloud.sdk.oauth2.DialogListener.java
com.appcelerator.cloud.sdk.oauth2.DlgCustomizer.java
com.appcelerator.cloud.sdk.oauth2.Util.java
com.appcelerator.com.cloud.demotest.TestDriver.java