Search in sources :

Example 1 with GoogleCloudMessaging

use of com.google.android.gms.gcm.GoogleCloudMessaging in project baker-android by bakerframework.

the class GcmBroadcastReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (testing == false) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        receiveNotifications = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS, true);
        receiveNotificationsDownload = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS_DOWNLOAD, true);
        receiveNotificationsDownloadOnlyWifi = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS_DOWNLOAD_ONLY_WIFI, true);
        canDownload = !receiveNotificationsDownloadOnlyWifi | Configuration.connectionIsWiFi(context);
    }
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    messageType = gcm.getMessageType(intent);
    if (!receiveNotifications) {
        processFinishedCode = 1;
        return;
    }
    if (!extras.isEmpty()) {
        // has effect of unparcelling Bundle
        Log.i(this.getClass().toString(), "Received: " + 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(context, "Error notification", "Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(context, "Deleted messages", "Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            try {
                JSONObject json = new JSONObject(extras.getString("notificationData"));
                // Values can be either "standard-notification" or "background-download". This value is required.
                String type = json.getString("type");
                if ("standard-notification".equals(type)) {
                    // A title to show at the notification bar in android. This value is optional.
                    String title = json.has("title") ? json.getString("title") : "";
                    // The message description for the notification to show at the notifications bar in android. This value is optional.
                    String message = json.has("message") ? json.getString("message") : "";
                    this.sendNotification(context, title, message);
                } else if ("background-download".equals(type)) {
                    if (receiveNotificationsDownload) {
                        if (canDownload) {
                            processFinishedCode = 4;
                            if (json.has("issueName")) {
                                // Values can be "latest" or the name of the issue, for example "magazine-12". This value is required.
                                String issueName = json.getString("issueName");
                                Intent gindIntent = new Intent(context, GindActivity.class);
                                gindIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                gindIntent.putExtra("START_DOWNLOAD", issueName);
                                context.startActivity(gindIntent);
                            }
                        } else {
                            processFinishedCode = 3;
                            return;
                        }
                    } else {
                        processFinishedCode = 2;
                        return;
                    }
                }
            } catch (Exception ex) {
                // Do nothing, if it fails we simply do not process the notification.
                Log.e(this.getClass().toString(), ex.getMessage());
            }
        // Post notification of received message.
        //sendNotification(context, extras.getString("collapse_key"), extras.getString("message"));
        }
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging)

Example 2 with GoogleCloudMessaging

use of com.google.android.gms.gcm.GoogleCloudMessaging in project actor-platform by actorapp.

the class PushManager method tryRegisterPush.

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;
    }
}
Also used : GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging) IOException(java.io.IOException)

Example 3 with GoogleCloudMessaging

use of com.google.android.gms.gcm.GoogleCloudMessaging in project OneSignal-Android-SDK by OneSignal.

the class PushRegistratorGPS method registerInBackground.

private void registerInBackground(final String googleProjectNumber) {
    new Thread(new Runnable() {

        public void run() {
            boolean firedComplete = false;
            for (int currentRetry = 0; currentRetry < GCM_RETRY_COUNT; currentRetry++) {
                try {
                    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(appContext);
                    String registrationId = gcm.register(googleProjectNumber);
                    OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "Device registered, Google Registration ID = " + registrationId);
                    registeredHandler.complete(registrationId, 1);
                    break;
                } catch (IOException e) {
                    if (!"SERVICE_NOT_AVAILABLE".equals(e.getMessage())) {
                        OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error Getting Google Registration ID", e);
                        if (!firedComplete)
                            registeredHandler.complete(null, -11);
                        break;
                    } else {
                        if (currentRetry >= (GCM_RETRY_COUNT - 1))
                            OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "GCM_RETRY_COUNT of " + GCM_RETRY_COUNT + " exceed! Could not get a Google Registration Id", e);
                        else {
                            OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "Google Play services returned SERVICE_NOT_AVAILABLE error. Current retry count: " + currentRetry, e);
                            if (currentRetry == 2) {
                                // Retry 3 times before firing a null response and continuing a few more times.
                                registeredHandler.complete(null, -9);
                                firedComplete = true;
                            }
                            try {
                                Thread.sleep(10000 * (currentRetry + 1));
                            } catch (Throwable t) {
                            }
                        }
                    }
                } catch (Throwable t) {
                    OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error Getting Google Registration ID", t);
                    registeredHandler.complete(null, -12);
                    break;
                }
            }
        }
    }).start();
}
Also used : GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging) OSUtils.getResourceString(com.onesignal.OSUtils.getResourceString) IOException(java.io.IOException)

Example 4 with GoogleCloudMessaging

use of com.google.android.gms.gcm.GoogleCloudMessaging in project Android by hmkcode.

the class GcmMessageHandler method onHandleIntent.

@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);
    mes = extras.getString("title");
    showToast();
    Log.i("GCM", "Received : (" + messageType + ")  " + extras.getString("title"));
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Also used : Bundle(android.os.Bundle) GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging)

Example 5 with GoogleCloudMessaging

use of com.google.android.gms.gcm.GoogleCloudMessaging in project actor-platform by actorapp.

the class PushReceiver method onReceive.

@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"));
                int authId = Integer.parseInt(extras.getString("authId", "0"));
                Log.d(TAG, "Push received #" + seq);
                ActorSDK.sharedActor().getMessenger().onPushReceived(seq, authId);
                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);
}
Also used : Bundle(android.os.Bundle) GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging)

Aggregations

GoogleCloudMessaging (com.google.android.gms.gcm.GoogleCloudMessaging)6 Bundle (android.os.Bundle)3 IOException (java.io.IOException)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 OSUtils.getResourceString (com.onesignal.OSUtils.getResourceString)1 JSONObject (org.json.JSONObject)1