@@ -20,7 +20,10 @@ import org.xmtp.android.example.R
20
20
import org.xmtp.android.example.conversation.ConversationDetailActivity
21
21
import org.xmtp.android.example.extension.truncatedAddress
22
22
import org.xmtp.android.example.utils.KeyUtil
23
+ import org.xmtp.android.library.Conversation
23
24
import org.xmtp.android.library.messages.EnvelopeBuilder
25
+ import org.xmtp.android.library.messages.Topic
26
+ import uniffi.xmtpv3.org.xmtp.android.library.codecs.GroupMembershipChanges
24
27
import java.util.Date
25
28
26
29
class PushNotificationsService : FirebaseMessagingService () {
@@ -57,40 +60,78 @@ class PushNotificationsService : FirebaseMessagingService() {
57
60
GlobalScope .launch(Dispatchers .Main ) {
58
61
ClientManager .createClient(keysData, applicationContext)
59
62
}
60
- val conversation =
61
- runBlocking { ClientManager .client.fetchConversation(topic, includeGroups = true ) }
62
- if (conversation == null ) {
63
- Log .e(TAG , " No keys or conversation persisted" )
64
- return
65
- }
66
- val envelope = EnvelopeBuilder .buildFromString(topic, Date (), encryptedMessageData)
67
- val peerAddress = conversation.peerAddress
68
- val decodedMessage = conversation.decode(envelope)
63
+ val welcomeTopic = Topic .userWelcome(ClientManager .client.installationId).description
64
+ val builder = if (welcomeTopic == topic) {
65
+ val group = ClientManager .client.conversations.fromWelcome(encryptedMessageData)
66
+ val pendingIntent = PendingIntent .getActivity(
67
+ this ,
68
+ 0 ,
69
+ ConversationDetailActivity .intent(
70
+ this ,
71
+ topic = group.topic,
72
+ peerAddress = Conversation .Group (group).peerAddress
73
+ ),
74
+ (PendingIntent .FLAG_IMMUTABLE or PendingIntent .FLAG_UPDATE_CURRENT )
75
+ )
76
+
77
+ NotificationCompat .Builder (this , CHANNEL_ID )
78
+ .setSmallIcon(R .drawable.ic_xmtp_white)
79
+ .setContentTitle(Conversation .Group (group).peerAddress.truncatedAddress())
80
+ .setContentText(" New Group Chat" )
81
+ .setAutoCancel(true )
82
+ .setColor(ContextCompat .getColor(this , R .color.black))
83
+ .setPriority(NotificationCompat .PRIORITY_DEFAULT )
84
+ .setStyle(NotificationCompat .BigTextStyle ().bigText(" New Group Chat" ))
85
+ .setContentIntent(pendingIntent)
86
+ } else {
87
+ val conversation =
88
+ runBlocking { ClientManager .client.fetchConversation(topic, includeGroups = true ) }
89
+ if (conversation == null ) {
90
+ Log .e(TAG , topic)
91
+ Log .e(TAG , " No keys or conversation persisted" )
92
+ return
93
+ }
94
+ val decodedMessage = if (conversation is Conversation .Group ) {
95
+ runBlocking { conversation.group.processMessage(encryptedMessageData).decode() }
96
+ } else {
97
+ val envelope = EnvelopeBuilder .buildFromString(topic, Date (), encryptedMessageData)
98
+ conversation.decode(envelope)
99
+ }
100
+ val peerAddress = conversation.peerAddress
69
101
70
- val body = decodedMessage.body
71
- val title = peerAddress.truncatedAddress()
102
+ val body: String = if (decodedMessage.content<Any >() is String ) {
103
+ decodedMessage.body
104
+ } else if (decodedMessage.content<Any >() is GroupMembershipChanges ) {
105
+ val changes = decodedMessage.content() as ? GroupMembershipChanges
106
+ " Membership Changed ${
107
+ changes?.membersAddedList?.mapNotNull { it.accountAddress }.toString()
108
+ } "
109
+ } else {
110
+ " "
111
+ }
112
+ val title = peerAddress.truncatedAddress()
72
113
73
- val pendingIntent = PendingIntent .getActivity(
74
- this ,
75
- 0 ,
76
- ConversationDetailActivity .intent(
114
+ val pendingIntent = PendingIntent .getActivity(
77
115
this ,
78
- topic = topic,
79
- peerAddress = peerAddress
80
- ),
81
- (PendingIntent .FLAG_IMMUTABLE or PendingIntent .FLAG_UPDATE_CURRENT )
82
- )
83
-
84
- val builder = NotificationCompat .Builder (this , CHANNEL_ID )
85
- .setSmallIcon(R .drawable.ic_xmtp_white)
86
- .setContentTitle(title)
87
- .setContentText(body)
88
- .setAutoCancel(true )
89
- .setColor(ContextCompat .getColor(this , R .color.black))
90
- .setPriority(NotificationCompat .PRIORITY_DEFAULT )
91
- .setStyle(NotificationCompat .BigTextStyle ().bigText(body))
92
- .setContentIntent(pendingIntent)
116
+ 0 ,
117
+ ConversationDetailActivity .intent(
118
+ this ,
119
+ topic = topic,
120
+ peerAddress = peerAddress
121
+ ),
122
+ (PendingIntent .FLAG_IMMUTABLE or PendingIntent .FLAG_UPDATE_CURRENT )
123
+ )
93
124
125
+ NotificationCompat .Builder (this , CHANNEL_ID )
126
+ .setSmallIcon(R .drawable.ic_xmtp_white)
127
+ .setContentTitle(title)
128
+ .setContentText(body)
129
+ .setAutoCancel(true )
130
+ .setColor(ContextCompat .getColor(this , R .color.black))
131
+ .setPriority(NotificationCompat .PRIORITY_DEFAULT )
132
+ .setStyle(NotificationCompat .BigTextStyle ().bigText(body))
133
+ .setContentIntent(pendingIntent)
134
+ }
94
135
// Use the URL as the ID for now until one is passed back from the server.
95
136
NotificationManagerCompat .from(this ).apply {
96
137
if (ActivityCompat .checkSelfPermission(
0 commit comments