public
GcmIntentService()
{
super
(
"GcmIntentService"
);
}
@Override
protected
void
onHandleIntent(Intent intent)
{
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(
this
);
String messageType = gcm.getMessageType(intent);
if
(!extras.isEmpty())
{
if
(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
{
}
else
if
(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
{
}
else
if
(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
{
String msg = intent.getStringExtra(
"msg"
);
sendNotification(intent);
Log.i(
"GcmIntentService"
,
"Received: "
+ extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private
void
sendNotification(Intent intents)
{
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String title = intents.getStringExtra(
"title"
);
String msg = intents.getStringExtra(
"msg"
);
Intent intent =
new
Intent(getApplicationContext(), MainActivity.
class
);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(
"title"
, title);
intent.putExtra(
"msg"
, msg);
intent.putExtra(
"eventcode"
, intents.getStringExtra(
"eventcode"
));
PendingIntent contentIntent = PendingIntent.getActivity(
this
,
0
, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new
NotificationCompat.Builder(
this
).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setStyle(
new
NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setAutoCancel(
true
).setVibrate(
new
long
[] {
0
,
500
});
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}