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®istrationId=" + 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");
}
}
}
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 ) {}
}
It works fine so far. The WebView loads the correct content, and the onRegistered method is called and displays the registrationId to LogCat.
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.
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?
You can do this in 2 steps, without having to reload the webview:
In the webview activity you should have a member variable called mWebView that gets assigned to the webview instance in onCreate.
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.
In the service, use sendBroadcast to send the registrationId to the BroadcastReceiver in the WebView activity.
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>')");
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With