Reputation: 2712
I am working on a project that include GCM as its main functionality.
I followed this AndroidHive.com for implementing GCM client and GCM server.
While running the html file in xampp server, I am getting this type of output on screen:
{"multicast_id":7075430686045407371,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{"message_id":"0:1394625413704378%93abcc37f9fd7ecd"}
]
}
Here I am not obtaining any type of message or notification on my Android device.
I don't have much knowledge about php, so I am not able to understand what is the matter.
Is there any problem with my php code or there is a problem in my Android code?
Upvotes: 1
Views: 292
Reputation: 7102
First off, have you tried your PHP script just to see if a connection to Google servers was successful? Try echoing your response and see if all is well on that end.
Second, you need to build your GCMBroadcastReceiver'java
client side to receive the messages:
package myApp.packageName;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.games.Notifications;
import com.google.android.gms.gcm.GoogleCloudMessaging;
/**
* Handling of GCM messages.
*/
public class GcmBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Intent intent)
{
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
{
sendNotification("MyNotificationTitle", "Send error: " + intent.getExtras().toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
{
sendNotification("MyNotificationTitle", "Deleted messages on server: " + intent.getExtras().toString());
}
else
{
String payload = intent.getStringExtra("payload");
String type = intent.getStringExtra("type");
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
}
// Put the GCM message into a notification and post it.
private void sendNotification(String title, String msg) {
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, myActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
In the onRecieve()
method try printing a message to the log, any message, just to see if the event triggered properly.
Upvotes: 1
Reputation: 23883
Make sure you have 3 things for your push notification to make it work.
Server side: Project Id(you can obtain from google cloud)
Client side: API key(from google cloud also) and registration number(your device ID)
Upvotes: 1