Advertisement
Guest User

Untitled

a guest
Jun 10th, 2014
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. package ::APP_PACKAGE::;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import com.pushwoosh.support.v4.app.*;
  6. import com.google.android.gcm.*;
  7. import com.arellomobile.android.push.*;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.widget.Toast;
  12. import android.content.Context;
  13. //import com.arellomobile.android.push.data.*
  14. import com.arellomobile.android.push.exception.*;
  15. import com.arellomobile.android.push.preference.*;
  16. import com.arellomobile.android.push.registrar.*;
  17. import com.arellomobile.android.push.request.*;
  18. import com.arellomobile.android.push.utils.*;
  19. import com.arellomobile.android.push.utils.executor.*;
  20. import com.arellomobile.android.push.utils.notification.*;
  21.  
  22.  
  23. //din haxetoolkit
  24. public class MainActivity extends org.haxe.lime.GameActivity {
  25. //Registration receiver
  26. BroadcastReceiver mBroadcastReceiver = new RegisterBroadcastReceiver()
  27. {
  28. @Override
  29. public void onRegisterActionReceive(Context context, Intent intent)
  30. {
  31. checkMessage(intent);
  32. }
  33. };
  34.  
  35. //Push message receiver
  36. private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
  37. {
  38. @Override
  39. protected void onMessageReceive(Intent intent)
  40. {
  41. //JSON_DATA_KEY contains JSON payload of push notification.
  42. showMessage("push message is " + intent.getExtras().getString(JSON_DATA_KEY));
  43. }
  44. };
  45.  
  46. //Registration of the receivers
  47. public void registerReceivers()
  48. {
  49. IntentFilter intentFilter = new IntentFilter(getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
  50.  
  51. registerReceiver(mReceiver, intentFilter);
  52.  
  53. registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));
  54. }
  55.  
  56. public void unregisterReceivers()
  57. {
  58. //Unregister receivers on pause
  59. try
  60. {
  61. unregisterReceiver(mReceiver);
  62. }
  63. catch (Exception e)
  64. {
  65. // pass.
  66. }
  67.  
  68. try
  69. {
  70. unregisterReceiver(mBroadcastReceiver);
  71. }
  72. catch (Exception e)
  73. {
  74. //pass through
  75. }
  76. }
  77.  
  78. @Override
  79. public void onResume()
  80. {
  81. super.onResume();
  82.  
  83. //Re-register receivers on resume
  84. registerReceivers();
  85. }
  86.  
  87. @Override
  88. public void onPause()
  89. {
  90. super.onPause();
  91.  
  92. //Unregister receivers on pause
  93. unregisterReceivers();
  94. }
  95. @Override
  96. public void onCreate(Bundle savedInstanceState)
  97. {
  98. super.onCreate(savedInstanceState);
  99.  
  100. //Register receivers for push notifications
  101. registerReceivers();
  102.  
  103. //Create and start push manager
  104. PushManager pushManager = PushManager.getInstance(this);
  105.  
  106. //Start push manager, this will count app open for Pushwoosh stats as well
  107. try {
  108. pushManager.onStartup(this);
  109. }
  110. catch(Exception e)
  111. {
  112. //push notifications are not available or AndroidManifest.xml is not configured properly
  113. }
  114.  
  115. //Register for push!
  116. pushManager.registerForPushNotifications();
  117.  
  118. checkMessage(getIntent());
  119.  
  120. // other code
  121. }
  122. private void checkMessage(Intent intent)
  123. {
  124. if (null != intent)
  125. {
  126. if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
  127. {
  128. showMessage("push message is " + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));
  129. }
  130. else if (intent.hasExtra(PushManager.REGISTER_EVENT))
  131. {
  132. showMessage("register");
  133. }
  134. else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
  135. {
  136. showMessage("unregister");
  137. }
  138. else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
  139. {
  140. showMessage("register error");
  141. }
  142. else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
  143. {
  144. showMessage("unregister error");
  145. }
  146.  
  147. resetIntentValues();
  148. }
  149. }
  150.  
  151. /**
  152. * Will check main Activity intent and if it contains any PushWoosh data, will clear it
  153. */
  154. private void resetIntentValues()
  155. {
  156. Intent mainAppIntent = getIntent();
  157.  
  158. if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
  159. {
  160. mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);
  161. }
  162. else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))
  163. {
  164. mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);
  165. }
  166. else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))
  167. {
  168. mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);
  169. }
  170. else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
  171. {
  172. mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
  173. }
  174. else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
  175. {
  176. mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);
  177. }
  178.  
  179. setIntent(mainAppIntent);
  180. }
  181.  
  182. private void showMessage(String message)
  183. {
  184. Toast.makeText(this, message, Toast.LENGTH_LONG).show();
  185. }
  186. @Override
  187. protected void onNewIntent(Intent intent)
  188. {
  189. super.onNewIntent(intent);
  190. setIntent(intent);
  191.  
  192. checkMessage(intent);
  193.  
  194. setIntent(new Intent());
  195. }
  196.  
  197.  
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement