Skip to content

Commit

Permalink
Re-coded module using Zendesk SDK Messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
serybva committed Oct 23, 2024
1 parent b3c6242 commit 7f870b5
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 369 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
node_modules

# generated by bob
lib/
7 changes: 3 additions & 4 deletions ReactNativeZendesk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ Pod::Spec.new do |s|

s.authors = package['author']
s.homepage = package['repository']
s.platform = :ios, "12.0"
s.ios.deployment_target = '12.0'
s.platform = :ios, "13.0"
s.ios.deployment_target = '13.0'

s.source = { :git => "https://github.com/wavyapp/react-native-zendesk.git", :tag => "v#{s.version}" }
s.source_files = "ios/**/*.{h,m,swift}"

s.dependency 'ZendeskChatSDK', '~> 3.0'
s.dependency 'ZendeskSupportSDK', '~> 6.0'
s.dependency 'ZendeskSDKMessaging', '~> 2.0'
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
if respond_to?(:install_modules_dependencies, true)
Expand Down
8 changes: 3 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ dependencies {
implementation 'com.facebook.react:react-android:+'
implementation "androidx.core:core-ktx:1.13.1"
implementation "androidx.appcompat:appcompat:1.7.0"
// implementation platform('com.google.firebase:firebase-bom:33.1.1')
api "com.zendesk:support:5.2.0"
api "com.zendesk:messaging:5.4.0"
api "com.zendesk:chat:3.4.0"
implementation 'com.google.firebase:firebase-messaging-ktx:24.0.0'
implementation platform('com.google.firebase:firebase-bom:33.4.0')
implementation 'com.google.firebase:firebase-messaging'
implementation "zendesk.messaging:messaging-android:2.25.0"
}
5 changes: 2 additions & 3 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<service
android:name=".PushListenerService"
android:exported="false"
android:permission="android.permission.POST_NOTIFICATIONS"
>
<intent-filter>
<intent-filter android:priority="10">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,90 +1,33 @@
package fr.wavyapp.reactNativeZendesk

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
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 zendesk.chat.Chat
import zendesk.chat.ChatConfiguration
import zendesk.chat.ChatEngine
import zendesk.chat.PushData
import zendesk.classic.messaging.MessagingActivity
import zendesk.messaging.android.push.PushNotifications
import zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_DISPLAY
import zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_NOT_DISPLAY
import zendesk.messaging.android.push.PushResponsibility.NOT_FROM_MESSAGING

class PushListenerService() : FirebaseMessagingService() {
class PushListenerService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
super.onNewToken(token)
PushNotifications.updatePushNotificationToken(token)
}

override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
val zendeskChatPushNotificationProvider = Chat.INSTANCE.providers()?.pushNotificationsProvider()

manager?.let {
val builder =
NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
)

channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 100, 200)
builder.setChannelId(NOTIFICATION_CHANNEL_ID)

val chatConfig = ChatConfiguration.builder()
chatConfig.withPreChatFormEnabled(false)

val intent = MessagingActivity.builder().withEngines(ChatEngine.engine()).intent(
applicationContext,
chatConfig.build()
)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

val contentIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val shouldDisplay = PushNotifications.shouldBeDisplayed(remoteMessage.data)

builder.setContentIntent(contentIntent)
manager.createNotificationChannel(channel)
when (shouldDisplay) {
MESSAGING_SHOULD_DISPLAY -> {
PushNotifications.setNotificationSmallIconId(R.mipmap.ic_launcher)
PushNotifications.displayNotification(context = this, messageData = remoteMessage.data)
}
MESSAGING_SHOULD_NOT_DISPLAY -> {}
NOT_FROM_MESSAGING -> {

val pushData = zendeskChatPushNotificationProvider?.processPushNotification(remoteMessage.data)

when (pushData?.type) {
PushData.Type.END -> {
builder.setContentTitle("Chat ended")
builder.setContentText("The chat has ended!")
}

PushData.Type.MESSAGE -> {
builder.setContentTitle(pushData.author)
builder.setContentText(pushData.message)
EventsEmitter.instance.dispatchEvent("chatReceivedMessage", pushData)
}

else -> return
}
manager.notify(NOTIFICATION_ID, builder.build())
} ?: {
Log.d(LOG_TAG, "Notification manager not found")
}
}

companion object {
private const val LOG_TAG = "PushListenerService"

private val NOTIFICATION_ID = System.currentTimeMillis().toInt()

private const val NOTIFICATION_CHANNEL_ID = "ZendeskChat"
private const val NOTIFICATION_CHANNEL_NAME = "Zendesk chat notifications"
}
}
Loading

0 comments on commit 7f870b5

Please sign in to comment.