com.google.android.gms.gcm.GoogleCloudMessaging

Here are the examples of the java api class com.google.android.gms.gcm.GoogleCloudMessaging taken from open source projects.

1. LensRocketBroadcastReceiver#onReceive()

View license
@Override
public void onReceive(Context context, Intent intent) {
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    mContext = context;
    mLensRocketApplication = (LensRocketApplication) mContext.getApplicationContext();
    mLensRocketService = mLensRocketApplication.getLensRocketService();
    String messageType = gcm.getMessageType(intent);
    if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
        sendNotification("Send error: " + intent.getExtras().toString());
    } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
        sendNotification("Deleted messages on server: " + intent.getExtras().toString());
    } else {
        LensRocketLogger.i(TAG, "Message: " + intent.getStringExtra("message"));
        String message = intent.getStringExtra("message");
        processPush(message);
    }
    setResultCode(Activity.RESULT_OK);
}

2. ABGCMIntentService#onHandleIntent()

View license
@Override
protected void onHandleIntent(Intent intent) {
    ABLog.d(TAG, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> onHandleIntent()");
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    Bundle extras = intent.getExtras();
    if (!extras.isEmpty()) {
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(intent);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(intent);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            sendNotification(intent);
        }
    }
    ABGCMBroadcastReceiver.completeWakefulIntent(intent);
}

3. GCMIntentService#onHandleIntent()

View license
@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging googleCloudMessaging = GoogleCloudMessaging.getInstance(this);
    String messageType = googleCloudMessaging.getMessageType(intent);
    if (extras != null && !extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            new ChatNotificationHelper(this).parseChatMessage(extras);
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

4. PushManager#tryRegisterPush()

Project: actor-platform
Source File: PushManager.java
View license
private String tryRegisterPush(Context context) {
    GoogleCloudMessaging cloudMessaging = GoogleCloudMessaging.getInstance(context);
    Log.d(TAG, "Requesting push token iteration...");
    try {
        return cloudMessaging.register("" + ActorSDK.sharedActor().getPushId());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

5. PushReceiver#onReceive()

Project: actor-platform
Source File: PushReceiver.java
View license
@Override
public void onReceive(Context context, Intent intent) {
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    Bundle extras = intent.getExtras();
    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            ActorSDK.sharedActor().waitForReady();
            if (extras.containsKey("seq")) {
                int seq = Integer.parseInt(extras.getString("seq"));
                Log.d(TAG, "Push received #" + seq);
                ActorSDK.sharedActor().getMessenger().onPushReceived(seq);
                setResultCode(Activity.RESULT_OK);
            } else if (extras.containsKey("callId")) {
                long callId = Long.parseLong(extras.getString("callId"));
                int attempt = 0;
                if (extras.containsKey("attemptIndex")) {
                    attempt = Integer.parseInt(extras.getString("attemptIndex"));
                }
                Log.d(TAG, "Received Call #" + callId + " (" + attempt + ")");
                ActorSDK.sharedActor().getMessenger().checkCall(callId, attempt);
                setResultCode(Activity.RESULT_OK);
            }
        }
    }
    WakefulBroadcastReceiver.completeWakefulIntent(intent);
}

6. GcmBroadcastReceiver#onReceive()

View license
@Override
public void onReceive(Context context, Intent intent) {
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);
    Bundle extras = intent.getExtras();
    if (!extras.isEmpty()) {
        /*
             * 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)) {
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            Apptentive.setPendingPushNotification(context, intent);
            Notification.Builder builder = new Notification.Builder(context);
            builder.setContentTitle(context.getString(R.string.support_message));
            builder.setContentText(context.getString(R.string.new_message));
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setAutoCancel(true);
            //builder.setContentIntent(PendingIntent.getBroadcast(context, 3531, new Intent("com.RSen.OpenMic.Pheonix.MESSAGE_OPENED"), 0));
            builder.setContentIntent(PendingIntent.getActivity(context, 123, new Intent(context, MainActivity.class), 0));
            ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(1232, builder.build());
        }
    }
}

7. CountlyMessagingService#onHandleIntent()

View license
@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
    String messageType = gcm.getMessageType(intent);
    if (extras != null && !extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            final Message msg = new Message(extras);
            if (msg.isValid()) {
                if (Countly.sharedInstance().isLoggingEnabled()) {
                    Log.i(TAG, "Got a message from Countly Messaging: " + msg);
                }
                // Send broadcast
                Intent broadcast = new Intent(CountlyMessaging.getBroadcastAction(getApplicationContext()));
                broadcast.putExtra(CountlyMessaging.BROADCAST_RECEIVER_ACTION_MESSAGE, msg);
                sendBroadcast(broadcast);
                // Init Countly in case app is not running
                if (!Countly.sharedInstance().isInitialized()) {
                    if (!CountlyMessaging.initCountly(getApplicationContext())) {
                        Log.e(TAG, "Cannot init Countly in background");
                    }
                }
                // Show message if not silent
                if (msg.isSilent()) {
                    CountlyMessaging.recordMessageOpen(msg.getId());
                } else {
                    // Go through proxy activity to be able to record message open & action performed events
                    Intent proxy = new Intent(getApplicationContext(), ProxyActivity.class);
                    proxy.putExtra(CountlyMessaging.EXTRA_MESSAGE, msg);
                    notify(proxy);
                }
            }
        }
    }
    CountlyMessaging.completeWakefulIntent(intent);
}

8. UpstreamFragment#doGcmSendUpstreamMessage()

Project: gcm
Source File: UpstreamFragment.java
View license
private void doGcmSendUpstreamMessage() {
    final Activity activity = getActivity();
    final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(activity);
    final String senderId = getValue(R.id.upstream_sender_id);
    final String msgId = getValue(R.id.upstream_message_id);
    final String ttl = getValue(R.id.upstream_ttl);
    final Bundle data = new Bundle();
    EditableMapView dataView = (EditableMapView) activity.findViewById(R.id.upstream_data);
    for (EditableMapView.MapEntry entry : dataView.getMapEntries()) {
        data.putString(entry.key, entry.value);
    }
    if (senderId.equals(getString(R.string.upstream_sender_id_hint))) {
        Toast.makeText(activity, R.string.upstream_sender_id_not_select, Toast.LENGTH_SHORT).show();
        return;
    }
    if (msgId.equals("")) {
        Toast.makeText(activity, R.string.upstream_message_id_not_provided, Toast.LENGTH_SHORT).show();
        return;
    }
    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            try {
                if (isNotEmpty(ttl)) {
                    try {
                        gcm.send(senderId + "@gcm.googleapis.com", msgId, Long.parseLong(ttl), data);
                    } catch (NumberFormatException ex) {
                        mLogger.log(Log.ERROR, "Error sending upstream message: could not parse ttl", ex);
                        return "Error sending upstream message: could not parse ttl";
                    }
                } else {
                    gcm.send(senderId + "@gcm.googleapis.com", msgId, data);
                }
                mLogger.log(Log.INFO, "Successfully sent upstream message");
                return null;
            } catch (IOException ex) {
                mLogger.log(Log.ERROR, "Error sending upstream message", ex);
                return "Error sending upstream message:" + ex.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                Toast.makeText(activity, "send message failed: " + result, Toast.LENGTH_LONG).show();
            }
        }
    }.execute(null, null, null);
}

9. GCMIntentService#onHandleIntent()

View license
@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()) {
        /*
             * 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.i(Consts.TAG, "onHandleIntent: message error");
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            Log.i(Consts.TAG, "onHandleIntent: message deleted");
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            String subId = intent.getStringExtra(GCM_KEY_SUBID);
            Log.i(Consts.TAG, "onHandleIntent: subId: " + subId);
            String[] tokens = subId.split(":");
            String typeId = tokens[1];
            // dispatch message
            if (GCM_TYPEID_QUERY.equals(typeId)) {
                Intent messageIntent = new Intent(BROADCAST_ON_MESSAGE);
                messageIntent.putExtras(intent);
                messageIntent.putExtra("token", tokens[2]);
                LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
            }
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

10. QBGCMRegistrationTask#performInBackground()

View license
@Override
public Bundle performInBackground(GoogleCloudMessaging... params) throws Exception {
    GoogleCloudMessaging gcm = params[0];
    Bundle registration = new Bundle();
    String registrationId = getRegistrationId(gcm);
    registration.putString(QBServiceConsts.EXTRA_REGISTRATION_ID, registrationId);
    boolean subscribed = subscribeToPushNotifications(registrationId);
    registration.putBoolean(QBServiceConsts.EXTRA_IS_PUSH_SUBSCRIBED_ON_SERVER, subscribed);
    return registration;
}

11. GcmBroadcastReceiver#onReceive()

View license
@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {
        /*
             * 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)) {
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            Log.i(TAG, "Received: " + extras.toString());
            if (extras.getString("event").equals("message")) {
                Intent broadcast = new Intent(BROADCAST);
                broadcast.putExtras(extras);
                context.sendOrderedBroadcast(broadcast, null);
            }
        }
    }
    setResultCode(Activity.RESULT_OK);
}