1
votes

So I have a simple Ruby app that uses Rpush to send push notifications to my iphone and my android phone. Right now all it does is sends it to the iphone. I am not sure whether the problem is with my script (i.e. incorrect values for the registration_id, app_name, or auth_key), or whether the problem is how I have my Android app configured.

The relevant part of that code is here (values changed for security - but format/length of keys left untouched to make sure they "look right" to people with experience)

API SETUP/RATIONALE (Sending the notification)

# GCM
app = Rpush::Gcm::App.new
app.name = "MyApp"
app.auth_key = "POfaSyfghilK3l-ueSvRLmbawcRThCWkwmcYGeM"
app.connections = 1
app.save

n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("MyApp")
n.registration_ids = ["derGV80JK-s:APA91bHgerskBnrhHndes947nMKxI116tC3-k-tGd-hT5NzVc8QAWEkvCrMwrvs78RUL8-vvhp2ultoevqzZnn8gsr9t6WDXDYpGfCliqaJzj0XByBgbi0bm-rYufjcxfCc_5lEL381F"]
n.data = { message: "testing!" }
n.save!

Rpush.push

I determined that the name of my app was "MyApp" by looking at my google developer console here and noticing that the "Project Name" of the desired project is "MyApp".

I determined the Auth Key on the same site, by navigating to API & Auth -> Credentials -> API Key and copy/pasting the API key from there.

I determined my device's registration id using this code in the main activity of my Android App:

public static String getDeviceID(Context context) {
    final TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, tmPhone, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "";// + tm.getSimSerialNumber();
    androidId = ""
            + android.provider.Settings.Secure.getString(
            context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(),
            ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    String deviceId = deviceUuid.toString();

    return deviceId;
}

When logged, getDeviceID shows me the registration id that I specified in the above ruby code.

APP SETUP/RATIONALE (Receiving the notification)

First, I set up my Android Manifest to have all the necessary permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.johncorser.myapp.permission.RECEIVE" />
<permission android:protectionLevel="signature"
    android:name="com.johncorser.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.johncorser.myapp.permission.C2D_MESSAGE" />

Then, I set up a listener service to react to push notifications:

    <service
        android:name="com.johncorser.myapp.services.GcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

That class is very simple and looks like this:

public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d("push", "From: " + from);
        Log.d("push", "Message: " + message);
    }
}

I would expect these messages to log out after sending the push notifications. But instead nothing happens (no exceptions thrown on the server or app).

Any one see what I'm doing wrong here?

3

3 Answers

2
votes

Why are you using the UUID you generated from the Android Device ID as a registration ID for Google Cloud Messaging? That's NOT how you get a registration id.

To get a registration ID you have to register with GCM on the device and receive back a registration ID/token, as described in Cloud Messaging for Android Quickstart:

InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

In order for the above code to work you need to have a google-services.json configuration file (which is parsed by the com.google.gms.google-services gradle plugin, that you also need) in your app/ directory and you need the gcm_defaultSenderId which is the Project ID (Number) you get from the Google Developers Console.

You can easily generate that file and receive the above details by clicking the button "Get a configuration file" and following the steps mentioned there.

The code to get the registration ID needs to be in an IntentService as described here, and you need to define the service in the AndroidManifest.xml file as outlined here
For GCM to be able to communicate with your app, you also need to define in the manifest file a com.google.android.gms.gcm.GcmReceiver which has an intent filter with an action name "com.google.android.c2dm.intent.RECEIVE" and a category name with your package name. Look here for an example.

0
votes

Try running the sample here by Google themselves.

https://developers.google.com/cloud-messaging/android/start https://github.com/googlesamples/google-services/tree/master/android/gcm

I feel like something might just be missing in your manifest file.

Hope that helps.

0
votes

In case anyone else runs in to this, the problem was that I did not whitelist my IP address on developers.google.com. I now have it set so all IPs are whitelisted, and it works like a charm.