-
-
Save hkmushtaq/4d6f7903c01df12b0766 to your computer and use it in GitHub Desktop.
BroadcastReceiver Class Android GCM
This file contains 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
package com.loopedllc.supapp; | |
import java.util.Date; | |
import java.util.Random; | |
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.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.app.TaskStackBuilder; | |
import android.util.Log; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
import com.google.gson.Gson; | |
import com.loopedllc.functionality.Constants; | |
import com.loopedllc.supapp.models.Prompt; | |
public class SUPBroadcastReceiver extends BroadcastReceiver { | |
private Context context; | |
private static final String TAG = "SUPBroadcastReceiver"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); | |
String messageType = gcm.getMessageType(intent); | |
Bundle extras = intent.getExtras(); | |
this.context = context; | |
// Filter messages based on message type. It is likely that GCM will be extended in the future | |
// with new message types, so just ignore message types you're not interested in, or that you | |
// don't recognize. | |
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { | |
Log.i(TAG, "Error Message Received from GCM"); | |
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { | |
Log.i(TAG, "Some messages were deleted by GCM"); | |
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { | |
Log.i(TAG, "Got an actual push notification"); | |
Log.i(TAG, "Received the following: " + extras.toString()); | |
sendNotification(extras); | |
} | |
} | |
private void sendNotification(Bundle extras) { | |
String senderUserName = extras.getString(Constants.requests.SENDER_USER_NAME); | |
String promptID = extras.getString(Constants.requests.PROMPT_ID); | |
String senderFacebookID = extras.getString(Constants.requests.SENDER_FACEBOOK_ID); | |
String receiverUserName = extras.getString(Constants.requests.RECEIVER_USER_NAME); | |
int messageType = Integer.parseInt(extras.getString(Constants.requests.MESSAGE_TYPE)); | |
Prompt newPrompt = new Prompt(); | |
newPrompt.setPromptID(promptID); | |
newPrompt.setMessageType(String.valueOf(messageType)); | |
newPrompt.setSenderFacebookID(senderFacebookID); | |
newPrompt.setSenderUserName(senderUserName); | |
newPrompt.setReceiverUserName(receiverUserName); | |
newPrompt.setTimestamp(new Date().getTime()); | |
Gson gson = new Gson(); | |
String promptString = gson.toJson(newPrompt, Prompt.class); | |
String message = senderUserName + | |
" says " + | |
Constants.MESSAGES[messageType]; | |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context) | |
.setSmallIcon(R.drawable.ic_launcher) | |
.setContentTitle("'Sup") | |
.setContentText(message); | |
Intent resultIntent = new Intent(this.context, RootActivity.class); | |
resultIntent.putExtra("promptToOpen", promptString); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.context); | |
// Adds the back stack for the Intent (but not the Intent itself) | |
stackBuilder.addParentStack(RootActivity.class); | |
// Adds the Intent that starts the Activity to the top of the stack | |
stackBuilder.addNextIntent(resultIntent); | |
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( | |
0, | |
PendingIntent.FLAG_ONE_SHOT | |
); | |
mBuilder.setContentIntent(resultPendingIntent); | |
mBuilder.setAutoCancel(true); | |
Uri soundUri = Uri.parse("android.resource://" + | |
this.context.getPackageName() + | |
"/" + | |
Constants.SOUND_FILES[messageType]); | |
mBuilder.setSound(soundUri); | |
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); | |
NotificationManager mNotificationManager = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE); | |
mNotificationManager.notify(new Random(new Date().getTime()).nextInt(), mBuilder.build()); | |
//mNotificationManager.notify(null, mBuilder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment