【问题标题】:how to get link from notification and open in mainactivity webview by firebase?如何从通知中获取链接并通过firebase在mainactivity webview中打开?
【发布时间】:2020-05-13 03:12:18
【问题描述】:

我在做什么:通过这些代码,我收到了来自 Firebase 的通知和打开 mainactivity webview 默认 URL 的 onclick 通知。

我想做什么: 我想在我将通过 Firebase 发送的 mainactivity webview 中打开 url。

我不知道如何实现(从 Firebase 通知接收 url 并在 mainactivity webview 中打开)

notification.java

public class FirebaseMessageReciver extends FirebaseMessagingService {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            //handle when receive notification via data event
            if (remoteMessage.getData().size()>0){
                showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
            }
            //handle when receive notification
            if(remoteMessage.getNotification()!=null){
                showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
            }
        }

        private RemoteViews getCustomDesign(String title,String message) {
            RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
            remoteViews.setTextViewText(R.id.title,title);
            remoteViews.setTextViewText(R.id.message,message);
            remoteViews.setImageViewResource(R.id.icon,R.mipmap.ic_launcher_round);
            return remoteViews;

        }

        public void showNotification(String title,String message,String link){
            Intent intent = new Intent(this, MainActivity.class);
            String channel_id = "web_app_channel";
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setSound(uri)
                    .setAutoCancel(true)
                    .setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent);

            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
                builder=builder.setContent(getCustomDesign(title, message));
            }
            else{
                builder= builder.setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.mipmap.ic_launcher_round);
            }

            NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(uri, null);
                notificationManager.createNotificationChannel(notificationChannel);
            }

            notificationManager.notify(0,builder.build());

        }
    }

ma​​inactivity.java

public class MainActivity extends AppCompatActivity {


    String webAdress = "https://google.com";
    WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
        FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new HelpClient());
        webView.getSettings().setJavaScriptEnabled(true); //enable javascript
        webView.loadUrl(webAdress);
}

    private class HelpClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    }
}

【问题讨论】:

    标签: android firebase webview push-notification


    【解决方案1】:

    你可以使用intent.putExtra

    像这样在你的代码中添加它。

    Intent intent = new Intent(this, MainActivity.class);
    String channel_id = "web_app_channel";
    intent.putExtra("link", link);  // add this line for send url
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    

    在MainActivity中添加getIntent

    Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
    FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
    
    String urlFromFirebase = getIntent().getStringExtra("link"); //get link from firebase
    
    // for exception
    if (urlFromFirebase.equals("")) {
       urlFromFirebase = "https://google.com";
    }
    
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new HelpClient());
    webView.getSettings().setJavaScriptEnabled(true); //enable javascript
    webView.loadUrl(urlFromFirebase ); //load
    

    然后您可以从 firebase 加载 url。

    [编辑] 我创立了

    showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
    

    这段代码在你编辑之前,所以如果你想编辑。试试这个代码。

    @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {
                //handle when receive notification via data event
                if (remoteMessage.getData().size()>0){
                    showNotification(remoteMessage.getData().get("body"));
                }
                //handle when receive notification
                if(remoteMessage.getNotification()!=null){
                    showNotification(remoteMessage.getData().get("body"));
                }
            }
    
    
        private void sendNotification(String messageBody) {
            try {
            JSONObject jsonBody = new JSONObject(messageBody);
    
            Intent intent = new Intent(this, MainActivity.class);
            String channel_id = "web_app_channel";
    
           // you can edit this part as you want or follow data.
            String title = jsonBody.getString("title");
            String message = jsonBody.getString("message");
            String link = jsonBody.getJSONObject("data").getString("link");
    
            intent.putExtra("link", link);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setSound(uri)
                    .setAutoCancel(true)
                    .setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent);
    
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
                builder=builder.setContent(getCustomDesign(title, message));
            }
            else{
                builder= builder.setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.mipmap.ic_launcher_round);
            }
    
            NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(uri, null);
                notificationManager.createNotificationChannel(notificationChannel);
            }
    
                notificationManager.notify(0,builder.build());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            }
    

    就我而言,我制作 JSONObject,并获取链接表单数据。试试这个。

    【讨论】:

    • 代码运行良好,但有一个问题是如何通过 getnotification 函数获取链接,我只能通过 getdata 获得
    • 因为我收到错误(错误:类 FirebaseMessageReciver 中的方法 showNotification 不能应用于给定类型;必需:字符串、字符串、找到的字符串:字符串、字符串原因:实际参数列表和形式参数列表的长度不同)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2017-03-10
    • 2019-07-06
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多