如何在接收通知时在应用程序中打开mainview的webview

时间:2015-11-21 07:24:01

标签: android webview notifications google-cloud-messaging push

我正在使用GCM制作webview android应用程序

当我将消息从服​​务器调用到android应用程序

我可以在手机中收到完整的通知。

如果我点击,那么我可以在我的应用程序中看到Mainactivity。

但我希望改进这个,

例如,

GCM服务器发送一个特殊的网址,然后如果我点击来自手机的通知,

我想在我的应用程序中查看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();
}

}

1 个答案:

答案 0 :(得分:2)

尝试以下一个..

 private void sendNotification(String msg, String url)
    {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent m_intent = new Intent(this, MainActivity.class);
        m_intent.putExtra("url", url); // Receive this in main activity in bundle
        m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, m_intent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notification_small).setContentTitle(getString(R.string.app_name)).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setAutoCancel(true).setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }