Last active
August 29, 2015 14:01
-
-
Save rachitmishra/36ab4a8ce6a3a5b6c2fb to your computer and use it in GitHub Desktop.
Connecting with GCM, and getting Registration ID.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Rachit Mishra | |
* @licence The MIT License (MIT) Copyright (c) <2013> <Rachit Mishra> | |
* | |
*/ | |
import android.app.IntentService; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v4.app.NotificationCompat; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
public class CloudProcessor extends IntentService { | |
public CloudProcessor() { | |
super("CloudProcessor"); | |
} | |
public static final int NOTIFICATION_ID = 1; | |
private NotificationManager mNotificationManager; | |
private GoogleCloudMessaging gcm; | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
Bundle extras = intent.getExtras(); | |
gcm = GoogleCloudMessaging.getInstance(this); | |
String messageType = gcm.getMessageType(intent); | |
if (!extras.isEmpty()) { | |
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR | |
.equals(messageType)) { | |
sendNotification("Send error: " + extras.toString()); | |
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED | |
.equals(messageType)) { | |
sendNotification("Deleted messages on server: " | |
+ extras.toString()); | |
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE | |
.equals(messageType)) { | |
sendNotification("Deleted messages on server: " | |
+ extras.toString()); | |
} | |
} | |
CloudReceiver.completeWakefulIntent(intent); | |
} | |
private void sendNotification(String msg) { | |
mNotificationManager = (NotificationManager) this | |
.getSystemService(Context.NOTIFICATION_SERVICE); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, | |
new Intent(this, Home.class), 0); | |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( | |
this).setSmallIcon(R.drawable.ic_launcher) | |
.setContentTitle("GCM Notification") | |
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) | |
.setContentText(msg); | |
mBuilder.setContentIntent(contentIntent); | |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v4.content.WakefulBroadcastReceiver; | |
public class CloudReceiver extends WakefulBroadcastReceiver { | |
public CloudReceiver() { | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
ComponentName component = new ComponentName(context.getPackageName(), | |
CloudProcessor.class.getName()); | |
startWakefulService(context, (intent.setComponent(component))); | |
setResultCode(Activity.RESULT_OK); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private GoogleCloudMessaging gcm; | |
public void setupGoogleCloudMessaging() { | |
new AsyncTask<Void, Void, Boolean>() { | |
private String registrationId; | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
try { | |
if (gcm == null) { | |
gcm = GoogleCloudMessaging.getInstance(Home.this); | |
} | |
registrationId = gcm.register(SENDER_ID); | |
// save registration id to Shared preferences. | |
return true; | |
} catch (IOException ex) { | |
return false; | |
} | |
} | |
@Override | |
protected void onPostExecute(Boolean result) { | |
// Do something if registered successfully. | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment