public
class
GCMIntentService
extends
GCMBaseIntentService
implements
NetDefine {
private
static
final
String TAG =
"GCM Tutorial::Service"
;
public
static
NextListener listener;
public
GCMIntentService() {
super
(SENDER_ID);
}
Setting mSetting;
@Override
protected
void
onMessage(Context context, Intent intent) {
mSetting =
new
Setting(context);
if
(intent.getAction().equals(
"com.google.android.c2dm.intent.RECEIVE"
)) {
String option_push = mSetting.getString(KEY_PUSHCH,
""
);
if
(
"true"
.equals(option_push)) {
showMessage(context, intent);
}
}
}
@Override
protected
void
onError(Context context, String msg) {
Log.w(TAG,
"onError!! "
+ msg);
}
@Override
protected
void
onRegistered(Context context, String regID) {
if
(listener !=
null
) {
listener.next();
listener =
null
;
}
mSetting =
new
Setting(context);
mSetting.input(KEY_GCM_REGI_ID, regID);
}
@Override
protected
void
onUnregistered(Context context, String regID) {
Log.w(TAG,
"onUnregistered!! "
+ regID);
}
public
void
showToast() {
Toast.makeText(
this
,
"RegID 등록 완료"
, Toast.LENGTH_SHORT).show();
}
private
void
showMessage(Context context, Intent intent) {
String title = intent.getStringExtra(KEY_TITLE);
String msg = intent.getStringExtra(KEY_MSG);
String section = intent.getStringExtra(KEY_SECTION);
String category = intent.getStringExtra(KEY_CATEGORY);
String no = intent.getStringExtra(KEY_NO);
String fairurl = intent.getStringExtra(KEY_FAIRURL);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Activity.NOTIFICATION_SERVICE);
PendingIntent pendingIntent;
String url =
"section="
+ section +
"&category="
+ category +
"&no="
+ no;
int
titleBar = DataManager.getCodeModel(Integer.valueOf(category)).getTitleText();
pendingIntent = PendingIntent.getActivity(
context,
0
,
new
Intent(context, DetailNewsViewActivity.
class
)
.putExtra(KEY_SUB_URL, url)
.putExtra(KEY_SECTION, section)
.putExtra(KEY_CATEGORY, category)
.putExtra(KEY_TITLEBAR, titleBar)
.putExtra(KEY_CHANNEL,
true
)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification =
new
Notification();
notification.icon = R.drawable.icon;
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, title, msg, pendingIntent);
notificationManager.notify(
0
, notification);
Intent i =
new
Intent(context, GCMMessageView.
class
);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra(KEY_TITLEBAR, titleBar);
i.putExtra(KEY_MSG, msg);
i.putExtra(KEY_SUB_URL, url);
i.putExtra(KEY_SECTION, section);
i.putExtra(KEY_CATEGORY, category);
i.putExtra(KEY_FAIRURL, fairurl);
context.startActivity(i);
{
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final
PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP,
"GCM_PUSH"
);
mWakelock.acquire();
Timer timer =
new
Timer();
TimerTask task =
new
TimerTask() {
public
void
run() {
mWakelock.release();
}
};
vibe.vibrate(
1000
);
timer.schedule(task,
5000
);
}
}