注册到 GCM 推送通知后如何重新加载 Android Webview [英] How to reload Android Webview after Registration to GCM push notification

查看:25
本文介绍了注册到 GCM 推送通知后如何重新加载 Android Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个移动优化的网络应用程序,并且想要制作一个 Android 应用程序(一个简单的 WebView,它加载网络应用程序),以便发送 GCM 推送通知.

I have a mobile optimized web app, and want to make an Android App (a simple WebView, which loads the web app), in order to send GCM push notifications.

我的代码(没有不重要的行).

My code (without the unimportant lines).

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Webview 
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://" + HOST + "?type=android&registrationId=" + REGISTRATIONID);       

        // GCM Registration                   
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "SENDERID");
          Log.i("TAG", "Registered! ");
        } else {
          Log.i("TAG", "Already registered");
        }
    }
}

我已经实现(复制粘贴)了 GCMIntentService 类

Than I have implemented (copypasted) the GCMIntentService Class

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("SENDERID");
    }

    protected void onRegistered( Context arg0, String registrationId ) {
        Log.i( "TAG", "Registration id is: " + registrationId );
    }    

    protected void onError( Context arg0, String errorId ) {}
    protected void onMessage( Context arg0, Intent intent ) {}
    protected void onUnregistered( Context arg0, String registrationId ) {} 
}

到目前为止一切正常.WebView 加载正确的内容,并调用 onRegistered 方法并将 registrationId 显示到 LogCat.

It works fine so far. The WebView loads the correct content, and the onRegistered method is called and displays the registrationId to LogCat.

不幸的是,我不知道如何从这里开始.我最终需要的是我们的 UserId(在 Native App 中不可用)和 registrationId 之间的相关性.

Unfortunately I have no clue how to proceed from here. What I finally need is a correlation between our UserId (which is not available in the Native App) and the registrationId.

对我来说最好的解决方案是在每次 onRegistered 调用后重新加载 WebView 并将 registrationId 作为参数添加到 URL.但是由于我在此方法中没有对 MainActivity 的引用,因此我不知道如何重新加载 WebView.我需要用 BroadcastManager 做这些事情还是有其他方法?

The best solution for me would be to reload the WebView after each onRegistered call and to add the registrationId as a parameter to the URL. But since I have no reference in this method to the MainActivity, i dont know how to reload the WebView. Do I need to do such things with BroadcastManager or are there other ways?

推荐答案

您可以分两步完成,而无需重新加载 webview:

You can do this in 2 steps, without having to reload the webview:

  1. 在 webview 活动中,您应该有一个名为 mWebView 的成员变量,它被分配给 onCreate 中的 webview 实例.

  1. In the webview activity you should have a member variable called mWebView that gets assigned to the webview instance in onCreate.

在 webview 活动中,动态注册一个基于类的 BroadcastReceiver,该类是您的 webview 活动的内部类.内部类将允许您访问上面 #1 中定义的 mWebView 成员变量.

In the webview activity, dynamically register a BroadcastReceiver that is based on a class that is an inner class of your webview activity. The inner class will allow you to access the mWebView member variable defined in #1 above.

在服务中,使用sendBroadcastregistrationId发送到WebView活动中的BroadcastReceiver.

In the service, use sendBroadcast to send the registrationId to the BroadcastReceiver in the WebView activity.

从 webview 活动中的 BroadcastReceiver 中,您可以使用 mWebView.loadUrl("javascript:myJavascriptRegistrationReceiver('<registrationId>') 将信息发送到 webview 中,而无需重新加载页面");

From the BroadcastReceiver in the webview activity, you can send the info into the webview without any need to reload the page by using mWebView.loadUrl("javascript:myJavascriptRegistrationReceiver('<registrationId>')");

下面的链接解释了 sendBroadcast()BroadcastReceiver 在应用组件之间传输信息:http://www.cs.umd.edu/class/fall2011/cmsc436/CMSC436/Lectures_Labs_files/BroadcastReceivers.pdf

The link below explains sendBroadcast() and BroadcastReceiver to transmit info between app components: http://www.cs.umd.edu/class/fall2011/cmsc436/CMSC436/Lectures_Labs_files/BroadcastReceivers.pdf

如果您喜欢冒险和/或想要最新的热点",那么您可以使用 Square 的 Otto [1] 或 GreenRobot 的 EventBus [2] 等事件总线在您的服务和活动之间传递消息.这些是 BroadcastReceiver 的替代品,通常会减少样板代码的数量.

If you're feeling adventurous and / or you want the latest "hotness", then you can use an event bus like Square's Otto [1] or GreenRobot's EventBus [2] to pass messages between your service and the activity. These are replacements for BroadcastReceivers that generally reduce the amount of boilerplate code.

[1] https://github.com/square/otto

[2] https://github.com/greenrobot/EventBus

这篇关于注册到 GCM 推送通知后如何重新加载 Android Webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆