I created an application that uses GCM functionality. When I send a message from the server to the device in response, I get the message identifier. After sending a message from the server, when I start the application again, I get a response on the device. It does not come as a notification, such as a GMAIL notification after the application is installed, when the server sends a message on which the notification will be displayed. I am sending the complete server and client code. Please help me solve the problem.
The problem I received is:. When I run the Android application again, what I sent from the server side, it displays a message, but I want it to be like a notification that I am sending from the server ...
Server side code
@WebServlet("/GCMBroadcast") public class GCMBroadcast extends HttpServlet { private static final long serialVersionUID = 1L; private static final String SENDER_ID = ""; private static final String ANDROID_DEVICE = ""; private List<String> androidTargets = new ArrayList<String>(); public GCMBroadcast() { super(); androidTargets.add(ANDROID_DEVICE); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String collapseKey = ""; String userMessage = ""; try { userMessage = request.getParameter("Message"); collapseKey = request.getParameter("CollapseKey"); } catch (Exception e) { e.printStackTrace(); return; } Sender sender = new Sender(SENDER_ID); Message message = new Message.Builder() .collapseKey(collapseKey) .timeToLive(30) .delayWhileIdle(true) .addData("message", userMessage) .build(); try { MulticastResult result = sender.send(message, androidTargets, 1); System.out.println("Response: " + result.getResults().toString()); if (result.getResults() != null) { int canonicalRegId = result.getCanonicalIds(); if (canonicalRegId != 0) { System.out.println("response " +canonicalRegId ); } } else { int error = result.getFailure(); System.out.println("Broadcast failure: " + error); } } catch (Exception e) { e.printStackTrace(); } request.setAttribute("CollapseKey", collapseKey); request.setAttribute("Message", userMessage); request.getRequestDispatcher("index.jsp").forward(request, response); } }
Demo Activity Code
public class DemoActivity extends Activity { 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 static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; String SENDER_ID = ""; static final String TAG = "GCM Demo"; TextView mDisplay; GoogleCloudMessaging gcm; AtomicInteger msgId = new AtomicInteger(); Context context; String regid; @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDisplay = (TextView) findViewById(R.id.display); context = getApplicationContext();
GCMBroadCastReciver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
GCMIntentServicve
public class GcmIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; public GcmIntentService() { super("GcmIntentService"); } public static final String TAG = "GCM Demo"; @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newgcmproject" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <permission android:name="com.example.newgcmproject.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.newgcmproject.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".DemoActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|screenSize" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <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="com.google.android.gcm.demo.app" /> </intent-filter> </receiver> <service android:name=".GcmIntentService" /> </application> </manifest>
I do not receive any notifications on my device, even if I receive a message identifier when sending from the server side.
Edit Now I have made some changes to my code. I get an exception that the service is unavailable