To integrate Pushwoosh with your Android PhoneGap application you need to do simple following steps:
0. Make sure you change the package name com.phonegap.special to your application package (YOUR_APPLICATION_PACKAGE).
1. Add the following lines to your AndroidManifest.xml under manifest tag. It is similar to step 1 in Android configuration guide
<!-- Only this application can receive the messages and registration result -->
<permission android:name="YOUR_APPLICATION_PACKAGE.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="YOUR_APPLICATION_PACKAGE.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- App must have this permission to use the library -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
2. Add the following lines to your AndroidManifest.xml under application tag. Again this is similar to step 2 in Android configuration guide
<activity android:name="com.arellomobile.android.push.PushWebview" />
<activity android:name="com.arellomobile.android.push.MessageActivity" />
<activity android:name="com.arellomobile.android.push.PushHandlerActivity" />
<!-- In order to use the c2dm library, an
application must declare a class with the name C2DMReceiver, in its
own package, extending com.google.android.c2dm.C2DMBaseReceiver
-->
<service android:name="com.arellomobile.android.push.C2DMReceiver" />
<!-- Only Google services can send messages to the app. If this permission weren't set
any other app would be able to send messages to us. -->
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="YOUR_APPLICATION_PACKAGE" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="YOUR_APPLICATION_PACKAGE" />
</intent-filter>
</receiver>
3. Add the following lines In AndroidManifest.xml to your main activity
<intent-filter> <action android:name="YOUR_APPLICATION_PACKAGE.MESSAGE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
Also add additional flag to the main activity:
android:launchMode="singleTop"
In the end your activity should look like:
<activity android:name="MyApp"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="YOUR_APPLICATION_PACKAGE.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>
4. In your main app file (in my case it is src/com/phonegap/special/MyApp.java) do the following:
Add the following imports:
import android.content.Intent; import com.arellomobile.android.push.PushManager;
5. Modify onCreate function to look like this. Replace SENDER_ID with your C2DM login and YOUR_APPLICATION_ID with your application ID from Pushwoosh
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PushManager mPushManager = new PushManager(this, YOUR_APPLICATION_ID, SENDER_ID);
mPushManager.onStartup(savedInstanceState, this);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
// load url first, then we can access appView and appView in DroidGap
super.loadUrl("file:///android_asset/www/index.html");
checkMessage(getIntent());
}
6. Add the following functions (just copy-paste):
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
checkMessage(intent);
}
private void checkMessage(Intent intent)
{
if (null != intent)
{
if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
{
sendJavascript(getJSFunction("onPushReceive",
intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT)));
}
else if (intent.hasExtra(PushManager.REGISTER_EVENT))
{
sendJavascript(getJSFunction("onRegister",
intent.getExtras().getString(PushManager.REGISTER_EVENT)));
}
else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
{
sendJavascript(getJSFunction("onUnregister",
intent.getExtras().getString(PushManager.UNREGISTER_EVENT)));
}
else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
{
sendJavascript(getJSFunction("onRegisterError",
intent.getExtras().getString(PushManager.REGISTER_ERROR_EVENT)));
}
}
}
private String getJSFunction(String functionName, String string)
{
return "javascript:" + functionName + "(\"" + string.replaceAll("\"", "'") + "\");";
}
}
7. Now you can define the following functions in your main.js file. You can modify them according to your needs. The main one is onPushReceive which is called when push notification has been received by the app.
function onPushReceive(msg) {
alert(msg);
}
function onRegister(msg) {
alert(msg);
}
function onUnregister(msg) {
alert(msg);
}
function onRegisterError(msg) {
alert(msg);
}
That’s it! Easy, isn’t it?
Note that you have to have your application package registered with C2DM to receive tokens and messages. Make sure you change the package name com.phonegap.special to your application package (YOUR_APPLICATION_PACKAGE). NOTE: Simulator is not able neither to subscribe nor receive push notifications.


Devices connected —
Pushes delivered —