To start sending notifications from FCM, you’ll have to first create a firebase project. Create firebase project and application in firebase console with your applications package name which you can find in MainApplication.java or AndroidManifest.xml
2
Adding google-services.json
You can get your Service Account JSON by following these instructions. Download google-services.json and add the file inside your android>app folder.
implementation("com.google.firebase:firebase-messaging:22.0.0")// or latest version
4
Implementing push
Push feature can be implemented in two ways:
You may use this option if all of your android push notifications are to be handled via SuprSend SDK. We recommend you to use this method as it is just a single step process to just register the service in your application manifest and everything else will be ready.
<uses-permissionandroid:name="android.permission.INTERNET"/><!--If you are targeting to API 33 (Android 13) you will additional need to add POST_NOTIFICATIONS --><uses-permissionandroid:name="android.permission.POST_NOTIFICATIONS"/><service android:name="app.suprsend.fcm.SSFirebaseMessagingService" android:enabled="true" android:exported="false"><intent-filter><actionandroid:name="com.google.firebase.MESSAGING_EVENT"/></intent-filter></service>
Since your service is registered in the app manifest, to render the push notification directed from SuprSend server you will have to add the below code to your service.
console.log("Hello World");
5
Sending the FCM token to SuprSend
class YourApplication :Application(){overridefunonCreate(){ FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->if(!task.isSuccessful){ Log.w(TAG,"Fetching FCM registration token failed", task.exception)return@OnCompleteListener}val fcmToken = task.result suprsend.user.setAndroidFcmPush(fcmToken);})//or if you are on older FCM versionval fcmToken = FirebaseInstanceId.getInstance().token instance.getUser().setAndroidFcmPush(fcmToken)}
import android.Manifestimport android.annotation.SuppressLintimport android.content.Intentimport android.net.Uriimport android.os.Buildimport android.os.Bundleimport android.provider.Settingsimport android.util.Logimport android.widget.Toastimport androidx.activity.result.contract.ActivityResultContractsimport androidx.appcompat.app.AlertDialogimport androidx.appcompat.app.AppCompatActivityimport androidx.viewpager.widget.ViewPagerimport com.google.firebase.messaging.FirebaseMessagingimport org.json.JSONObjectclass YourActivity :AppCompatActivity(){privateval activityResultLauncher =registerForActivityResult(ActivityResultContracts.RequestPermission()){ isGranted: Boolean ->if(!isGranted){// You can show a dialog which explains the intent of this permission request of how it is important for certain features of your app to work AlertDialog.Builder(this).setView(R.layout.notification_permission_desc).setTitle(getString(R.string.app_name)).setPositiveButton("Proceed"){ _, _ ->val intent =Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)val uri: Uri = Uri.fromParts("package", packageName,null) intent.data = uristartActivity(intent)}.setNegativeButton("Deny"){ _, _ ->}.show()}}overridefunonCreate(savedInstanceState: Bundle?){super.onCreate(savedInstanceState)if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){ activityResultLauncher.launch( Manifest.permission.POST_NOTIFICATIONS)}}}
< !— vale Google.Spacing = YES >
If the androidx dependency is not present then you will have to add the below dependency in your app dependencies