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?