Skip to main content

Android Push Notifications

After a regular configuration of the FCM on your project. Retrieve the Firebase token and set with the setFirebaseToken(Context context, String token) from the Grouplink class.


FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}

// Get new FCM registration token
String token = task.getResult();
Context context = MainActivity.this;
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
//Set the Firebase Token in the grouplink sdk.
Grouplink.setFirebaseToken(context, token);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
});

The Firebase Token must be monitored, because it changes periodically. Do the same to the after a token is renewed.

/**
* There are two scenarios when onNewToken is called:
* 1) When a new token is generated on initial app startup
* 2) Whenever an existing token is changed
* Under #2, there are three scenarios when the existing token is changed:
* A) App is restored to a new device
* B) User uninstalls/reinstalls the app
* C) User clears app data
*/
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: " + token)
//Set the Firebase Token in the grouplink sdk.
Grouplink.setFirebaseToken(context, token)
super.onNewToken(token)
}

Configuring RemoteMessages reception from Firebase

To config Grouplink Regular Notifications in foreground, it is needed to config the notification reception from FCM.

1. Get the Notification from the notification inside the RemoteMessage.

Full FirebaseMessagingService extension implementation result:

import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.grouplink.marketdemo.notification.NotificationManager
import com.grouplink.marketdemo.notification.NotificationManager.Companion.createNotificationChannelVisible
import com.grouplinknetwork.GroupLink
import java.util.*

class FirebaseService : FirebaseMessagingService() {

override fun onMessageReceived(message: RemoteMessage) {

/*
* To show a notification with the app in foreground, please override
* this method, and build a new Notification from the RemoteMessage
* received.
*/

super.onMessageReceived(message)
}

override fun onNewToken(token: String) {
GroupLink.setFirebaseToken(applicationContext, token)
//Create the notification channel as soon as possible,
//to be able to receive notifications.
createNotificationChannelVisible(this)
super.onNewToken(token)
}
}

Adding meta-data for Custom Notification Channel

To configure a custom Notification Channel for firebase notifications, please add the following lines:

<application>
.
.
.

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_gl_alarm" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/gl_blue" />

</application>