gpt4 book ai didi

android - 单击通知时无法获取意向消息

转载 作者:行者123 更新时间:2023-11-29 02:35:55 27 4
gpt4 key购买 nike

你好这个问题集中在

  1. 点击通知时,应用应在 WebView 中加载不同的 url

这里的问题是我无法在 MainActivity.java 中使用 .getStringExtra 获得未决的 Intent 值,我每次都得到 null

我不是 android 人所以如果我犯了任何愚蠢的错误请不要评判我

三个文件的发布代码

  1. 主要 Activity
  2. 我的通知管理器
  3. MyFireBaseMessagingServices

主要 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 组件( ServiceActivity 等)。

如果你需要 Context ,您需要将其传递到您的方法中。

关于android - 单击通知时无法获取意向消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com