Android Open Source - capture-the-flag Notifications Manager Google






From Project

Back to project page capture-the-flag.

License

The source code is released under:

Copyright ? 2014 Microsoft Mobile Oy. All rights reserved. Microsoft is a registered trademark of Microsoft Corporation. Nokia and HERE are trademarks and/or registered trademarks of Nokia Corporati...

If you think the Android project capture-the-flag 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

/*
 * Copyright (c) 2014 Microsoft Mobile. All rights reserved.
 * See the license text file provided with this project for more information.
 *//*from   w  w w.jav  a2s.c  o m*/

package com.nokia.example.capturetheflag.notifications.google;

import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.*;
import com.nokia.example.capturetheflag.notifications.NotificationsManagerBase;
import com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface;

/**
 * Google-specific implementation of the {@link NotificationsManagerInterface}.
 * Uses {@link GoogleCloudMessaging} for registering the application for push
 * notifications.
 */
public class NotificationsManagerGoogle extends NotificationsManagerBase {
    private static final String TAG = "CtF/NotificationsManagerGoogle";

    private static final String PREFS_KEY_REGISTRATION_ID = "registration_id";
    private static final String GCM_SENDER_ID = "1006294830624";

    private GoogleCloudMessaging mGcm;

    /**
     * Constructor.
     * <p/>
     * Constructs the Google Cloud Messaging specific Notifications Manager instance.
     *
     * @param context Context.
     */
    public NotificationsManagerGoogle(Context context) {
        super(context);

        if (checkPlayServices()) {
            mGcm = GoogleCloudMessaging.getInstance(context);
        } else {
            Log.d(TAG, "Google Play Services not available!");
        }
    }

    /**
     * Registers the application to the {@link GoogleCloudMessaging} for receiving
     * push notifications.
     */
    @Override
    public void register() {
        if (!hasRegistrationId()) {
            registerInBackground();
        }
    }

    ;

    /**
     * Checks whether Google Play Services are available or not.
     *
     * @return <code>true</code> if Google Play Services are available, <code>false</code> if not.
     */
    private boolean checkPlayServices() {
        final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
        return resultCode == ConnectionResult.SUCCESS;
    }

    /**
     * Stores the application's registration to {@link SharedPreferences}.
     *
     * @param registrationId Registration id to store.
     */
    private void storeRegistrationId(String registrationId) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        prefs.edit().putString(PREFS_KEY_REGISTRATION_ID, registrationId).apply();
    }

    /**
     * Returns the application's registration id.
     *
     * @return The registration id if available, null otherwise.
     */
    public String getRegistrationId() {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        return prefs.getString(PREFS_KEY_REGISTRATION_ID, null);
    }

    /**
     * Closes the Google Cloud Messaging connection when the application is terminated.
     */
    public void onDestroy() {
        mGcm.close();
    }

    @Override
    public NotificationServiceType getServiceType() {
        return NotificationServiceType.GOOGLE_CLOUD_MESSAGING;
    }

    /**
     * Registers the application to Google Cloud Messaging in the background
     * and stores the received registration id for later use.
     * <p/>
     * Registration is blocking so the operation is executed using
     * {@link AsyncTask} so that the UI does not get blocked.
     */
    private void registerInBackground() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                if (mGcm != null) {
                    try {
                        String registrationId = mGcm.register(GCM_SENDER_ID);
                        storeRegistrationId(registrationId);
                        Log.d(TAG, "Registered to Google Cloud Messaging:" + registrationId);
                    } catch (IOException ex) {
                        Log.d(TAG, "IO ERROR REGISTERING: " + ex.toString());
                    }
                }
                return null;
            }
        }.execute(null, null, null);
    }
}




Java Source Code List

com.nokia.example.capturetheflag.AboutActivity.java
com.nokia.example.capturetheflag.Controller.java
com.nokia.example.capturetheflag.CreateGameFragment.java
com.nokia.example.capturetheflag.GameEndedDialogFragment.java
com.nokia.example.capturetheflag.GameMenuFragment.java
com.nokia.example.capturetheflag.HelpActivity.java
com.nokia.example.capturetheflag.JoinGameFragment.java
com.nokia.example.capturetheflag.MainActivity.java
com.nokia.example.capturetheflag.PauseDialog.java
com.nokia.example.capturetheflag.PurchasePremiumFragment.java
com.nokia.example.capturetheflag.ServerSettingsDialog.java
com.nokia.example.capturetheflag.Settings.java
com.nokia.example.capturetheflag.location.LocationManagerBase.java
com.nokia.example.capturetheflag.location.LocationManagerFactory.java
com.nokia.example.capturetheflag.location.LocationManagerInterface.java
com.nokia.example.capturetheflag.location.LocationManagerListener.java
com.nokia.example.capturetheflag.location.LocationUtils.java
com.nokia.example.capturetheflag.location.google.LocationManagerGoogle.java
com.nokia.example.capturetheflag.location.here.LocationManagerHere.java
com.nokia.example.capturetheflag.map.GameMapFactory.java
com.nokia.example.capturetheflag.map.GameMapInterface.java
com.nokia.example.capturetheflag.map.GameMapUtils.java
com.nokia.example.capturetheflag.map.MarkerFactoryBase.java
com.nokia.example.capturetheflag.map.google.GameMapGoogle.java
com.nokia.example.capturetheflag.map.google.MarkerFactoryGoogle.java
com.nokia.example.capturetheflag.map.here.GameMapHere.java
com.nokia.example.capturetheflag.map.here.MarkerFactoryHere.java
com.nokia.example.capturetheflag.network.FlagCapturedResponse.java
com.nokia.example.capturetheflag.network.GameListRequest.java
com.nokia.example.capturetheflag.network.GameListResponse.java
com.nokia.example.capturetheflag.network.JSONRequest.java
com.nokia.example.capturetheflag.network.JSONResponse.java
com.nokia.example.capturetheflag.network.JoinRequest.java
com.nokia.example.capturetheflag.network.JoinedResponse.java
com.nokia.example.capturetheflag.network.NetworkClient.java
com.nokia.example.capturetheflag.network.OfflineClient.java
com.nokia.example.capturetheflag.network.SocketIONetworkClient.java
com.nokia.example.capturetheflag.network.UpdatePlayerRequest.java
com.nokia.example.capturetheflag.network.UpdatePlayerResponse.java
com.nokia.example.capturetheflag.network.model.Flag.java
com.nokia.example.capturetheflag.network.model.Game.java
com.nokia.example.capturetheflag.network.model.ModelConstants.java
com.nokia.example.capturetheflag.network.model.Player.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerBase.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.java
com.nokia.example.capturetheflag.notifications.NotificationsUtils.java
com.nokia.example.capturetheflag.notifications.google.GcmBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.google.GcmIntentService.java
com.nokia.example.capturetheflag.notifications.google.NotificationsManagerGoogle.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsIntentService.java
com.nokia.example.capturetheflag.notifications.nokia.NotificationsManagerNokia.java