CHROME PUSHES - THE NEW FORCE AWAKENS Bullet

As soon as I heard about Apple's new Safari push notifications on OS X Mavericks, I got excited to offer it to our readers at MacMagazine.com.br. However, looking into the details I became worried as it wasn't as simple as I previously imagined.

Then, I found out about Pushwoosh and by that time they had already published a quick tutorial for everyone to easily deploy Safari's notifications via their service. It worked like a charm for us and we even received a personalized support service while the Pushwoosh team updated their WordPress plugin.

I'm very satisfied with the service! Congrats, guys! ;-)

Rafael Fischmann

Founder and editor-in-chief

MacMagazine.com.br

READ TESTIMONIAL

Native Android SDK

Download SDK Sample Project SDK API Docs

Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API.

The SDK will run on older devices.

NOTE: Push notifications work on Android Emulator. You have to have Google Play account set up on the Emulator.

To integrate Pushwoosh into your Android application you need to do simple following steps:

1. Get SDK (Pushwoosh.jar) from the link above


2. Place it in the libs folder of your Android project.

In 99% you don’t have to do this step as adding Pushwoosh.jar to libs folder adds it to classpath of your project automatically. But if something goes wrong you might need to add it manually:

For Eclipse: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

For IDEA: http://stackoverflow.com/questions/1051640/correct-way-to-add-lib-jar-to-an-intellij-idea-project


3.a In the main activity add the following receivers:

	//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, getPackageName() +".permission.C2D_MESSAGE", null);
		
		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
		}
	}

3.b Manage receivers registration in the onPause/onResume functions

	@Override
	public void onResume()
	{
		super.onResume();
		
		//Re-register receivers on resume
		registerReceivers();
	}

	@Override
	public void onPause()
	{
		super.onPause();

		//Unregister receivers on pause
		unregisterReceivers();
	}

3.c In the main activity initialize the PushManager and register receivers:

 @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
   }

4. In AndroidManifest.xml manifest add the following lines under application tag. The GCM Project Number is a number, but make sure you prefix it with the letter “A”.

Example:

<meta-data android:name="PW_APPID" android:value="4F0C807E51EC77.93591449" />
<meta-data android:name="PW_PROJECT_ID" android:value="A60756016005" />

Where:

PW_APPID – Pushwoosh Application Code

PW_PROJECT_IDProject Number you receive from Google (Locate your Project Number) prefixed with A.

(see more: http://developer.android.com/guide/google/gcm/index.html and http://developer.android.com/guide/google/gcm/gs.html)

PW_PROJECT_ID value is a number but make sure you prefix it with the letter “A” as in the example above.

5.a In the main activity create the following method:

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

5.b Also call this method from onNewIntent as well.

   @Override
   protected void onNewIntent(Intent intent)
   {
       super.onNewIntent(intent);
       setIntent(intent);

       checkMessage(intent);
   }

5.c In the AndroidManifest.xml make the following changes for this activity. Replace PACKAGE_NAME with the package name for your application.

       <activity android:name="YourStartActivity"
                 android:launchMode="singleTop"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="PACKAGE_NAME.MESSAGE"/>
               <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
           <intent-filter>
               <action android:name="android.intent.action.MAIN"/>
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>

6. Add the following changes in your AndroidManifest.xml under manifest tag. Replace PACKAGE_NAME with the package name for your application.

You can find your package name in the AndroidManifest.xml under the manifest tag in the very top of the file.
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!-- Permission to get DeviceId -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <!--
     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.
    -->
    <permission
            android:name="PACKAGE_NAME.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
    <uses-permission
            android:name="PACKAGE_NAME.permission.C2D_MESSAGE"/>

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
            android:name="com.google.android.c2dm.permission.RECEIVE"/>

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <!-- The targetSdkVersion is optional, but it's always a good practice
         to target higher versions. -->
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>

READ_PHONE_STATE permission: The SDK uses this permission to get DeviceId property of the device if ANDROID_ID is not available. You can omit this permission in your APK. However if ANDROID_ID will not be available, features like AppGroups or cross-application user targeting will not work for this device.


GET_ACCOUNTS permission: GCM requires a Google account if you target Androids lower than 4.0.4


7. Add the following changes in your AndroidManifest.xml under application tag. Replace PACKAGE_NAME with the package name for your application.

You can find your package name in the AndroidManifest.xml under the manifest tag in the very top of the file.
        <activity android:name="com.arellomobile.android.push.PushWebview"/>

        <activity android:name="com.arellomobile.android.push.MessageActivity"/>

        <activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>

        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
                android:name="com.google.android.gcm.GCMBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <category android:name="PACKAGE_NAME"/>
            </intent-filter>
        </receiver>

        <!--
          Application-specific subclass of PushGCMIntentService that will
          handle received messages.
        -->
        <service android:name="com.arellomobile.android.push.PushGCMIntentService"/>

If you need to handle custom notifications from your server, you should create a class extended from com.arellomobile.android.push.PushGCMIntentService, and include this custom intent service under the “PW_PUSH_SERVICE” tag in your manifest file.

Ex.: In your manifest you have to substitute the

<service android:name="com.arellomobile.android.push.PushGCMIntentService"/>
with
<service android:name=".CustomPushService" />
<meta-data android:name="PW_PUSH_SERVICE" android:value="PACKAGE_NAME.CustomPushService" />


If you would like to use Geo Push Notifications, add the following service to your manifest file:

        <!--
          Service for sending location updates
        -->
        <service android:name="com.arellomobile.android.push.GeoLocationService"/>

You should also call startTrackingGeoPushes() for your PushManager object. Please see our SDK API here.

That’s it!


For load balancing purposes we may need to change the API endpoints in your application. This happens automatically and handled by our SDK. Please make sure that nothing in your app is blocking access to any URL that contains *.pushwoosh.com in it.

Note: If your device is connected to the Internet via WiFi, and messages don’t get through to the device, please make sure GCM ports are not blocked by your firewall.

The device accesses the GCM servers on ports 5228-5230. If your organization has a firewall that restricts the traffic to or from the Internet, you’ll need to configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn’t provide specific IPs. It changes IPs frequently.


Push Notifications SDK API:

Pushwoosh https://www.pushwoosh.com/wp-content/themes/pushwoosh/img/logo.png support@pushwoosh.com