com.survtapp.gcm.MyGcmListenerService.java Source code

Java tutorial

Introduction

Here is the source code for com.survtapp.gcm.MyGcmListenerService.java

Source

/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.survtapp.gcm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GcmListenerService;
import com.survtapp.R;
import com.survtapp.database.DatabaseOperations;
import com.survtapp.login.MainActivity;

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

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";
    Bitmap bitmap;

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     * For Set of keys use data.keySet().
     */
    @Override
    public void onMessageReceived(String from, Bundle data) {
        super.onMessageReceived(from, data);
        // notifies user

        Log.d(TAG, data.toString());
        Log.d(TAG, from.toString());

        String msg = data.getString("msg");
        String date = data.getString("date");
        String title = data.getString("title");
        String url = data.getString("image_url");
        insertIntoDataBase(title, msg, date, url);

        try {
            showNotification(msg, url, title);

        } catch (Exception e) {
        }

        /*Intent downstreamMessageIntent = new Intent(RegistrationConstants.NEW_DOWNSTREAM_MESSAGE);
        downstreamMessageIntent.putExtra(RegistrationConstants.SENDER_ID, from);
        downstreamMessageIntent.putExtra(RegistrationConstants.EXTRA_KEY_BUNDLE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(downstreamMessageIntent);*/
    }

    private void insertIntoDataBase(String title, String msg, String date, String url) {
        /* NotificationIem ticket = new NotificationIem();
        ticket.setTitle(msg);
        ticket.setMessage("Alam");
        ticket.setDate(date);
        ticket.setImg_url("Faiz");
            
        myOpenTicketHandler.addOpenTicket(ticket);*/
        DatabaseOperations dop = new DatabaseOperations(getApplicationContext());
        dop.insertNotification(dop, title, msg, date, url);

    }

    public void showNotification(String msg, String url, String title) {
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
        Resources r = getResources();
        /* Notification notification = new NotificationCompat.Builder(this)
        .setTicker(msg)
        .setSmallIcon(R.drawable.app_icon)
        .setContentTitle("Survtapp")
        .setContentText(msg)
        .setContentIntent(pi)
        .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setAutoCancel(true)
        .build();*/
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, setBigPictureStyleNotification(msg, url, title));
    }

    private Notification setBigPictureStyleNotification(String msg, String url, String title) {
        Bitmap remote_picture = null;
        // Create the style object with BigPictureStyle subclass.
        NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
        notiStyle.setBigContentTitle(title);
        notiStyle.setSummaryText(msg);
        try {
            remote_picture = getBitmapFromURL(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        notiStyle.bigPicture(remote_picture);
        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        return new NotificationCompat.Builder(this).setSmallIcon(R.drawable.survtapp_notification)
                .setColor(getResources().getColor(R.color.actionbar_color)).setAutoCancel(true)
                .setDefaults(
                        Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(resultPendingIntent).setContentTitle(title).setContentText(msg)
                .setStyle(notiStyle).build();
    }

    public Bitmap getBitmapFromURL(String strURL) {
        try {
            URL url = new URL(strURL);
            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;
        }
    }
}