Skip to content

Commit

Permalink
feat: Rich push support (customerio#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Nov 11, 2021
1 parent bad59b7 commit 2480127
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 153 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/io.customer/android/Versions.kt
@@ -1,7 +1,7 @@
package io.customer.android

object Versions {
internal const val ANDROID_GRADLE_PLUGIN = "7.0.2"
internal const val ANDROID_GRADLE_PLUGIN = "7.0.3"
internal const val ANDROID_JUNIT5_GRADLE_PLUGIN = "1.7.1.1"
internal const val ANDROIDX_TEST_JUNIT = "1.1.3"
internal const val ANDROIDX_APPCOMPAT = "1.3.1"
Expand Down
15 changes: 14 additions & 1 deletion messagingpush/src/main/AndroidManifest.xml
Expand Up @@ -16,12 +16,25 @@

<!-- Action receiver for push interactions -->
<receiver
android:name=".CustomerIOPushActionReceiver"
android:name=".CustomerIOPushReceiver"
android:exported="false">
<intent-filter>
<action android:name="io.customer.messagingpush.PUSH_ACTION" />
</intent-filter>
</receiver>
</application>

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent>
</queries>

</manifest>
@@ -1,33 +1,17 @@
package io.customer.messagingpush

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.customer.base.comunication.Action
import io.customer.messagingpush.CustomerIOPushActionReceiver.Companion.ACTION
import io.customer.sdk.CustomerIO
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.extensions.getErrorResult
import kotlin.math.abs

class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {

companion object {
private const val TAG = "FirebaseMessaging:"
const val DELIVERY_ID = "CIO-Delivery-ID"
const val DELIVERY_TOKEN = "CIO-Delivery-Token"
const val NOTIFICATION_REQUEST_CODE = "requestCode"

private const val CHANNEL_NAME = "CustomerIO Channel"

/**
* Handles receiving an incoming push notification.
Expand Down Expand Up @@ -84,99 +68,8 @@ class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {
remoteMessage: RemoteMessage,
handleNotificationTrigger: Boolean = true
): Boolean {
// Check if message contains a data payload.
// You can have data only notifications.
if (remoteMessage.data.isEmpty()) {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
return false
}

// Customer.io push notifications include data regarding the push
// message in the data part of the payload which can be used to send
// feedback into our system.
val deliveryId = remoteMessage.data[DELIVERY_ID]
val deliveryToken = remoteMessage.data[DELIVERY_TOKEN]

if (deliveryId != null && deliveryToken != null) {
try {
CustomerIO.instance().trackMetric(
deliveryID = deliveryId,
deviceToken = deliveryToken,
event = MetricEvent.delivered
).enqueue()
} catch (exception: IllegalStateException) {
Log.e(TAG, "Error while handling message: ${exception.message}")
}
}

// Check if message contains a notification payload.
if (remoteMessage.notification != null && handleNotificationTrigger) {
val pushContentIntent = Intent(ACTION)
pushContentIntent.setClass(context, CustomerIOPushActionReceiver::class.java)
pushContentIntent.putExtra(DELIVERY_ID, deliveryId)
pushContentIntent.putExtra(DELIVERY_TOKEN, deliveryToken)

handleNotification(
context,
remoteMessage.notification!!,
pushContentIntent
)
}

return true
}


@SuppressLint("LaunchActivityFromNotification")
private fun handleNotification(
context: Context,
notification: RemoteMessage.Notification,
pushContentIntent: Intent
) {

val requestCode = abs(System.currentTimeMillis().toInt())
pushContentIntent.putExtra(NOTIFICATION_REQUEST_CODE, requestCode)

// In Android 12, you must specify the mutability of each PendingIntent
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}

val notificationClickedIntent = PendingIntent.getBroadcast(
context,
requestCode,
pushContentIntent,
flags
)

val icon = context.applicationInfo.icon

val channelId = context.packageName
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(icon)
.setContentTitle(notification.title)
.setContentText(notification.body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(notificationClickedIntent)

val notificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}

notificationManager.notify(requestCode, notificationBuilder.build())
val handler = CustomerIOPushNotificationHandler(remoteMessage = remoteMessage)
return handler.handleMessage(context, handleNotificationTrigger)
}

}
Expand Down

This file was deleted.

0 comments on commit 2480127

Please sign in to comment.