【问题标题】:Load Specific URL page in WebView if the Push Notification is Clicked如果单击推送通知,则在 WebView 中加载特定 URL 页面
【发布时间】:2020-06-02 08:16:55
【问题描述】:

我是 Android Studio 的新手,对此有疑问。

所以我有一个使用 Webview 的 android 应用程序。我还实现了 FCM 推送通知。 当我点击通知时,它直接打开主页。如果我想在点击通知时打开应用中的特定页面怎么办?

这里是主要活动

class MainActivity : Activity() {
    private val URL = "https://thelink.pl"
    lateinit var webView: WebView
    private var notificationId: Int = 0;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.requestWindowFeature(Window.FEATURE_NO_TITLE)
        webView = createView()
        setContentView(webView)

        firebaseInstanceToken() //get token
        subscibeToTopics("androidUsers")
    }

这是我的 FCM 服务

 // [START receive_message]
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        //check if message contains a data payload
        remoteMessage
                .data.isNotEmpty().let {
            Log.d("isData", "Message data payload: " + remoteMessage.data)
                    theMessageBody = remoteMessage.data.get("body").toString()
                    theMessageTitle = remoteMessage.data.get("title").toString()
                    theMessageDetail_ = remoteMessage.data.get("messageDetail").toString()
                    sendNotification(theMessageBody, theMessageTitle, theMessageDetail_)
        }

        //check if the message contains a notification payload
        remoteMessage
                .notification?.let {
                    theMessageBody = it.body!!
                    theMessageTitle = it.title!!
                    sendNotification(theMessageBody, theMessageTitle, theMessageDetail_)
        }
    }

感谢任何帮助..谢谢

【问题讨论】:

    标签: android firebase android-studio firebase-cloud-messaging


    【解决方案1】:

    您是否通过 FCM 发送了特定页面?然后使用意图发送。

    就我而言,

    private void sendNotification(String messageBody) {
            Dlog.e("messageBody = " + messageBody);
    
            try {
                JSONObject jsonBody = new JSONObject(messageBody);
    
                int pushType = jsonBody.getJSONObject("data").getInt("pushType");
                String pushTitle = jsonBody.getString("title");
                String textBody = "";
                String contentText = "";
                PendingIntent pendingIntent = null;
    
    
    
                    String push_title = jsonBody.getString("title");
                    String boardContent = jsonBody.getJSONObject("data").getString("boardContent");
                    String boardUrl = jsonBody.getJSONObject("data").getString("boardUrl");
    
                    textBody = boardContent;
                    contentText = boardContent;
    
                    if (boardUrl.equals("")){
                        Intent intent = new Intent(this, SplashActivity.class);
                        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }else {
                        Intent intent = new Intent(this, PushWebViewActivity.class);
                        intent.putExtra("push_title", push_title);
                        intent.putExtra("boardUrl", boardUrl);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }
    
    
                Bitmap icon2 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    
                NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
                bigText.bigText(textBody);
    
                NotificationCompat.Builder notificationBuilder;
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel mChannel = new NotificationChannel("channel", "channel_nm", NotificationManager.IMPORTANCE_HIGH);
                    mChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
                    mChannel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    AudioAttributes att = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .build();
                    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                    mChannel.enableVibration(true);
                    mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), att);
                    notificationManager.createNotificationChannel(mChannel);
                    notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
                } else {
                    notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
                }
                notificationBuilder
                        .setLargeIcon(icon2)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(pushTitle)
                        .setContentText(contentText)
                        .setStyle(bigText)
                        .setLights(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), 3000, 3000)
                        .setVibrate(new long[]{500, 500, 500, 500})
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true)
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setContentIntent(pendingIntent);
    
    
                int random_num = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
                notificationManager.notify(random_num, notificationBuilder.build());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    

    我从 FCM 获取 url 并发送到页面和 webView 加载 url

    【讨论】:

    • 你的意思是我应该创建一个新活动?并将意图重定向到这个新活动?
    • 你可以做一个活动。我的意思是你必须像以前一样发送到活动。所以在Activity中,为不同的url分开类型。
    • 所以,目前,我有一个包含所有配置的 MainActivity。所以,现在我必须创建一个具有不同 url 的新活动.. 正确吗?
    • 没有。您不需要进行其他活动(如果您不想)。 1. 用于设置与现有 url 不同的 url 的类型应作为 fcm 接收。 2. 在活动中重写 OnResume 函数。在这里您可以更改 url(或在 webview 加载之前更改 url)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多