Android/iOS SDK
AnsweredCan you help to explain how to enable push notifications ?
(in order to receive calls when the Application is off)
Thanks
-
Hi,
It's possible for deployed applications on production.
You have to fill the push ids in the HUB https://hub.openrainbow.com/#/dashboard/applications
For Android, here is a howto to use it in the code: (will be updated and pushed soon on the HUB)
### Preamble
---
Using the Rainbow SDK for Android, you will not be able to receive notifications when your device is in background mode. This is an Android behavior to extend battery life by stopping unused applications for a long periods of time.
When your application needs to receive notification at any time, you have to implement the push notifications and that includes several steps. The goal is to intercept the token and messages from application side and then send these information to the Rainbow SDK for Android which will manage it.
NB: For the moment, the Rainbow SDK for Android only supports Firebase Cloud Messaging.
### Step 1 - Activate the push notification for the Rainbow SDK for Android
---
In your `Application` class just after the initialization method of the SDK (in the onCreate method), add:
RainbowSdk.instance().push().activate(this);
### Step 2 - Set up Firebase Cloud Messaging
---
The second step is setting up the Firebase Cloud Messaging feature. To do that, you can follow the Google tutorial: [Add Firebase to Your Android Project](https://firebase.google.com/docs/android/setup) and you will have the choice between using the Firebase Assistant or adding it manually.
Note that this step of the tutorial is about steps 1 and 2 of the assistant :
1. Connect your app to Firebase
2. Add FCM to your app
If you use the Firebase Assistant and have that such of error:
Error:Could not find com.google.gms:google-services:3.1.0.
Searched in the following locations:
file:/C:/Program Files/Android/Android Studio/gradle/m2repository/com/google/gms/google-services/3.1.0/google-services-3.1.0.pom
file:/C:/Program Files/Android/Android Studio/gradle/m2repository/com/google/gms/google-services/3.1.0/google-services-3.1.0.jar
Required by:
project :app
Just add the repositories part below in your **app/build.gradle** (the assistant automatically creates the dependency):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
}
}
At the end of this step, your project should be configured with a `google-services.json` (in app directory) and all required dependencies.
### Step 3 - Access the device registration token
---
Create a new class which extends `FirebaseInstanceIdService`. In the `onTokenRefresh()` overrided method, get the token and send it to the Rainbow SDK for Android.
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService
{
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is also called
* when the InstanceID token is initially generated, so this is where
* you retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh()
{
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
RainbowSdk.instance().push().onTokenRefresh(refreshedToken);
}
}
Declare the service in your app's manifest:
<service
android:name=".MyFirebaseInstanceIdService"
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
### Step 4 - Handle messages
---
Create a new class which extends `FirebaseMessagingService`. In the `onMessageReceived(RemoteMessage message)` overrided method, send the received message to the Rainbow SDK for Android.
public class MyFirebaseMessagingService extends FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage message)
{
RainbowSdk.instance().push().onMessageReceived(message.getData());
super.onMessageReceived(message);
}
}
Declare the service in your app's manifest:
<service
android:name=".MyFirebaseMessagingService"
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
That's it! Your application is now able to get push notifications and then receive messages when in background mode.Best regards,
Pascal
Please sign in to leave a comment.
Comments
2 comments