I am trying to do push notification in android using GCM. I read the Google docs for GCM and their demo application. I created the client side program mentioned here http://android.amolgupta.in/
. But i am not getting registration ID. Also I am not getting some points like:
Can any one provide me proper project other than google provided so that i clear my concepts.
Any help will be appreciated.
A Registration ID is an identifier assigned by GCM to a single instance of a single application installed on an Android device. The device is assigned this identifier when it registers to Google Cloud Messaging. The GCM documentation doesn't specify what information is encoded in this identifier.
To receive the Device Token (and updates to the token value) and push notifications, you must create a custom class that extends FirebaseMessagingService . The onNewToken callback fires whenever a new token is generated.
The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app.
Here I have written a few steps for How to Get RegID and Notification starting from scratch
You can find a complete tutorial here:
Code snippet to get Registration ID (Device Token for Push Notification).
Configure project for GCM
To enable GCM in our project we need to add a few permissions to our manifest file. Go to AndroidManifest.xml
and add this code: Add Permissions
<uses-permission android:name="android.permission.INTERNET”/> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name=“.permission.RECEIVE" /> <uses-permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE" /> <permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
Add GCM Broadcast Receiver declaration in your application tag:
<application <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" ]]> <intent-filter]]> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="" /> </intent-filter]]> </receiver]]> <application/>
Add GCM Service declaration
<application <service android:name=".GcmIntentService" /> <application/>
Now Go to your Launch/Splash Activity
Add Constants and Class Variables
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; public static final String EXTRA_MESSAGE = "message"; public static final String PROPERTY_REG_ID = "registration_id"; private static final String PROPERTY_APP_VERSION = "appVersion"; private final static String TAG = "LaunchActivity"; protected String SENDER_ID = "Your_sender_id"; private GoogleCloudMessaging gcm =null; private String regid = null; private Context context= null;
Update OnCreate and OnResume methods
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); context = getApplicationContext(); if (checkPlayServices()) { gcm = GoogleCloudMessaging.getInstance(this); regid = getRegistrationId(context); if (regid.isEmpty()) { registerInBackground(); } else { Log.d(TAG, "No valid Google Play Services APK found."); } } } @Override protected void onResume() { super.onResume(); checkPlayServices(); } // # Implement GCM Required methods(Add below methods in LaunchActivity) private boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Log.d(TAG, "This device is not supported - Google Play Services."); finish(); } return false; } return true; } private String getRegistrationId(Context context) { final SharedPreferences prefs = getGCMPreferences(context); String registrationId = prefs.getString(PROPERTY_REG_ID, ""); if (registrationId.isEmpty()) { Log.d(TAG, "Registration ID not found."); return ""; } int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion) { Log.d(TAG, "App version changed."); return ""; } return registrationId; } private SharedPreferences getGCMPreferences(Context context) { return getSharedPreferences(LaunchActivity.class.getSimpleName(), Context.MODE_PRIVATE); } private static int getAppVersion(Context context) { try { PackageInfo packageInfo = context.getPackageManager() .getPackageInfo(context.getPackageName(), 0); return packageInfo.versionCode; } catch (NameNotFoundException e) { throw new RuntimeException("Could not get package name: " + e); } } private void registerInBackground() { new AsyncTask() { @Override protected Object doInBackground(Object...params) { String msg = ""; try { if (gcm == null) { gcm = GoogleCloudMessaging.getInstance(context); } regid = gcm.register(SENDER_ID); Log.d(TAG, "########################################"); Log.d(TAG, "Current Device's Registration ID is: " + msg); } catch (IOException ex) { msg = "Error :" + ex.getMessage(); } return null; } protected void onPostExecute(Object result) { //to do here }; }.execute(null, null, null); }
Note : please store REGISTRATION_KEY
, it is important for sending PN Message to GCM. Also keep in mind: this key will be unique for all devices and GCM will send Push Notifications by REGISTRATION_KEY
only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With