Android 收到通知时如何在应用程序中打开mainactivity的webview
我正在使用GCM制作webview android应用程序 当我从服务器向android应用程序调用消息时 我可以在我的手机里得到完美的通知 如果单击,则可以在应用程序中看到Mainactivity 但我想改进一下Android 收到通知时如何在应用程序中打开mainactivity的webview,android,webview,notifications,google-cloud-messaging,push,Android,Webview,Notifications,Google Cloud Messaging,Push,我正在使用GCM制作webview android应用程序 当我从服务器向android应用程序调用消息时 我可以在我的手机里得到完美的通知 如果单击,则可以在应用程序中看到Mainactivity 但我想改进一下 比如说, GCM服务器发送一个特殊的url,然后如果我点击电话通知 我想在我的应用程序中检查webview的此url 我的消息来源是这样的 public class GcmMessageHandler extends IntentService { String mes;
比如说, 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));
}
});
}
}
}
}试试下面的一个
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());
}
我的应用程序不工作,我看不到“google.com”网站,当我点击alertdialog中的“ok”按钮时,只有我的应用程序启动了…webview显示了默认url我试图通过此修改,我也可以完美地获得相同的消息,但应用程序无法移动url,只需停留在mainactivity中…啊!现在我了解了意图,我尝试在mainactivity中创建意图,我成功了!非常感谢!!!
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();
}
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());
}