Due to the recent changes in Notification permissions in the Android 13 SDK, I need to update my app with FCM integration to obey the notifications rule. I have upgraded the compileSdk to 33 and also the targetSdk to 33. I have written code to ask POST_NOTIFICATIONS and the prompt appears. On pressing "Allow" the Notification Permission is enabled. But on sending a push notification the app receives no notification.
I only made changes in asking for the notification permission. Did not change anything in FirebaseMessagingService Class. The app was previously targetSdk 30.
class MyFirebaseMessagingService : FirebaseMessagingService() {
var intent: Intent? = null
var badgecount: Int = 0
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data.isNotEmpty()) {
try {
val json = JSONObject(remoteMessage.data.toString())
handleDataMessage(json)
} catch (e: Exception) {
Log.e("FIREBASEEXCEPTION:", e.message.toString())
}
}
if (remoteMessage.notification != null) {
sendNotification(remoteMessage.notification!!.body)
println("Message Notification Body:" + remoteMessage.notification!!.body)
}
super.onMessageReceived(remoteMessage)
}
private fun handleDataMessage(json: JSONObject) {
try {
val data = json.getJSONObject("body")
val badge = data.getString("badge")
val message = data.getString("message")
Log.e("FIREBASE_MSG:", message)
badgecount = badge.toInt()
sendNotification(message)
} catch (e: JSONException) {
Log.e("JSONEXCEPTION:", e.message.toString())
} catch (e: java.lang.Exception) {
}
}
@SuppressLint("ObsoleteSdkInt")
private fun sendNotification(messageBody: String?) {
ShortcutBadger.applyCount(this, badgecount)
intent = Intent(this, HomeActivity::class.java)
intent!!.action = System.currentTimeMillis().toString()
intent!!.putExtra("Notification_Recieved", 1)
val notId = NotificationID.getID()
val stackBuilder = TaskStackBuilder.create(this)
stackBuilder.addNextIntentWithParentStack(intent!!)
val pendingIntent = stackBuilder.getPendingIntent(notId, PendingIntent.FLAG_UPDATE_CURRENT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder =
NotificationCompat.Builder(this)
.setContentTitle(resources.getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel_id = getString(R.string.app_name) + "_01"
val name: CharSequence = getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(channel_id, name, importance)
notificationBuilder.setChannelId(mChannel.id)
mChannel.setShowBadge(true)
mChannel.canShowBadge()
mChannel.enableLights(true)
mChannel.lightColor = resources.getColor(R.color.split_bg)
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500)
notificationManager.createNotificationChannel(mChannel)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.notify_small)
notificationBuilder.color = resources.getColor(R.color.split_bg)
} else {
notificationBuilder.setSmallIcon(R.drawable.nas_large)
notificationBuilder.color = resources.getColor(R.color.split_bg)
}
notificationManager.notify(notId, notificationBuilder.build())
}
}
class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {
var mContext: Context = this
override fun onTokenRefresh() {
//val refreshedToken = FirebaseInstanceId.getInstance().token
val refreshedToken = FirebaseInstanceId.getInstance().token.toString()
Log.e("FIREBASETOKEN", refreshedToken)
sendRegistrationToServer(refreshedToken)
super.onTokenRefresh()
}
private fun sendRegistrationToServer(refreshedToken: String) {
if (refreshedToken != null) {
PreferenceManager.setFcmID(mContext, refreshedToken)
}
}
}
And in manifest
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<service
android:name=".fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
I had followed everything answered here and referred the documentation, still couldn't find a solution to this issue. But somehow when I added the below flags, I could receive Notifications.
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(notId, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);