Customize Android SDK
Customize Pushwoosh Android SDK to meet your goals and preferences
Make sure you've integrated Pushwoosh Android SDK into your project:

In App Purchase tracking

In order to track in-app purchases, you should call the sendInappPurchase method of the Pushwoosh class when a user buys a product:
1
public void sendInappPurchase(java.lang.String sku,
2
java.math.BigDecimal price,
3
java.lang.String currency)
Copied!
    sku – purchased product ID
    price – price of the product
    currency – currency of the price (ex: “USD”)
Example
1
Pushwoosh.getInstance().sendInappPurchase("com.example.inapp1", new BigDecimal(1.99), "USD");
Copied!

Deep linking

In your activity that will handle the deep link, add <data> tag with the scheme, host, and pathPrefix parameters.
1
<activity
2
android:name=".PromoActivity"
3
android:label="PromoActivity">
4
<intent-filter>
5
<action android:name="android.intent.action.VIEW" />
6
<category android:name="android.intent.category.DEFAULT" />
7
<category android:name="android.intent.category.BROWSABLE" />
8
9
<data android:scheme="com.pushwoosh"
10
android:host="promotion"
11
android:pathPrefix="" />
12
</intent-filter>
13
</activity>
Copied!
Deep link page name (promotion in the example given) goes to the host field, not pathPrefix.
In the example above, the deep link will open PromoActivity. The basic implementation below displays alert with promo id value for the sake of simplicity. In your application it could definitely do something useful!
1
public class PromoActivity extends Activity
2
{
3
@Override
4
protected void onCreate(Bundle savedInstanceState)
5
{
6
super.onCreate(savedInstanceState);
7
8
setContentView(R.layout.deep_link);
9
setTitle("Deep link activity");
10
11
Intent intent = getIntent();
12
String action = intent.getAction();
13
Uri data = intent.getData();
14
15
if (TextUtils.equals(action, Intent.ACTION_VIEW))
16
{
17
openUrl(data);
18
}
19
}
20
21
private void openUrl(Uri uri)
22
{
23
String promoId = uri.getQueryParameter("id");
24
Toast.makeText(getApplicationContext(), promoId, Toast.LENGTH_LONG).show();
25
}
26
}
Copied!

Geozones push notification

To use geozone pushes, simply add com.pushwoosh:pushwoosh-location library and call
1
PushwooshLocation.startLocationTracking();
Copied!
It is recommended to check dynamic permissions android.Manifest.permission.ACCESS_FINE_LOCATION and android.Manifest.permission.ACCESS_COARSE_LOCATION before invoking this method.

Using Local Notifications with Pushwoosh

If you use Pushwoosh Local Notifications API, add RECEIVE_BOOT_COMPLETED permission to your AndroidManifest.xml:
AndroidManifest.xml
1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>x
Copied!

Using badge number on Android

Pushwoosh supports setting badge number on the app icon shortcut for the following Android launchers: Sony, Samsung, LG, HTC, Xiaomi, ASUS, ADW, APEX, NOVA, HUAWEI, ZUK, OPPO. To use this functionality, simply add com.pushwoosh:pushwoosh-badge library to your application.

Opening custom activity

If you want to start a particular activity in response to push notifications, add the following intent-filter to that activity:
AndroidManifest.xml
1
<activity android:name="YourActivity">
2
<intent-filter><meta-data android:name="com.pushwoosh.log_level" android:value="ERROR" />
3
<action android:name="${applicationId}.MESSAGE"/>
4
<category android:name="android.intent.category.DEFAULT"/>
5
</intent-filter>
6
</activity>
Copied!

Controlling Log Level

In order to assist with debugging and integration, SDK will print all the requests to the console by default. When you are ready for the production build, add com.pushwoosh.log_level meta-data with value "ERROR" to the AndroidManifest.xml. This way only information about errors will go to the console. Other option could be one of the following:
NONE - No logs from the SDK ERROR - Display only errors in the console WARN - Display also a warnings INFO - Display informational messages DEBUG - Even debug information is displayed now NOISE - Everything SDK can print and more
AndroidManifest.xml
1
<meta-data android:name="com.pushwoosh.log_level" android:value="ERROR" />
Copied!

Using Proguard

When using Proguard, add the following options:
proguard-rules.pro
1
-keep class com.pushwoosh.** { *; }
2
-dontwarn com.pushwoosh.**
Copied!
See Google Play Services library requirements regarding Proguard here: https://developers.google.com/android/guides/setup

Customising Notification open behaviour

If you need to programmatically select which activity to display as a result of push notification, you can create custom NotificationServiceExtension and include fully qualified class name of your NotificationServiceExtension in metadata under com.pushwoosh.notification_service_extension value.
AndroidManifest.xml
1
<meta-data
2
android:name="com.pushwoosh.notification_service_extension"
3
android:value="com.your.package.YourNotificationServiceExtension" / >
Copied!
YourNotificationServiceExtension.java
1
public class YourNotificationServiceExtension extends NotificationServiceExtension {
2
@Override
3
protected void startActivityForPushMessage(PushMessage message) {
4
// super.startActivityForPushMessage() starts default launcher activity
5
// or activity marked with ${applicationId}.MESSAGE action.
6
// Simply do not call it to override this behaviour.
7
// super.startActivityForPushMessage(message);
8
9
// start your activity instead:
10
Intent launchIntent = new Intent(getApplicationContext(), YourActivity.class);
11
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
12
13
// (Optional) pass notification data to Activity
14
launchIntent.putExtra(Pushwoosh.PUSH_RECEIVE_EVENT, message.toJson().toString());
15
16
context.startActivity(launchIntent);
17
}
18
}
Copied!
Important
If you use proguard in production builds, make sure your custom NotificationServiceExtension is not obfuscated ( by adding -keep class rule), otherwise it will lead to ClassNotFoundException.

Customizing push notifications

To customize the view of push notifications, you need to create a custom Factory, you can create custom NotificationFactory and include fully qualified class name of your NotificationFactory in metadata under com.pushwoosh.notification_factory value.
AndroidManifest.xml
1
<meta-data
2
android:name="com.pushwoosh.notification_factory"
3
android:value="com.your.package.YourNotificationFactory" / >
Copied!
YourNotificationFactory
1
public class YourNotificationFactory extends PushwooshNotificationFactory {
2
@Override
3
public Notification onGenerateNotification(@NonNull PushMessage pushMessage) {
4
if (customNotification) {
5
// TODO: generate and return custom notification
6
}
7
8
// return default Pushwoosh notification
9
return super.onGenerateNotification(pushMessage);
10
}
11
}
Copied!

Customizing group summary

To customize the appearance of a group summary, create a custom Factory. You can create custom SummaryNotificationFactory and include fully qualified class name of your SummaryNotificationFactory in metadata under com.pushwoosh.summary_notification_factory value.
AndroidManifest.xml
1
<meta-data
2
android:name="com.pushwoosh.summary_notification_factory"
3
android:value="com.your.package.YourSummaryNotificationFactory" />
Copied!
YourSummaryNotificationFactory
1
public class YourSummaryNotificationFactory extends PushwooshSummaryNotificationFactory {
2
@Override
3
public String summaryNotificationMessage(int notificationsAmount) {
4
// return the message you want
5
return super.summaryNotificationMessage(notificationsAmount);
6
}
7
@Override
8
public int summaryNotificationIconResId() {
9
// return the icon resource id you want
10
return super.summaryNotificationIconResId();
11
}
12
}
Copied!

Private endpoint URL

For Private Offering subscriptions only.
Pushwoosh provides Private endpoints for customers with Private Offering subscriptions. To set up Private endpoint for Android SDK, you need to add the following to your AndroidManifest.xml file:
AndroidManifest.xml
1
<meta-data android:name="com.pushwoosh.base_url" android:value="PUSHWOOSH_PRIVATE_ENDPOINT_URL_PROVIDED" />
Copied!

Creating a Rich Media queue

In case there are several Rich Media pages to display simultaneously (for example, trigger events for two or more In-Apps take place at one moment, or a Rich Media page is being displayed already at the moment a different trigger event occurs), you can set up a queue for Rich Media pages displaying. To create a queue, add the following code to your project:
Application.java
1
package com.pushwoosh.testingapp;
2
3
import com.pushwoosh.RichMediaManager;
4
import com.pushwoosh.exception.PushwooshException;
5
import com.pushwoosh.richmedia.RichMediaPresentingDelegate;
6
import com.pushwoosh.richmedia.RichMedia;
7
import com.pushwoosh.internal.utils.PWLog;
8
9
import java.util.ArrayDeque;
10
import java.util.concurrent.locks.ReentrantLock;
11
12
public class DefaultRichMediaPresentingDelegate implements RichMediaPresentingDelegate {
13
private final String TAG = DefaultRichMediaPresentingDelegate.class.getSimpleName();
14
private ArrayDeque<RichMedia> richMediaQueue = new ArrayDeque<>();
15
private RichMedia currentRichMedia = null;
16
private ReentrantLock reentrantLock;
17
18
public DefaultRichMediaPresentingDelegate() {
19
PWLog.noise(TAG, "new DefaultRichMediaPresentingDelegate:" + this);
20
reentrantLock = new ReentrantLock();
21
}
22
23
@Override
24
public boolean shouldPresent(RichMedia richMedia) {
25
PWLog.noise(TAG, "shouldPresent:" + richMedia);
26
if (currentRichMedia == null) {
27
PWLog.noise(TAG, "currentRichMedia is null");
28
}
29
if (richMedia.isLockScreen()) {
30
PWLog.noise(TAG, "isLockScreen is true");
31
return true;
32
}
33
try {
34
reentrantLock.lock();
35
if (currentRichMedia == null) {
36
PWLog.noise(TAG, "show:" + richMedia);
37
currentRichMedia = richMedia;
38
return true;
39
} else {
40
PWLog.noise(TAG, "add to queue:" + richMedia);
41
richMediaQueue.add(richMedia);
42
return false;
43
}
44
} finally {
45
reentrantLock.unlock();
46
}
47
}
48
49
@Override
50
public void onPresent(RichMedia richMedia) {
51
PWLog.noise(TAG, "onPresent" + richMedia);
52
}
53
54
@Override
55
public void onError(RichMedia richMedia, PushwooshException pushwooshException) {
56
PWLog.error(TAG, pushwooshException + " richMedia:"+richMedia.toString());
57
tryShowNextRichMediaThreadSafety();
58
}
59
60
@Override
61
public void onClose(RichMedia richMedia) {
62
PWLog.noise(TAG, "onClose:" + richMedia);
63
tryShowNextRichMediaThreadSafety();
64
}
65
66
private void tryShowNextRichMediaThreadSafety() {
67
try {
68
reentrantLock.lock();
69
tryShowNextRichMedia();
70
} finally {
71
reentrantLock.unlock();
72
}
73
}
74
75
private void tryShowNextRichMedia() {
76
if (!richMediaQueue.isEmpty()) {
77
currentRichMedia = richMediaQueue.poll();
78
PWLog.noise(TAG, "try manual show:" + currentRichMedia);
79
RichMediaManager.present(currentRichMedia);
80
} else {
81
PWLog.noise(TAG, "richMediaQueue is empty");
82
currentRichMedia = null;
83
}
84
}
85
}
Copied!
We strongly recommend to set up a queue in Application instead of Activity. Otherwise, it may create several queues.

Custom sound push

Available for Android 8+ devices.
    1.
    Put your audio file in the proper folder. For the native Android framework, your files should be placed in the /app/src/main/res/raw folder.
Please refer to the corresponding guides to find where to place the audio file in projects built in other frameworks.
3. Select a sound while configuring a push message.
4. Set the Notification Channel the message will belong to. To do so, specify the following in the “Android root params” field:{"pw_channel": "PUSH NOTIFICATION CHANNEL NAME"} // here you need to specify the name for your channel with custom sound
In case of using remote API, set the parameters as follows within your /createMessage API request:
1
"android_root_params": {"pw_channel": "push"} // here you need to specify the name for your channel with custom sound, for example, "push" for the notifications with push.wav sound.
2
"android_sound": "push" // here you should specify the file name without extension
Copied!
Once you send the push with those params specified, the Notification Channel with the selected sound is created for all devices with Android 8+.
Now, to send the push with a custom sound, you have to specify only the channel associated with that sound.

Proguard rules for custom notification sounds

If your app uses proguard for code and resource shrinking, it is important to keep your sound files intact and available for external libraries. If you use minifyEnabled = true property to your build.gradle, add the following rules to your proguard-rules.pro:
1
-keep public class your.package.name.R$raw {
2
*;
3
}
Copied!
If you shrink resources of your app on top of code shrinking by using shrinkResources=true property, you should additionally specify what resources you want to keep. To do that, create a new XML file with any name, save it somewhere in your project (for example, in res/xml), and specify the resource names under the tools:keep parameter in the resources tag:
1
<?xml version="1.0" encoding="utf-8"?>
2
<resources xmlns:tools="http://schemas.android.com/tools"
3
tools:keep="@raw/*"
4
/>
Copied!
Last modified 1mo ago