Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ::APP_PACKAGE::;
- import android.os.Bundle;
- import com.pushwoosh.support.v4.app.*;
- import com.google.android.gcm.*;
- import com.arellomobile.android.push.*;
- import android.content.BroadcastReceiver;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.widget.Toast;
- import android.content.Context;
- //import com.arellomobile.android.push.data.*
- import com.arellomobile.android.push.exception.*;
- import com.arellomobile.android.push.preference.*;
- import com.arellomobile.android.push.registrar.*;
- import com.arellomobile.android.push.request.*;
- import com.arellomobile.android.push.utils.*;
- import com.arellomobile.android.push.utils.executor.*;
- import com.arellomobile.android.push.utils.notification.*;
- //din haxetoolkit
- public class MainActivity extends org.haxe.lime.GameActivity {
- //Registration receiver
- BroadcastReceiver mBroadcastReceiver = new RegisterBroadcastReceiver()
- {
- @Override
- public void onRegisterActionReceive(Context context, Intent intent)
- {
- checkMessage(intent);
- }
- };
- //Push message receiver
- private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
- {
- @Override
- protected void onMessageReceive(Intent intent)
- {
- //JSON_DATA_KEY contains JSON payload of push notification.
- showMessage("push message is " + intent.getExtras().getString(JSON_DATA_KEY));
- }
- };
- //Registration of the receivers
- public void registerReceivers()
- {
- IntentFilter intentFilter = new IntentFilter(getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
- registerReceiver(mReceiver, intentFilter);
- registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));
- }
- public void unregisterReceivers()
- {
- //Unregister receivers on pause
- try
- {
- unregisterReceiver(mReceiver);
- }
- catch (Exception e)
- {
- // pass.
- }
- try
- {
- unregisterReceiver(mBroadcastReceiver);
- }
- catch (Exception e)
- {
- //pass through
- }
- }
- @Override
- public void onResume()
- {
- super.onResume();
- //Re-register receivers on resume
- registerReceivers();
- }
- @Override
- public void onPause()
- {
- super.onPause();
- //Unregister receivers on pause
- unregisterReceivers();
- }
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- //Register receivers for push notifications
- registerReceivers();
- //Create and start push manager
- PushManager pushManager = PushManager.getInstance(this);
- //Start push manager, this will count app open for Pushwoosh stats as well
- try {
- pushManager.onStartup(this);
- }
- catch(Exception e)
- {
- //push notifications are not available or AndroidManifest.xml is not configured properly
- }
- //Register for push!
- pushManager.registerForPushNotifications();
- checkMessage(getIntent());
- // other code
- }
- private void checkMessage(Intent intent)
- {
- if (null != intent)
- {
- if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
- {
- showMessage("push message is " + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));
- }
- else if (intent.hasExtra(PushManager.REGISTER_EVENT))
- {
- showMessage("register");
- }
- else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
- {
- showMessage("unregister");
- }
- else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
- {
- showMessage("register error");
- }
- else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
- {
- showMessage("unregister error");
- }
- resetIntentValues();
- }
- }
- /**
- * Will check main Activity intent and if it contains any PushWoosh data, will clear it
- */
- private void resetIntentValues()
- {
- Intent mainAppIntent = getIntent();
- if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
- {
- mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);
- }
- else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))
- {
- mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);
- }
- else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))
- {
- mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);
- }
- else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
- {
- mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
- }
- else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
- {
- mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);
- }
- setIntent(mainAppIntent);
- }
- private void showMessage(String message)
- {
- Toast.makeText(this, message, Toast.LENGTH_LONG).show();
- }
- @Override
- protected void onNewIntent(Intent intent)
- {
- super.onNewIntent(intent);
- setIntent(intent);
- checkMessage(intent);
- setIntent(new Intent());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement