-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-coded module using Zendesk SDK Messaging
- Loading branch information
Showing
18 changed files
with
547 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
node_modules | ||
|
||
# generated by bob | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
android/src/main/java/fr/wavyapp/reactNativeZendesk/EventsEmitter.kt
This file was deleted.
Oops, something went wrong.
91 changes: 17 additions & 74 deletions
91
android/src/main/java/fr/wavyapp/reactNativeZendesk/PushListenerService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.