public
class
GCMIntentService
extends
GCMBaseIntentService {
@SuppressWarnings
(
"hiding"
)
private
static
final
String TAG =
"GCMIntentService"
;
public
GCMIntentService() {
super
(SENDER_ID);
}
@Override
protected
void
onMessage(Context context, Intent intent) {
Log.i(TAG,
"Received message"
);
TextView textView1;
Bundle b = intent.getExtras();
Iterator<String> iterator = b.keySet().iterator();
String key = iterator.next();
String value = b.get(key).toString();
if
(key.equals(
"msg"
)) {
Log.d(TAG,
"onMessage. "
+key+
" : "
+value);
}
displayMessage(context, value);
generateNotification(context, value);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup)findViewById(R.id.toast_layout_root));
Toast toast =
new
Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM,
0
,
0
);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
switch
(audioManager.getRingerMode()) {
case
AudioManager.RINGER_MODE_VIBRATE:
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(
1000
);
break
;
case
AudioManager.RINGER_MODE_NORMAL:
Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);
ringtone.play();
break
;
case
AudioManager.RINGER_MODE_SILENT:
break
;
}
}
private
static
void
generateNotification(Context context, String message) {
int
icon = R.drawable.ic_stat_gcm;
long
when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification =
new
Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent =
new
Intent(context, VTPMsMain_Activity.
class
);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context,
0
, notificationIntent,
0
);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(
0
, notification);
}