从FCM通知访问,打开或编辑Webview

时间:2016-09-01 11:39:31

标签: android firebase push-notification android-webview firebase-cloud-messaging

我在我的应用程序中实现了android FCM,我的App只在WebView中加载了一个Web应用程序。

我想在收到(并点击)FCM通知后更改webview的网址。 我在“MyFirebaseMessagingService.java”中管理通知,特别是在sendNotification方法中。

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, Principal.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

/*HERE I WANNA ACCES, CREATE OR EDIT A WEBVIEW*/


    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Formación Alcalá")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setContentText(messageBody);



    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

我认为这应该是可能的。请有人指导我? P.D.抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

检查这个

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("URl", Your URL);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |            Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent =      PendingIntent.getActivity(getApplicationContext(),notificationIndex,notification    Intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle,      notificationMessage, pendingNotificationIntent);

并在您的主要活动中:

Bundle extras = intent.getExtras();
if(extras != null){
    if(extras.containsKey("NotificationMessage"))
    {
        setContentView(R.layout.viewmain);
        // extract the extra-data in the Notification
        String url= extras.getString("URl");
        webview.loadurl(url);
    }
}