ReceiverActivity.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21. import org.json.JSONArray;
  22. import android.app.Activity;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.os.Bundle;
  26. import de.appplant.cordova.plugin.notification.*;
  27. public class ReceiverActivity extends Activity {
  28. /** Called when the activity is first created. */
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. Intent intent = this.getIntent();
  33. Bundle bundle = intent.getExtras();
  34. try {
  35. JSONObject args = new JSONObject(bundle.getString(Receiver.OPTIONS));
  36. Options options = new Options(getApplicationContext()).parse(args);
  37. launchMainIntent();
  38. fireClickEvent(options);
  39. } catch (JSONException e) {}
  40. }
  41. /**
  42. * Launch main intent for package.
  43. */
  44. private void launchMainIntent () {
  45. Context context = getApplicationContext();
  46. String packageName = context.getPackageName();
  47. Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
  48. launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  49. context.startActivity(launchIntent);
  50. }
  51. /**
  52. * Fires the onclick event.
  53. */
  54. private void fireClickEvent (Options options) {
  55. JSONArray data = new JSONArray().put(options.getJSONObject());
  56. LocalNotification.fireEvent("click", options.getId(), options.getJSON(),data);
  57. if (options.getAutoCancel()) {
  58. LocalNotification.fireEvent("cancel", options.getId(), options.getJSON(),data);
  59. }
  60. }
  61. }