I implemented a PHP code to send a notification to multiple Registration IDs but after searching alot, i didn’t find an implementation to GCMIntentService & GCMBroadcastReciever that can show me how to implement it on my code
Here is the PHP code
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $apiKey,
//'data' => array( "message" => $message ),
'data' =>$message ,
);
$headers = array(
'Authorization: key=' . "AIzaSyBCzOGxXyvvo8-ZSzesOsTIYa-7Ua64_SM",
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
AND here is my Android class that i implemented
package guc.edu.iremote;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "===GCMIntentService===";
private static final String senderId = "559753615670";
String message;
public GCMIntentService() {
super(senderId);
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
// sets the app name in the intent
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", senderId);
startService(registrationIntent);
Log.i(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);
Log.i(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context context, Intent intent) {
intent.getStringExtra( "message" );
Intent notificationIntent=new Intent(context,this.getApplicationContext().getClass());
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
generateNotification(context, message, notificationIntent);
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message, Intent notificationIntent) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
notificationManager.notify(0, notification);
String title = context.getString(R.string.app_name);
// 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, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
@Override
protected void onError(Context arg0, String errorId) {
Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
}
SO can Anyone please help me with what’s wrong OR missing in my Android code?
You’ve got the regids and the API key the wrong way round in the PHP. It needs to be something like: