所以基本上我只是 Android 的新手,我正在制作一个应用程序,它发送实时通知,该通知是从服务器用 php 发送的。
现在我希望当用户点击通知时在我的 webview 中打开一个网页。 它是一个基于网络的应用程序。
我使用 3 个选项卡在 3 个 fragment 之间切换。 每个 fragment 都有不同的 WebView 。
现在这是我的 GCMintentService.java:
package blah.blah;
import static blah.blah.CommonUtilities.SENDER_ID;
import static blah.blah.CommonUtilities.displayMessage;
import blah.blah.R;
import blah.blah.R.drawable;
import blah.blah.R.string;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Toestel geregistreerd: regId = " + registrationId);
displayMessage(context, "Je toestel is geregistreerd");
Log.d("NAME", NotificationMain.name);
ServerUtilities.register(context, NotificationMain.name, NotificationMain.klas, registrationId);
}
/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Toestel nog niet geregistreerd!");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
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, MyFragment.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;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
主要 Activity 包含 3 个具有所有不同 web View 的 fragment 。 我从一个名为 FragmentAdapter.java 的数组中获取 url,它扩展了 FragmentAdapter。
工作完美......在这里 是 fragment 适配器:
package blah.blah;
import blah.blah.MyFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
String[] toVisit={
"www.google.com",
"www.stackoverlow.com",
"www.lorumipsum.com",
};
final int PAGE_COUNT = 3;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
return MyFragment.newInstance(toVisit[position]);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0)
{
return "Klassen";
}
else if(position == 1)
{
return "Docenten";
}
else
{
return "Lokalen";
}
}
}
但是如何根据从服务器检索到的信息加载不同的 url? , GCM 推送通过带有通常代码的 php 发送。
应该是 intent.PushExtra("google.com, http://google.com ")//例如 一切都不起作用。我希望我的问题很清楚。
感谢所有帮助! 只需分享您的所有提示和想法即可:P。
哦,还有我的 MyFragment.java,如果它有用的话:
package blah.blah;
import blah.blah.R;
import blah.blah.R.id;
import blah.blah.R.layout;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MyFragment extends Fragment{
WebView browser;
String url;
private Bundle webViewBundle;
Intent extras = getActivity().getIntent();
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(
R.layout.myfragment_layout,
container,
false);
final ProgressBar spinner = (ProgressBar)view.findViewById(R.id.progress);
browser=(WebView)view.findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
browser.loadUrl("file:///android_asset/geeninternet.html");
}
@SuppressWarnings("unused")
public void onProgressChanged(final WebView view, final int progress)
{
if(progress == 100)
spinner.setVisibility(View.GONE);
}
});
browser.loadUrl(url);
// Just load whatever URL this fragment is
// created with.
return view;
}
// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(String url)
{
MyFragment f=new MyFragment();
f.url=url;
return f;
}
// Met browser;
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
browser.loadUrl(url);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Sla webview op
*/
@Override
public void onPause()
{
super.onPause();
webViewBundle = new Bundle();
browser.saveState(webViewBundle);
}
/**
* Herstel staat van webview
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (webViewBundle != null)
{
browser.restoreState(webViewBundle);
}
}
}
最佳答案
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
在此代码中,您仅从通知负载中获取一个值 - price
键的值。
您没有包含服务器代码,所以我不知道您是否在发送带有通知的 URL。如果这样做,您应该从 onMessage
方法中的 intent
获取它的值,并将其传递给打开 web View 的代码。如果不这样做,则应更改服务器代码以将 URL 包含在通知负载中。
编辑:
为了将 URL 传递给您的 fragment :
从
onMessage()
中的 intent 的 extras 中获取 URL。将其传递给
generateNotification
。将 URL 作为额外添加到将在点击通知时启动 Activity 的 Intent :
<notificationIntent.putExtra ("url",url)
。在 fragment 中,在您初始化 WebView 的代码中,从启动包含该 fragment 的 Activity 的 Intent 的额外部分获取 URL (
getActivity().getIntent()。 getExtras().getString("url"
).
编辑2:
您应该在
onActivityCreated()
中加载 WebView ,而不是在onCreateView()
中。onCreateView()
在 Activity 创建之前被调用,因此getActivity()
将返回 null。不要按原样使用该代码:
String test = (getActivity().getIntent().getExtras().getString("url"));
确保getActivity()
不返回 null。我相信当getActivity()
不为 null 时,您可以假设getIntent()
和getExtras()
不会返回 null,但检查起来更安全。getString("url")
如果 Activity 未从通知中打开,则返回 null。因此,您应该确保它在加载 WebView 之前具有值。通知标志可能有问题:
// set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
如果新 Activity 已经存在(例如,如果应用程序已经在运行),通知似乎不会打开新 Activity 。在这种情况下,将不会调用 onCreateView()
和 onActivityCreated()
的 fragment 代码,也不会加载 URL。您应该更改标志以便始终创建新 Activity ,或者您应该将作为通知结果加载 WebView 的逻辑放在其他地方 - 可能在 onResume()
中。
关于java - 从通知获取 url 并加载到 Fragment 的 webview 中的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596993/