Following are instructions on how to set Insert to send and receive push notifications in your app.
Important note: We have recently implemented a new version of the push with significant more functionality. The functionality is available by default for SDK 48 on our US based data center. For further information see the following documentation update
If your insert data center is located in the European Union, you will need to contact support in order to enable Rich push
Before you complete the appropriate configuration steps in Insert, be sure that your Android app is properly configured for push notifications. Consult Android documentation for more details.
After ensuring your app is enabled to receive push notifications, you need to complete two steps that are specific to Insert. First, you need to provide your Google Cloud Messaging API Key so that Insert can send push notifications to the app. Second, you need to add a small amount of code to your app.
To provide the API key, navigate to the Apps page and then click on your app in the list. Next, click the App Details link in the menu on the left. You will see a section called Push Settings. Click the edit icon, and then set your API key and optionally the name of an icon image and sound file in your app.
Instructions differ based on whether you are using Firebase Cloud Messaging or Google Cloud Messaging.
Firebase Instructions
Use the google instructions in here to configure Firebase. Google also provide a sample here.
You will need to extend the FirebaseInstanceIdService and call Insert.setPushId(<token>) from the "onTokenRefresh()" callback. Firebase knows to start this service whenever a new token is generated so you do not need to do any additional work (as is being done with GCM).
In addition, you must create a service to display the incoming notification on the device. Below is sample code for this service
public class InsertFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
// Icon
int iconResId = 0;
final String drawableResourceName = notification.getIcon();
if (!TextUtils.isEmpty(drawableResourceName)) {
iconResId = getResources().getIdentifier(drawableResourceName, "drawable", getPackageName());
}
// Sound
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final String soundFileName = notification.getSound();
if (!TextUtils.isEmpty(soundFileName) && !soundFileName.equals("default")) {
sound = Uri.parse("android.resource://" + this.getPackageName() + "/raw/" + soundFileName);
}
Bundle bundle = new Bundle();
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());
}
// Build Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Your App Name")
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSmallIcon(iconResId == 0 ? getApplicationInfo().icon : iconResId)
.setSound(sound)
.setContentIntent(Insert.generatePendingIntentForPush(bundle));
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
}
}
GCM Instructions
Use the Google instructions in here to configure GCM. Google also provide a sample here. In essence, you should add the following code:
- Create a registration intent service and send the Google push ID to insert. Start the intent from the main activity.
- Create an Instance ID listener service in order to update insert with a push ID when it changes.
- Create a Listener service to display the incoming notification on the device.
Remember that the service and the listeners should all be defined in the Android Manifest as well.
1. Registration Intent Service
Google instructs you to create your own Registration Intent Service in your app. Within the Service you need to add the call to Insert.setPushId. See an example below:
public class RegistrationIntentService extends IntentService {
public RegistrationIntentService() {
super("RegistrationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
try {
// Initially this call goes out to the network to retrieve the token, subsequent calls are local.
// R.string.gcm_defaultSenderId (the Sender ID) is derived from google-services.json (the project number).
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Insert.setPushId(token);
} catch (Exception e) {
}
}
}
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
public class InsertInstanceIDListenerService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
3. Listener service to display incoming notification
Create a Listener service to display the incoming notification on the device. Below is an example:
public class InsertGcmListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, final Bundle data) {
// Icon
int iconResId = 0;
final String drawableResourceName = data.getBundle("notification").getString("icon");
if (!TextUtils.isEmpty(drawableResourceName)) {
iconResId = getResources().getIdentifier(drawableResourceName, "drawable", getPackageName());
}
// Sound
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final String soundFileName = data.getBundle("notification").getString("sound");
if (!TextUtils.isEmpty(soundFileName) && !soundFileName.equals("default")) {
sound = Uri.parse("android.resource://" + this.getPackageName() + "/raw/" + soundFileName);
}
// Build Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Your App Name")
.setContentText(data.getString("message"))
.setAutoCancel(true)
.setSmallIcon(iconResId == 0 ? getApplicationInfo().icon : iconResId)
.setSound(sound)
.setContentIntent(Insert.generatePendingIntentForPush(data));
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
}
Using Cordova with Push Notification
- Create a new Cordova Project or use an existing project.
- Add the insert plug-in as discussed in here
- Add the Push notification cordova plug in using the following command line:
cordova plugin add phonegap-plugin-push --variable SENDER_ID=<sender-id> - Add the following code after "onDeviceReady" in your javascript file
var push = PushNotification.init({ "android": {"senderID": "<sender-id>"}, "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } ); push.on('registration', function(data) {
window.plugins.Insert.setPushId(data.registrationId);
}); push.on('notification', function(data) {
// this function will be called when the push notification is received by the app
}); push.on('error', function(e) {
console.log(e.message);
});
Comments
0 comments
Please sign in to leave a comment.