Android Open Source - Buddy-Android-SDK Gcm Intent Service






From Project

Back to project page Buddy-Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Buddy-Android-SDK 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

package chat.sample.buddy.com.buddychat;
//from ww w. j  ava 2s.  c o m
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.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.buddy.sdk.Buddy;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import java.util.Date;

/**
 * Created by shawn on 8/27/14.
 */
public class GcmIntentService extends IntentService {


    public static final String ACTION_MESSAGE_RECEIVED =
            "chat.sample.buddy.com.buddychat.MESSAGE_RECEIVED";


    public GcmIntentService() {
        super(BuddyChatApplication.SENDER_ID);
    }

    @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);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * 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)) {
                Log.e(BuddyChatApplication.TAG,"Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                Log.w(BuddyChatApplication.TAG, "Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                // Post notification of received message.
                Buddy.recordNotificationReceived(intent);

                String userName = extras.getString("userName");
                String message = extras.getString("message");
                String payload = extras.getString("payload");


                sendNotification(userName, message, payload);
            Log.i(BuddyChatApplication.TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }


    private void populateIntent(Intent intent,String u, String m, String p) {

        String uid = null;
        if (p != null) {

            String[] parts = Chat.crackPayload(p);
            if (parts.length == 2) {
                uid = parts[Chat.PART_USERID];
            }
        }

        intent.putExtra("payload", p);
        intent.putExtra("userId", uid);
        intent.putExtra("userName", u);
        intent.putExtra("message", m);
    }

    // Do something with the push info we got
    //
    private void sendNotification(String userName, String message, String payload) {



        Intent payloadIntent = new Intent(ACTION_MESSAGE_RECEIVED);


        // send message notification
        if (payload != null && BuddyChatApplication.activeChat != null) {

            // if we got a payload and a chatwindow is being shown
            // then just send a payload-only message
            //
            payloadIntent.putExtra("payload", payload);
        }
        else {

            // otherwise, no chat is shown so show the push UI
            //

            populateIntent(payloadIntent, userName, message, payload);

            if (message != null) {

                Intent innerIntent = new Intent(this, Chat.class);

                NotificationManager notificationManager = (NotificationManager)
                        this.getSystemService(Context.NOTIFICATION_SERVICE);


                populateIntent(innerIntent, userName, message, payload);


                NotificationCompat.Builder builder =
                        new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentText(message)
                                .setContentTitle("Buddy Chat")
                                .setContentIntent(PendingIntent.getActivity(this, 0, innerIntent, PendingIntent.FLAG_CANCEL_CURRENT))
                                .setAutoCancel(true)
                                .setPriority(1)
                                .setDefaults(Notification.DEFAULT_ALL);

                notificationManager.notify((int)new Date().getTime(), builder.build());
            }

        }

        LocalBroadcastManager.getInstance(getApplicationContext())
                .sendBroadcast(payloadIntent);


    }
}




Java Source Code List

chat.sample.buddy.com.buddychat.ApplicationTest.java
chat.sample.buddy.com.buddychat.BuddyChatApplication.java
chat.sample.buddy.com.buddychat.Chat.java
chat.sample.buddy.com.buddychat.GcmBroadcastReceiver.java
chat.sample.buddy.com.buddychat.GcmIntentService.java
chat.sample.buddy.com.buddychat.GetCurrentUserCallback.java
chat.sample.buddy.com.buddychat.Loading.java
chat.sample.buddy.com.buddychat.Login.java
chat.sample.buddy.com.buddychat.MainScreen.java
chat.sample.buddy.com.buddychat.Signup.java
chat.sample.buddy.com.buddychat.SimpleAdapter.java
com.buddy.sample.buddychat.LoginActivity.java
com.buddy.sdk.AccessTokenCallback.java
com.buddy.sdk.BuddyCallback.java
com.buddy.sdk.BuddyClientImpl.java
com.buddy.sdk.BuddyClientOptions.java
com.buddy.sdk.BuddyClient.java
com.buddy.sdk.BuddyDateDeserializer.java
com.buddy.sdk.BuddyFile.java
com.buddy.sdk.BuddyFutureCallback.java
com.buddy.sdk.BuddyFuture.java
com.buddy.sdk.BuddyLocationDeserializer.java
com.buddy.sdk.BuddyLocationRangeSerializer.java
com.buddy.sdk.BuddyLocationSerializer.java
com.buddy.sdk.BuddyResultBase.java
com.buddy.sdk.BuddyResult.java
com.buddy.sdk.BuddyServiceClient.java
com.buddy.sdk.Buddy.java
com.buddy.sdk.DateRangeSerializer.java
com.buddy.sdk.DateRange.java
com.buddy.sdk.JsonEnvelopeBase.java
com.buddy.sdk.JsonEnvelopeDeserializer.java
com.buddy.sdk.JsonEnvelope.java
com.buddy.sdk.UserAuthenticationRequiredCallback.java
com.buddy.sdk.models.AlbumItem.java
com.buddy.sdk.models.Album.java
com.buddy.sdk.models.BinaryModelBase.java
com.buddy.sdk.models.Blob.java
com.buddy.sdk.models.BuddyLocation.java
com.buddy.sdk.models.Checkin.java
com.buddy.sdk.models.Identity.java
com.buddy.sdk.models.LocationRange.java
com.buddy.sdk.models.Message.java
com.buddy.sdk.models.MetadataItem.java
com.buddy.sdk.models.ModelBase.java
com.buddy.sdk.models.NotificationResult.java
com.buddy.sdk.models.PagedResult.java
com.buddy.sdk.models.Picture.java
com.buddy.sdk.models.SocialNetworkUser.java
com.buddy.sdk.models.TimedMetric.java
com.buddy.sdk.models.UserListItem.java
com.buddy.sdk.models.UserList.java
com.buddy.sdk.models.User.java
com.buddy.sdk.models.Video.java