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());
// textView1 = (TextView)layout.findViewById(R.id.textView1);
// textView1.setText(value);
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);
// long[] pattern = {1000, 200, 1000, 2000, 1200}; // 진동, 무진동, 진동 무진동 숫으로 시간을 설정한다.
// vibe.vibrate(pattern, 4); // 패턴을 지정하고 반복횟수를 지정
vibe.vibrate(1000); //1초 동안 진동이 울린다.
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);
// 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, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
GCMIntentService 에는 이렇게 소스가 들어가있습니다.
제가 하려고 하는것은 gcm 메세지를 받게되면 toast를 띄우는 것인데 일반 toast말고 customtoast를 사용하려고 합니다.
하지만
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup)findViewById(R.id.toast_layout_root));
이 부분에서 null이 들어가서 사용이 불가능한데 다른방법이 뭐가 있을지 한마디씩 부탁드리겠습니다.