Push notification reaching ios (apns) but not android (gcm) device -
so have simple ruby app uses rpush send push notifications iphone , android phone. right sends iphone. not sure whether problem script (i.e. incorrect values registration_id
, app_name
, or auth_key
), or whether problem how have android app configured.
the relevant part of code here (values changed security - format/length of keys left untouched make sure "look right" people experience)
api setup/rationale (sending 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 name of app "myapp" looking @ google developer console here , noticing "project name" of desired project "myapp".
i determined auth key on same site, navigating api & auth -> credentials -> api key
, copy/pasting api key there.
i determined device's registration id using code in main activity of 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 registration id specified in above ruby code.
app setup/rationale (receiving notification)
first, set android manifest have 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, set listener service react 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 simple , looks 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 expect these messages log out after sending push notifications. instead nothing happens (no exceptions thrown on server or app).
any 1 see i'm doing wrong here?
why using uuid generated android device id registration id google cloud messaging? that's not how registration id.
to registration id have register gcm on device , receive registration id/token, described in cloud messaging android quickstart:
instanceid instanceid = instanceid.getinstance(this); string token = instanceid.gettoken(getstring(r.string.gcm_defaultsenderid), googlecloudmessaging.instance_id_scope, null);
in order above code work need have google-services.json configuration file (which parsed com.google.gms.google-services gradle plugin, need) in app/ directory , need gcm_defaultsenderid project id (number) google developers console.
you can generate file , receive above details clicking button "get configuration file" , following steps mentioned there.
the code registration id needs in intentservice described here, , need define service in androidmanifest.xml file outlined here
gcm able communicate app, need define in manifest file com.google.android.gms.gcm.gcmreceiver has intent filter action name "com.google.android.c2dm.intent.receive" , category name package name. here example.
Comments
Post a Comment