kr.co.bettersoft.checkmileage.activities.GCMIntentService.java Source code

Java tutorial

Introduction

Here is the source code for kr.co.bettersoft.checkmileage.activities.GCMIntentService.java

Source

/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kr.co.bettersoft.checkmileage.activities;

import static kr.co.bettersoft.checkmileage.common.CommonConstant.SENDER_ID;

import kr.co.bettersoft.checkmileage.activities.R;
import kr.co.bettersoft.checkmileage.common.CheckMileageCustomerRest;
import kr.co.bettersoft.checkmileage.domain.CheckMileageMembers;
import kr.co.bettersoft.checkmileage.pref.DummyActivity;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

/**
 * GCMIntentService
 * IntentService responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {
    static String tmpStr = "";

    CheckMileageCustomerRest checkMileageCustomerRest = new CheckMileageCustomerRest();
    String callResult = "";
    String tempstr = "";
    JSONObject jsonObject;
    // checkMileageCustomerRest = new CheckMileageCustomerRest();   // oncreate

    String regIdGCM = "";
    Boolean dontTwice = true;

    Context localContext;
    String localRegistrationId;

    //  ?    -- ? GCM ID   GCM ID  ? ?  ?? .
    SharedPreferences sharedPrefCustom;
    String savedGCMId = "";
    private static final String TAG = "GCMIntentService";

    static String myQR = "";

    public GCMIntentService() {
        super(SENDER_ID);
    }

    /**
     * onRegistered
     *  gcm ?? ? ? ??
     *
     * @param context
     * @param registrationId
     * @return
     */
    @Override
    public void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        // prefs
        sharedPrefCustom = getSharedPreferences("MyCustomePref",
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        savedGCMId = sharedPrefCustom.getString("savedGCMId", "");
        if (dontTwice) { //   .
            regIdGCM = registrationId;
            if (!(savedGCMId.equals(regIdGCM))) {
                SharedPreferences.Editor updatesavedGCMId = sharedPrefCustom.edit();
                updatesavedGCMId.putString("savedGCMId", regIdGCM); // GCM ID
                updatesavedGCMId.commit();
                new backgroundUpdateMyGCMtoServer().execute(); // ?  - ? GCM ?    
                localContext = context;
                localRegistrationId = registrationId;
                dontTwice = false;
            }
        }
    }

    // ? GCM ?    -- ? ? gcm ? 
    /**
     * backgroundUpdateMyGCMtoServer
     * ? ? gcm ??
     *
     * @param context
     * @param registrationId
     * @return
     */
    public class backgroundUpdateMyGCMtoServer extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPostExecute(Void result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.d(TAG, "backgroundUpdateMyGCMtoServer");

            if (myQR == null || myQR.length() < 1) {
                myQR = Main_TabsActivity.myQR;
            }
            if (myQR == null || myQR.length() < 1) {
                // qr  ??  ?
            } else { // qr ?? ??.
                //  
                CheckMileageMembers checkMileageMembersParam = new CheckMileageMembers();
                checkMileageMembersParam.setCheckMileageId(myQR);
                checkMileageMembersParam.setRegistrationId(regIdGCM);
                callResult = checkMileageCustomerRest.RestUpdateMyGCMtoServer(checkMileageMembersParam);
                //  
            }
            return null;
        }
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
    }

    /**
     * onMessage
     *  ?   ??  
     *
     * @param context
     * @param intent
     * @return
     */
    @Override // GCM  ?   ?.
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");

        //        String message = getString(R.string.gcm_message);
        String message = intent.getStringExtra("MESSAGE");
        //        displayMessage(context, message);
        Log.i(TAG, "Received message of onMessage():" + intent.getStringExtra("MESSAGE")); // ?.

        /*
         * MILEAGE :  ? ??  ? ???.
         * MARKETING : ?? ?    ?.
         * Check Mileage     ?. : test ..
         *   :  
         */
        //        if(intent.getStringExtra("MESSAGE").contains("MILEAGE")){
        // noti   ..  ? ? ?   ???   . -- ?  ??..
        // --> noti  .   ???  ..(  ? ?)
        //        }else{
        // notifies user
        generateNotification(context, message); // ??  
        //        }

    }

    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
        String message = getString(R.string.gcm_deleted, total);
        //        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
        //        displayMessage(context, getString(R.string.gcm_error, errorId));
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
        //        displayMessage(context, getString(R.string.gcm_recoverable_error, errorId));
        return super.onRecoverableError(context, errorId);
    }

    /**
     * generateNotification
     * Issues a notification to inform the user that server has sent a message.
     * @param context
     * @param message
     * @return
     */
    private void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_stat_gcm;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when); // sdf
        String title = context.getString(R.string.app_name);
        String mileageUpdateStr = context.getString(R.string.mileage_noti);
        Intent notificationIntent;
        /*
         * MILEAGE :  ? ??  ? ???.
         * MARKETING : ?? ?    ?.
         * Check Mileage     ?. : test ..
         * 
         * <string name="gcm_new_msgkkk"> </string>
         */
        String new_msg = " ";

        if (message.equals(new_msg)) {
            Log.d(TAG, "new msg - test");
            notificationIntent = new Intent(context, DummyActivity.class);
            //            notificationIntent.putExtra("RunMode", "TEST");                  // ?? ? . ?? .. ?   ..
            notificationIntent.putExtra("RunMode", "MILEAGE"); //

            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            //            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
            //                    | Intent.FLAG_ACTIVITY_CLEAR_TOP 
            //                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);

            //        }else if(message.contains("MILEAGE")){
            //        }else if(message.contains(mileageUpdateStr)){         // ?? ?. ()
        } else if (message.equals(mileageUpdateStr)) { // ?? ?. ().
            Log.d(TAG, "update mileage");
            MyMileagePageActivity.searched = false;
            //           notificationIntent = new Intent(context, MainActivity.class);      // ? ?  ?..
            notificationIntent = new Intent(context, DummyActivity.class);
            notificationIntent.putExtra("RunMode", "MILEAGE");

            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            //            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
            //                    | Intent.FLAG_ACTIVITY_CLEAR_TOP 
            //                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        } else if (message.equals("MARKETING")) {
            Log.d(TAG, "noti event push");
            //           notificationIntent = new Intent(context, MainActivity.class);      // ? ?  ? ?  ..
            notificationIntent = new Intent(context, DummyActivity.class);
            notificationIntent.putExtra("RunMode", "MARKETING");
            notificationIntent.putExtra("MESSAGE", "New Event");
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            //            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
            //                    | Intent.FLAG_ACTIVITY_CLEAR_TOP 
            //                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        } else { //  ?.
            Log.d(TAG, "noti event push");
            //           notificationIntent = new Intent(context, MainActivity.class);      // ? ?  ? ?  ..
            notificationIntent = new Intent(context, DummyActivity.class);
            notificationIntent.putExtra("RunMode", "MARKETING");
            notificationIntent.putExtra("MESSAGE", message);

            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            //            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
            //                    | Intent.FLAG_ACTIVITY_CLEAR_TOP 
            //                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        }
    }
}