2

I followd android's GCM guide and it's is written there:

When onClick() calls gcm.send(), it triggers the broadcast receiver's onReceive() method, which has the responsibility of making sure that the GCM message gets handled.

Now, from what I understand gcm.send() is beeing called when doing upstream messaging.
But what if I don't want to use it? I just want messages from the cloud to the device and not the other way around. Is it possible and if so how do I

trigger the broadcast receiver's onReceive() method

?

GM6
  • 313
  • 1
  • 5
  • 14

1 Answers1

0

No, you don't need to. Messages are not sent by the device unless you want a 2 way communication.

So, if you only need to receive push messages, reception of a message is managed by onReceive() and after that for GcmIntentService() public class using method onHandleIntent() but your device has to meet some basic configurations before:

a) Must to be registered to Google and in your 3rd party app server b) The device must to be connected to Internet c) The device must to have intalled Google Play Services

To send a message you need a 3rd party server app to store registration_ids and to send message to Google's endpoint who takes the job to send the message under specific conditions to all registration_ids you provided.

This is an example of my build.gradle including Google Play Services to have access to GoogleCloudMessaging class:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.1'
    }
}

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

dependencies {
    compile project(':libraries:AmbilWarna')
    compile files('libs/libGoogleAnalyticsServices.jar')
    compile files('libs/commons-net-3.3.jar')
    compile files('libs/YouTubeAndroidPlayerApi.jar')
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.android.support:cardview-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'org.jsoup:jsoup:1.8.1@jar'
    compile 'joda-time:joda-time:2.4@jar'
    compile 'com.google.android.gms:play-services:6.1.71'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'ch.acra:acra:4.5.0'
    compile 'net.rdrei.android.dirchooser:library:2.1@aar'
    compile 'com.uwetrottmann:trakt-java:3.3.1'

}


def getVersionCode = { ->
    try {
        def code = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'log', '--oneline'
            standardOutput = code
        }
        return code.toString().split("\n").size()
    }
    catch (ignored) {
        return -1;
    }
}

android {
    signingConfigs {

        release {
            keyAlias '***'
            keyPassword '+++'
            storeFile file('/home/***')
            storePassword '***'
        }
    }

    compileSdkVersion 21
    buildToolsVersion '21.1'
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    defaultConfig {
        versionCode 30000+getVersionCode()
        versionName "3.0." + getVersionCode()
        signingConfig signingConfigs.release
    }
    lintOptions {
        abortOnError false
    }
    productFlavors {
    }
}

More detailed info of all topic(please ignore XMPP and gcm.send methods): Full GCM Client implementation

Martin Revert
  • 3,242
  • 2
  • 30
  • 33
  • Thanks, that's what I thought. You mentioned the method `onMessage` which is part of the `gcmbaseintentservice` class. However it's seems like the class is not recognized. Do you have any idea why? btw I'm using `android studio` if it matters. – GM6 Nov 08 '14 at 23:30
  • Oh, yes, i'm sorry. You're right. New implementation is no more an independent library and is part of Google Play Services. – Martin Revert Nov 08 '14 at 23:37
  • The correct method to manage the Intent awaked from Google is getMessageType() which is part of GoogleCloudMessaging class. Please check this link where a complete implementation is detailed. If you have more questions, please feel free to ask. https://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html. See my EDIT above on how to include Google Play Services dependency on your build.gradle file under Android Studio. – Martin Revert Nov 08 '14 at 23:44
  • Thanks Martin. I'm still having some trouble with the `GCM` implemantation. Please see my other question http://stackoverflow.com/questions/26855160/what-causses-gcm-server-to-return-notregistered?noredirect=1#comment42277196_26855160 and maybe you could help – GM6 Nov 11 '14 at 09:02