Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For GCM implementation. Create a new project and add GCM.jar file to its buildpath. Generate the Manifiest file as per the below.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="YOUR PACKAGE NAME" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;permission android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt; &lt;uses-permission android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE" /&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.USE_CREDENTIALS" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".DemoGCMPush" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" &gt; &lt;intent-filter&gt; &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt; &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt; &lt;category android:name="YOUR PACKAGE NAME" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name="YOUR PACKAGE NAME.GCMIntentService" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>Then in your main activity add the following code to register. replace the SENDER_ID with the 12 digit project Id from the URL.</p> <pre><code>GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, SENDER_ID); } else { Log.v(TAG, "Already registered"); } </code></pre> <p>and finally add second class of GCMIntentService.java for handling GCM messages.</p> <pre><code>import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.android.gcm.GCMBaseIntentService; /** * {@link IntentService} responsible for handling GCM messages. */ public class GCMIntentService extends GCMBaseIntentService { @SuppressWarnings("hiding") private static final String TAG = "GCMIntentService"; public GCMIntentService() { super("SENDER ID"); } /** * Issues a notification to inform the user that server has sent a message. */ private static void generateNotification(Context context, String message) { long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, message, when); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, SamplePushActivity.class); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); } @Override protected void onError(Context arg0, String arg1) { // TODO Auto-generated method stub } @Override protected void onMessage(Context arg0, Intent arg1) { Log.d("GCM", "RECIEVED A MESSAGE"); // Get the data from intent and send to notificaion bar generateNotification(arg0, arg1.getStringExtra("message")); } @Override protected void onRegistered(Context arg0, String arg1) { // TODO Auto-generated method stub } @Override protected void onUnregistered(Context arg0, String arg1) { // TODO Auto-generated method stub } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload