Push setup
iOS Setup
You're now ready to implement Batch Push in your app. First, you need to ask the user to allow your app to send Push notifications.
Showing a notification request
When appropriate (for example, at the end of a welcome flow), show the notification request popup using BatchPush.registerForRemoteNotifications()
.
If you don't know where to put this, just add it after Batch.start(config)
:
<fx:Script>
<![CDATA[
import com.batch.ane.Batch;
import com.batch.ane.BatchPush;
import com.batch.ane.Config;
// Build config
var config:Config = new Config().setAndroidAPIKey("YOUR-ANDROID-APIKEY").setiOSAPIKey("YOUR-IOS-APIKEY");
// Call batch to start with the configuration variable.
Batch.start(config);
// Request push
BatchPush.registerForRemoteNotifications(); /* You can move this line somewhere else, such as at the end of an onboarding flow if you dont want the push popup to appear at the start of your app on iOS */
]]>
</fx:Script>
Add the entitlement to yout app.xml
Finally, you need to add the entitlement to your app.xml under the iPhone tag:
<iPhone>
<Entitlements>
<![CDATA[
<key>aps-environment</key>
<string>production</string>
]]>
</Entitlements>
</iPhone>
Set Production as the string value when you are signing your app with a production certificate, and Development if you are using a development certificate.
Android Setup
You're now ready to implement Batch Push! First, you need to add Google Play Services into you app.
There are 2 versions of the new Batch ANE, one with Google Play Services included and the other without. If your app will use push with Android, include the Google Play Services version.
Adding Google Play Services
GCM uses the Google Play Services library. If you haven't already done so, you must use the Google Play Services version of Batch ANE to use Batch Push on Android.
If you use another extension that already include Google Play Services, you can use Batch ANE without Google Play Services.
Adding permissions and service
GCM needs 4 permissions to set up push:
- com.google.android.c2dm.permission.RECEIVE
- android.permission.WAKE_LOCK
- android.permission.VIBRATE
- [YOUR-PACKAGE-NAME].permission.C2D_MESSAGE
These 4 permissions will not show up in the Play Store, so you don't need to worry about users being presented with them.
You should add it in your app.xml under the android manifestAdditions tag:
<android>
<manifestAdditions><![CDATA[
<manifest android:installLocation="auto">
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="YOUR-PACKAGE-NAME.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="YOUR-PACKAGE-NAME.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
</manifest>
]]></manifestAdditions>
</android-v1>
Be sure to replace YOUR-PACKAGE-NAME with your actual package name, for exemple: com.google.android.
You must now declare the Batch service and receiver. They will handle notifications and display them for you, without any additional code.
Here's how to declare it under the application tag:
<android>
<manifestAdditions><![CDATA[
<manifest android:installLocation="auto">
<application android:enabled="true">
<!-- For Google Play Services -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Batch service and receiver -->
<service android:name="com.batch.android.BatchPushService" />
<receiver android:name="com.batch.android.BatchPushReceiver" 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>
</application>
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="YOUR-PACKAGE-NAME.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="YOUR-PACKAGE-NAME.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
</manifest>
]]></manifestAdditions>
</android-v1>
Enabling Batch push
Activate Batch Push by setting the GCM Sender Id that you obtained earlier, right before setting the Batch API Key (either dev or live).
<fx:Script>
<![CDATA[
import com.batch.ane.Batch;
import com.batch.ane.BatchPush;
import com.batch.ane.Config;
// Build config
var config:Config = new Config().setAndroidAPIKey("YOUR-ANDROID-APIKEY").setiOSAPIKey("YOUR-IOS-APIKEY");
// Set-up GCM id
BatchPush.setGCMSenderId("YOUR-GCM-SENDER-ID");
// Call batch start with the config
Batch.start(config);
// Request push
BatchPush.registerForRemoteNotifications(); /* You can move this line somewhere else, like at the end of a welcome screen if you dont want the push popup to pop at the start of your app on iOS */
]]>
</fx:Script>
That's it! If your implementation is correct, you will see your push token in the logs.
Check out this article on how to manage the notification display on Android.
Your first notification
1. Obtaining your device token
You can find your device's token using the debug tool or locating the token Batch posts to the logcat/console on Studio/Xcode.
The line you are looking for within the logcat/console is:
D/Batch (20509): Batch.Push: Registration id: X
//Where X represents the Batch Push token for your device.
//If you are having troubles finding the entry,
//searching for _Batch_ will find the correct entries.
[Batch] - Push token (Production): X
OR
[Batch] - Push token (Development): X
//Where X represents the Batch Push token for your device.
//
//The token shown in the console will be Production or Development based on
//your Provisioning Profile. Be sure to note which token you are working with.
//If you are having troubles finding the entry, searching for Batch will
//find the correct entries.
2. Sending a test push
Go to ⚙ Settings → Push settings, paste your device's token and click on Save. On iOS, make sure you chose the right environment.
Then, all you have to do is to click on the "Send" button. If you sucessfuly set up the SDK, you will receive a notification on your device.
Troubleshooting
If you're having trouble sending test notifications, you can check our troubleshooting documentation:
What's next
Congratulations on finishing the integration of Batch Push!
Here are a couple of extra steps you can take before releasing your app:
- Custom user identifier: Add support for custom user identifiers if you are planning to use the Transactional or the Custom Data APIs.
- Token import: Import your existing tokens if you're coming from another push provider.
Recommended
If you're coming from another Push provider, we can import your existing devices' tokens for you.