如何注册到GCM推送通知后重新装载的Andr​​oid网页视图 [英] How to reload Android Webview after Registration to GCM push notification

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

问题描述

我有一个移动优化Web应用程序,并希望让Android应用(一个简单的WebView,它加载Web应用程序),以发送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.

我的code(不带不重要线)。

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");
        }
    }
}

比我已经实现了(copypasted)的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 ) {} 
}

它工作正常为止。 web视图加载正确的内容,并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.

不幸的是我不知道如何从这里着手。我最后需要的是我们的用户ID之间以及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没有提到,我不知道如何重新加载web视图。
我需要做这样的事情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?

推荐答案

您可以这样做2个步骤,而无需重新加载web视图:

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


  1. 在网页流量的活动,你应该有一个叫做成员变量 mWebView 是被分配到的WebView实例的onCreate

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

在web视图的活动,动态注册是基于一类是内部类的网页视图活动的广播接收器。内部类可以让你访问上面的#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.

在该服务,使用 sendBroadcast 发送 registrationId 来的广播接收器中的WebView活动。

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

从web视图活动的BroadcastReceiver,你可以发送信息到WebView中没有任何需要使用重新加载页面<$c$c>mWebView.loadUrl(\"javascript:myJavascriptRegistrationReceiver('<registrationId>')\");

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()广播接收器来传输应用程序组件之间的信息:
<一href=\"http://www.cs.umd.edu/class/fall2011/cmsc436/CMSC436/Lectures_Labs_files/BroadcastReceivers.pdf\" rel=\"nofollow\">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

如果你喜欢冒险的感觉和/或你想要最新的魅力,那么你可以使用事件总线像广场的奥托[1]或GreenRobot的EventBus [2]通过您的服务和活动之​​间的消息。这些替代BroadcastReceivers这通常会降低样板code的量。

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推送通知后重新装载的Andr​​oid网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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