作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好这个问题集中在
这里的问题是我无法在 MainActivity.java 中使用 .getStringExtra 获得未决的 Intent 值,我每次都得到 null
我不是 android 人所以如果我犯了任何愚蠢的错误请不要评判我
三个文件的发布代码
主要 Activity
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 服务
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);
}
}
最佳答案
你的问题在这里:
intent.putExtra("url",true);
您使用键“url”和值“true”添加了一个“extra”。这是 boolean
.如果您调用 getStringExtra("url")
你会得到 null
.如果你调用``getBooleanExtra("url") you will get
真的`。
还有...
你绝对不能这样做(在 showNotification()
中):
MainActivity ctsx = new MainActivity();
只有 Android 可以实例化 Android 组件( Service
、 Activity
等)。
如果你需要 Context
,您需要将其传递到您的方法中。
关于android - 单击通知时无法获取意向消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084110/
是否可以将我的应用程序的用户从我的 Android 应用程序重定向到我在最近发布的 Pebble 商店中的表盘条目? 从技术上讲,我想知道新的 2.0.0 Pebble 应用程序是否捕获任何 URI
是否可以启动 genius scan(或类似的东西,只要它是免费的)作为获取其图片的 Intent ?或者让它出现在“打开方式”对话框中?我已经尝试联系制造商并用谷歌搜索了这个问题。 最佳答案 更新
我是一名优秀的程序员,十分优秀!