你好这个问题集中于
问题在这里我无法使用MainActivity.java中的.getStringExtra获取待处理的意图值我每次都得到null
我不是一个机器人,所以如果我犯了任何愚蠢的错误,请不要判断我
发布三个文件的代码
MainActivity
package com.xyz.zzz;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity{
private TextView textView;
private BroadcastReceiver broadcastReceiver;
private Button buttonRegister;
private LinearLayout WB;
private LinearLayout WB_ERROR;
public String notiIntent = "false";
WebView webView;
private boolean loadcheck = true;
private String loaderror ="No Error";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WB = (LinearLayout)this.findViewById(R.id.webViewLayout);
WB_ERROR = (LinearLayout)this.findViewById(R.id.webErrorLayout);
final ProgressDialog progDailog = ProgressDialog.show(this, "", "Loading...",true);
final TextView textViewToChange = (TextView) findViewById(R.id.errorMsg);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int error_code, String description , String falingUrl){
// webView.loadUrl("file:///android_assets/error.html");
WB.setVisibility(LinearLayout.GONE);
WB_ERROR.setVisibility(LinearLayout.VISIBLE);
loadcheck = false;
loaderror = description;
if(error_code == -8 || error_code == -6) {
textViewToChange.setText("Poor Internet Connectivity");
}
if(error_code == -14 || error_code == -2){
textViewToChange.setText("No Internet Connection");
}
/*Toast.makeText(MainActivity.this, "This is my Toast message!"+ error_code,
Toast.LENGTH_LONG).show();*/
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
if (progDailog != null && progDailog.isShowing()) {
progDailog.dismiss();
}
/* Toast.makeText(MainActivity.this, "This is my Toast message!",
Toast.LENGTH_LONG).show();
Log.e("onpageload" , view);*/
//WB.setVisibility(LinearLayout.VISIBLE);
}
});
Bundle extra = getIntent().getExtras();
if(extra != null) {
Log.d("Notification", extra.("url") + "hello Noti");
}
Intent intent = this.getIntent();
if(intent != null && intent.getStringExtra("url") != null ) {
Log.d("Notification", intent.getStringExtra("url"));
}
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null)
{
//Cry about not being clicked on
}
else if (extras.getBoolean("NotiClick"))
{
//Do your stuff here mate :)
}
}
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("example.com/index.php?" + SharedPrefManager.getmInstance(this).getToken());
buttonRegister = (Button) findViewById(R.id.retryButton);
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progDailog.show();
createWebView();
checkERROR();
}
});
Log.d("sas",SharedPrefManager.getmInstance(this).getToken());
Log.d("Click Intent",notiIntent);
}
public void createWebView(){
webView.loadUrl("example.com/index.php?" + SharedPrefManager.getmInstance(this).getToken());
}
public void checkERROR(){
if(loadcheck != false){
WB.setVisibility(LinearLayout.VISIBLE);
WB_ERROR.setVisibility(LinearLayout.GONE);
}
}
public void setNotiIntent(){
notiIntent = "true";
Log.d("ssssss","asas");
}
}
现在MYFIREBASEMESSAGING SERVICES
package com.XYZ.anew;
import android.content.Intent;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import static com.google.android.gms.internal.zzs.TAG;
/**
* Created by AR on 16-10-2017.
*/
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCMmessageR";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be:
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body1: " + remoteMessage.getNotification().getBody());
}
Log.d(TAG, "Message Notification Body2: " + remoteMessage.getNotification().getBody());
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
notifyUser(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
}
public void notifyUser(String from , String notification){
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
myNotificationManager.showNotification(from , notification , new Intent(getApplicationContext() , MainActivity.class));
}
}
现在我的通知经理
package com.XYZ.anew;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
/**
* Created by AR on 16-10-2017.
*/
public class MyNotificationManager{
private Context ctx;
public static final int NOTIFICATION_ID = 234;
public MyNotificationManager(Context ctx){
this.ctx = ctx;
}
public void showNotification(String from, String notification , Intent intent){
MainActivity ctsx = new MainActivity();
ctsx.setNotiIntent();
intent.putExtra("url",true);
PendingIntent pendingIntent = PendingIntent.getActivity(
ctx,
NOTIFICATION_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Notification mNotification = builder.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(from)
.setContentText(notification)
.setSound(uri)
.build();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID , mNotification);
}
}
答案 0 :(得分:0)
你的问题在这里:
intent.putExtra("url",true);
您添加了"额外"用钥匙" url"和价值"真"。这是boolean
。如果您致电getStringExtra("url")
,您将获得null
。如果你调用``getBooleanExtra(" url")you will get
为真。
也...
你绝对不能这样做(在showNotification()
中):
MainActivity ctsx = new MainActivity();
只有Android才能实例化Android组件(Service
,Activity
等。)。
如果您需要Context
,则需要将其传递给您的方法。