com.testpush.notification.GcmIntentService.java Source code

Java tutorial

Introduction

Here is the source code for com.testpush.notification.GcmIntentService.java

Source

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * 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 com.testpush.notification;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.testpush.MainActivity;
import com.testpush.MainActivity2;
import com.testpush.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/*
    
{
   titre="",
   message="",
   image="",
   id=""
}
    
*/

/**
 * This {@code IntentService} does the actual handling of the GCM message.
 * {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a
 * partial wake lock for this service while the service does its work. When the
 * service is finished, it calls {@code completeWakefulIntent()} to release the
 * wake lock.
 */
public class GcmIntentService extends IntentService {
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    //private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 10f;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCM";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        Log.d("GCM", "RECEIVE");

        if (!extras.isEmpty()) { // has effect of unparcelling Bundle

            Log.d("GCM", "RECEIVE CONTENT : " + extras.toString());

            /*
             * Filter messages based on message type. Since it is likely that GCM will be
            * extended in the future with new message types, just ignore any message types you're
            * not interested in, or that you don't recognize.
            */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                //sendNotification("Send error: " + extras.toString(),"");
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                //sendNotification("Deleted messages on server: " + extras.toString(),"");
                // If it's a regular GCM message, do some work.
            } else {
                if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    // LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                    //Criteria criteria = new Criteria();
                    // String bestProvider = locationManager.getBestProvider(criteria, false);
                    // Location location = locationManager.getLastKnownLocation(bestProvider);
                    //  Double lat, lon;
                    //}
                    // Toast.makeText(GcmIntentService.this, "message", Toast.LENGHT_LONG).show();
                    //  Toast mytoast=new Toast(MainActivity.this);
                    //  mytoast.setView(view);
                    ///  mytoast.setDuration(Toast.LENGTH_LONG);
                    // mytoast.show();
                    String titre = "Checking the location";// extras.getString("titre");
                    String message = "the office";// extras.getString("message");}

                    // if (location) {
                    // lat = location.getLatitude();
                    // lon = location.getLongitude();

                    //titre = String.valueOf(lat);//"this is my Toast message";// extras.getString("titre");
                    //message = String.valueOf(lat);//"this is my Toast message";// extras.getString("message");

                    //String titre = String.valueOf(lat);//"this is my Toast message";// extras.getString("titre");
                    //String message =String.valueOf(lat);//"this is my Toast message";// extras.getString("message");
                    String image = extras.getString("image");
                    String id = extras.getString("id");

                    Intent dialogIntent = new Intent(this, MainActivity2.class);
                    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(dialogIntent);
                    // startService(new Intent(this, services.class));
                    //Intent intent2 = new Intent(Intent.ACTION_SEND);
                    //intent2.setType("text/html");
                    //intent2.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
                    //intent2.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    // intent2.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

                    //startActivity(Intent.createChooser(intent2, "Send Email"));

                    //ComponentName location = new ComponentName(LocationService.getPackageName(),
                    //GcmIntentService.class.getName());
                    //  startService(new Intent(this, LocationService.class));
                    sendNotification(titre, message, image, id);
                }
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String titre, String message, String image, String id) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Log.d("GCM", "NOTIFICATION");

        //afficher la notif

        try {
            //Vibrator v = (Vibrator) this.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            //v.vibrate(new long[]{0,100,300,200},-1);
        } catch (Exception e) {
        }

        PackageManager pm = getPackageManager();
        Intent lancement = new Intent(this.getApplicationContext(), MainActivity.class);
        lancement.putExtra("LAUNCH_NOTIFICATION", true);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, lancement, 0);

        Bitmap bitmap = getBitmapFromURL(image);

        Notification notif;
        {
            notif = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(bitmap)
                    .setColor(Color.GRAY).setContentTitle(titre).setContentText(message)
                    .setContentIntent(contentIntent).build();

            notif.ledARGB = Color.GRAY;
            notif.flags = Notification.FLAG_SHOW_LIGHTS;
            notif.ledOnMS = 750;
            notif.ledOffMS = 2000;

        }

        //TODO mettre le nombre et un id diffrent
        notif.flags |= Notification.FLAG_AUTO_CANCEL;

        if (id == null)
            id = "";

        mNotificationManager.notify(id, id.hashCode(), notif);

    }

}