Android Tutorials : Pushwoosh Integration On Android For Sending Push Notifications
Yes! your applications can Talk! Pushwoosh makes your application talk. Pushwoosh supports 21 platforms. Using few easy steps, your android application can send push notifications. Push notifications are important as to make all users involve in the application. As parse has announced its shut down, parse users need to migrate to Pushwoosh which will allow applications to send push notifications and offers many mores services. Pushwoosh service is available for iOS, Android, Windows Phone, Blackberry, Amazon, Windows 8, OS X and Safari web sites.
The steps to create your first Push are described in 3 parts :
Part ! : Includes account creation and getting your project number
1. Sign up on Pushwoosh by using this link : SignUp. A 14 days trial Platinum period will start right after you activate your account. After 14 days you can either upgrade or use free account with which you can send up to 1 million pushes every month.
2. Activate your Pushwoosh account by clicking on a link which is sent to your email by Pushwoosh team. You should be able to login to your Pushwoosh dashboard (Pushwoosh control panel) after activating your account.
3. Click on Add New button in your Pushwoosh dashboard and enter your application name and add an icon for your application. Click on Save Application button.
4. Application code gets generated and can be seen just below your application name. You will be requiring this application code in later steps.
5. Login your Google Developer Console . Select an existing project or create a new project in case if you do not have created any project yet.
6. Now, in your Google Developer Console dashboard, click on Enable and Manage APIs.
7. Under Mobile APIs category you need to click on Google Cloud Messaging option.
8. In the next page you need to click on Enable API button.
9. Now you are supposed to add credentials to your project for which you need to click on Credentials Tab which is on the left pane of your dashboard.
10. Click on Create credentials option.Now select API key from the menu displayed.
11. Now select Server Key from the options displayed.
12. After this you need to click on create button.You “need not” enter any IP address as it is optional to accept requests from specific server IP addresses.By the end of this step you should have created a Server key.
13. Your credentials will be displayed as shown below in your Credentials Tab.Copy this server key and come back to your Pushwoosh dashboard.
14. Now you need to configure the platform on which your application is running. In our case it’s Android. So click on configure button which can be found just beside Android option displayed in your Pushwoosh application control panel.
15. Once you click on configure option you need to paste the Server key(API key) which you had created during the last step. Select Native framework or any other framework as applicable. Save this information.
16. If you see “MismatchSenderID” errors in your Push History, please try the following:
- Use “Browser Key” instead of the “Server key”;
- Check that the Google Project Number in your app matches the one in your Google Developers Console.
17. You will also need the Project Number which is automatically assigned by the Google Developers Console when you create a project. You can find the Project Number in the “Dashboard” tab of the Google API console. Don’t confuse it with Project ID, which is a completely different identifier & is used only within Developers Console!
Part !! : Includes changes to your MainActivity.java
1. Add following dependencies to your build.gradle(Module:app) file. Sync the project.
1 2 3 4 5 6 7 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.pushwoosh:pushwoosh:+' compile 'com.google.android.gms:play-services-gcm:8.3.0' compile 'com.google.android.gms:play-services-location:8.3.0' } |
2. In onCreate() method of your MainActivity.java you have add following lines of code to start PushManager.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Pushwoosh Begin 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()); //PushwooshEnd in onCreate |
3. In MainActivity.java you will have to add following receivers :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//Registration receiver BroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver() { @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 } } |
4. In onResume() and onPause() methods of MainActivity.java manage the receivers
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Override public void onPause() { super.onPause(); //Unregister receivers on pause unregisterReceivers(); } @Override protected void onResume() { super.onResume(); //Re-register receivers on resume registerReceivers(); } |
5. You need to create the following methods in your MainActivity.java: The method checkMessage() will check PushWoosh extras in this intent, and fire actual method.The method resetIntentValues() will check main Activity intent and if it contains any PushWoosh data, will clear it. The method showMessage() is to print Log messages. Also add onNewIntent() method to call checkMessage() method from it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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(); } } private void resetIntentValues() { Intent mainAppIntent = getIntent(); if (mainAppIntent != null) { 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) { Log.i("AndroidBash",message); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);checkMessage(intent); } |
6. You can also have a look at the sample Pushwoosh project on Github : Android Sample Project
Part !!! : Includes changes to AndroidManifest.xml
1. In AndroidManifest.xml manifest add the following lines under application tag:
1 2 |
<meta-data android:name="PW_APPID" android:value="XXXXX-XXXXX" /> <meta-data android:name="PW_PROJECT_ID" android:value="A123456789012" /> |
Where:PW_APPID is your Pushwoosh Application Code
PW_PROJECT_ID is the Project Number you received from Google (Locate your Project Number) prefixed with letter A.
Replace XXXX-XXXXX with your Pushwoosh Application code which was obtained in the 4th step of Part One of this tutorial.
Also replace “A12345678910” with your “Project Number prefixed with A”.The Project number was obtained in the 17th step of Part One of this tutorial.
For example, if your project number is 45678912345 then you need to write “A45678912345”.
2. Add the following changes in your AndroidManifest.xml under manifest tag. Replace YOUR_PACKAGE_NAME with the package name for your application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.VIBRATE" /> <permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature"/> <uses-permission android:name="YOUR_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"/> |
3. Add the following changes in your AndroidManifest.xml under application tag. Replace PACKAGE_NAME with the package name for your application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.pushwoosh.richpages.RichPageActivity" /> <activity android:name="com.pushwoosh.MessageActivity" /> <activity android:name="com.pushwoosh.PushHandlerActivity" /> <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="YOUR_PACKAGE_NAME" /> </intent-filter> </receiver> <service android:name="com.pushwoosh.GCMListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> </intent-filter> </service> <service android:name="com.pushwoosh.GCMInstanceIDListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID"/> </intent-filter> </service> <service android:name="com.pushwoosh.GCMRegistrationService" android:exported="false"> </service> |
4. Wooosh! All Done. Build your project and run your application. As soon as your application runs for the first time on an Android device, the device gets registered if internet connection is available. On successful registration of a device, number of subscribers for your application in Pushwoosh control panel will increase by one.
5. For sending Push notifications Log In to your Pushwoosh account and click on your application. In the control panel for sending a Push you can write your own message, add an image under Banner option, add badges etc before sending a Push notification.
6. Once you enter required details you can Click on Woosh! button to send your Push.
7. You can find features, configuration & programming guides for all supported platforms and frameworks here – Features and Configurations. You can join Pushwoosh Questions and Answers Community, where Pushwoosh users share their knowledge and experience.
8. If you are successful to send a Push notification please do comment and say you could Woosh!.
Here is the Video Demo to show the working of this application.
Thanks a Lot. I could Woosh!
Glad you could do it 🙂
I could woosh! Thanks for the tutorial!
Glad you could Woosh:)