Android Open Source - ITM_Android_App G C M Intent Service






From Project

Back to project page ITM_Android_App.

License

The source code is released under:

GNU General Public License

If you think the Android project ITM_Android_App 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 2013 Bhavyanshu Parasher   
 * This file is part of "ITM University Android Application".
 * "ITM University Android Application" is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 * "ITM University Android Application" is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with "ITM University Android Application". 
 * If not, see http://www.gnu.org/licenses/.
 *//*from ww  w . j  av a2s  .co m*/

/**
 * Important file for GCM.
 */
package com.wbs.itm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
 
import com.google.android.gcm.GCMBaseIntentService;
 
import static com.wbs.itm.CommonUtilities.SENDER_ID;
import static com.wbs.itm.CommonUtilities.displayMessage;
 
public class GCMIntentService extends GCMBaseIntentService {
 
  
    private static final String TAG = "GCMIntentService";
 
    public GCMIntentService() {
        super(SENDER_ID);
    }
 
    /**
     * Method called on device registered
     **/
    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        displayMessage(context, "Your device registred with GCM");
        Log.d("NAME", MainScreenActivity.name);
        ServerUtilities.register(context, MainScreenActivity.name, MainScreenActivity.email, registrationId);
    }
 
    /**
     * Method called on device un registred
     * */
    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        displayMessage(context, getString(R.string.gcm_unregistered));
        ServerUtilities.unregister(context, registrationId);
    }
 
    /**
     * Method called on Receiving a new message
     * */
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message = intent.getExtras().getString("price");
        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }
 
    /**
     * Method called on receiving a deleted 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);
    }
 
    /**
     * Method called on Error
     * */
    @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);
    }
 
    /**
     * 
     * Issues a notification to inform the user that server has sent a message.
     */
    private void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        /*String title = context.getString(R.string.app_name); I want to display the app name in the title. But was not successful. checkout the xml file for the custom notification layout.*/
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        @SuppressWarnings("deprecation")
    Notification notification = new Notification(icon, message, when);
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.customlayoutnotif);
        contentView.setTextViewText(R.id.title, message);
        contentView.setImageViewResource(R.id.icon, icon);
        notification.contentView = contentView;
        Intent notificationIntent = new Intent(context, MainScreenActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.contentIntent = intent;
        //notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        // Play default notification sound
      //  notification.defaults |= Notification.DEFAULT_SOUND;
        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);     
    }
}




Java Source Code List

com.wbs.itm.AlertDialogManager.java
com.wbs.itm.AllAbtActivity.java
com.wbs.itm.AllAnnActivity.java
com.wbs.itm.AllEventsActivity.java
com.wbs.itm.AllQuestionActivity.java
com.wbs.itm.AllResultsActivity.java
com.wbs.itm.AppStatus.java
com.wbs.itm.CommonUtilities.java
com.wbs.itm.ConnectionDetector.java
com.wbs.itm.DownloadManager.java
com.wbs.itm.GCMIntentService.java
com.wbs.itm.JSONParser.java
com.wbs.itm.MainScreenActivity.java
com.wbs.itm.NewAnswerActivity.java
com.wbs.itm.NewQuestionActivity.java
com.wbs.itm.OnlineView.java
com.wbs.itm.PdfManager.java
com.wbs.itm.PdfViewActivity.java
com.wbs.itm.PhotoViewercse.java
com.wbs.itm.RegisterActivity.java
com.wbs.itm.ScanPdf.java
com.wbs.itm.SelectedQuestionActivity.java
com.wbs.itm.ServerUtilities.java
com.wbs.itm.TalkyLaunchActivity.java
com.wbs.itm.TimetableActivity.java
com.wbs.itm.WakeLocker.java