我正在使用GCM制作webview Android应用程序。
当我从服务器调用消息到Android应用程序时,
我可以在手机上完美地获得通知。
如果我点击通知,那么我就可以在我的应用程序中看到MainActivity。
但是我想改进这个功能,
例如,
GCM服务器发送一个特殊的URL,那么如果我从手机接收到通知并点击它,
我希望在我的应用程序中检查此Webview的URL。
我的源代码如下...
public class GcmMessageHandler extends IntentService {
String mes;
String cont;
String title_name ;
String board;
String idx;
String pg;
private Handler handler;
private Bitmap bmBigPicture;
private WebView webview;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
bmBigPicture = BitmapFactory.decodeResource(getResources(), R.drawable.message);
mes = extras.getString("title");
cont = extras.getString("message");
String msg = cont;
title_name = msg.substring(msg.indexOf("@@@")+3,msg.length()); //"title_name"
board = msg.substring(0, msg.indexOf("!!!")); //idx
idx = msg.substring(msg.indexOf("!!!")+3, msg.indexOf("///")); //idx
pg = msg.substring(msg.indexOf("///")+3,msg.indexOf("@@@")); //"pg"
if(extras.getString("title") != null){
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null;
PendingIntent intent1 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
notification = new Notification.BigPictureStyle(
new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(bmBigPicture)
.setAutoCancel(true)
.setVibrate(new long[] { 1000, 1000, 1000 })
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setContentIntent(intent1)
.setContentTitle(title_name))
.bigPicture(bmBigPicture)
.build();
manager.notify(1234, notification);
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title") );
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), title_name , Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(),MyDialog.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("title", cont));
}
});
}
public class MainActivity extends ActionBarActivity {
GoogleCloudMessaging gcm;
String regid;
String gcmMsg;
String tag = "tag";
String androidId;
String DeviceId;
ImageButton button1;
ImageButton button2;
ImageButton button3;
ImageButton button4;
ImageButton button5;
private static ProgressDialog pd;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//set up full screen
setContentView(R.layout.activity_main);
setLayout();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
pd = ProgressDialog.show(MainActivity.this, null, "Loading...Please Wait");
// WebViewClient
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl("http:/xxxaa.cafe24.com/androidboard/user.jsp");
button1 = (ImageButton)findViewById(R.id.home);
button2 = (ImageButton)findViewById(R.id.title);
button3 = (ImageButton)findViewById(R.id.menu);
button4 = (ImageButton)findViewById(R.id.board1);
button5 = (ImageButton)findViewById(R.id.board2);
button1.setOnClickListener(new OnClickListener() { // top1
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");
// WebViewClient
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl("http://xxxaa.cafe24.com/androidboard/user.jsp");
button4.setBackgroundResource(R.drawable.board1_on);
button5.setBackgroundResource(R.drawable.board2_off);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//quickAction.show(v);
}
});
button4.setOnClickListener(new OnClickListener() { // board1
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");
// WebViewClient
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl("http://xxxaa.cafe24.com/androidboard/user.jsp");
button4.setBackgroundResource(R.drawable.board1_on);
button5.setBackgroundResource(R.drawable.board2_off);
}
});
button5.setOnClickListener(new OnClickListener() { // board2
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");
// WebViewClient
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl("http://xxxaa.cafe24.com/androidboard/user1.jsp");
button4.setBackgroundResource(R.drawable.board1_off);
button5.setBackgroundResource(R.drawable.board2_on);
}
});
}
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(pd.isShowing()) {
pd.dismiss();
}
}
public void onLoadResource (WebView view, String url) {
if (pd == null) {
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Chargement en cours");
pd.show();
}
}
}
private void setLayout(){
mWebView = (WebView) findViewById(R.id.webview);
}
public class MyDialog extends Activity {
public String idx = "";
public String pg = "";
public String title_name ="";
public String board = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String title = intent.getExtras().getString("title");
// board + "!!!" + idx + "///" + pg + "@@@" + vo.getTitle()
// String temp1 = title.substring(title.indexOf("///"),title.indexOf("///")+3); // "/// "
board = title.substring(0, title.indexOf("!!!")); //idx
idx = title.substring(title.indexOf("!!!")+3, title.indexOf("///")); //idx
pg = title.substring(title.indexOf("///")+3,title.indexOf("@@@")); //"pg"
title_name = title.substring(title.indexOf("@@@")+3,title.length()); //"title_name"
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(MyDialog.this);
myAlertDialog.setTitle(title_name);
myAlertDialog.setIcon(R.drawable.icon);
//myAlertDialog.setMessage(title_name);
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do something when the OK button is clicked
//Intent i = new Intent(Intent.ACTION_VIEW, Uri
// .parse("http://xxxaa.cafe24.com/androidboard/userview" + board + ".jsp?idx=" + idx + "&pg=" + pg));
//i.setComponent(new ComponentName("com.example.notitle","com.example.notitle.MainActivity"));
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(i);
Intent internetIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
internetIntent.setComponent(new ComponentName("com.example.notitle","com.example.notitle.MainActivity"));
internetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(internetIntent);
}});
myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do something when the Cancel button is clicked
}});
myAlertDialog.show();
}
}