com.devbd.cttd.hello_ct.PushPkg.GcmBroadcastReceiver.java Source code

Java tutorial

Introduction

Here is the source code for com.devbd.cttd.hello_ct.PushPkg.GcmBroadcastReceiver.java

Source

/*
 * Copyright 2013 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 com.devbd.cttd.hello_ct.PushPkg;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.Html;

import com.devbd.cttd.hello_ct.Noti;
import com.devbd.cttd.hello_ct.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * Handling of GCM messages.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;

    @Override
    public void onReceive(Context context, Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        System.out.println(">>>>>>>>>>  " + messageType + "  " + GoogleCloudMessaging.ERROR_MAIN_THREAD + "  "
                + GoogleCloudMessaging.ERROR_SERVICE_NOT_AVAILABLE + "  "
                + GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(intent.getExtras());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(intent.getExtras());
        } else {
            sendNotification(intent.getExtras());
        }
        setResultCode(Activity.RESULT_OK);
    }

    // Put the GCM message into a notification and post it.
    private void sendNotification(Bundle bundle) {

        String msg = bundle.getString("price");

        System.out.println(">>>>>>>>>>FFFF  " + bundle.toString());

        //       String from_name=bundle.getString("From_Name");
        //       String type=bundle.getString("Type");
        //       String params=bundle.getString("Params");

        //       NotificationDetails nd=new NotificationDetails();
        //       nd.setBody(msg);
        //       nd.setFrom_email(from_email);
        //       nd.setFrom_name(from_name);
        //       nd.setFrom_image(from_image);
        //       

        mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        // 
        if (msg != null) {

            Noti.msg = msg;
            PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, Noti.class),
                    PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx).setSmallIcon(R.mipmap.icon)
                    .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.icon))
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentTitle(ctx.getResources().getString(R.string.app_name)).setAutoCancel(true)
                    .setContentText(Html.fromHtml(msg))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(msg)));

            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    }
}