Skip to content

Commit

Permalink
update code in chatUI for fix PN not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Arief Nur Putranto committed Nov 6, 2024
1 parent 255d079 commit ac78b0d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 61 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
</intent-filter>
</service>

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/ic_launcher" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="com.qiscus.dragonfly"/>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/qiscus_key_google_apis_android"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
package com.qiscus.sdk.util;public class NotificationClickReceiver {
package com.qiscus.sdk.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.qiscus.sdk.chat.core.QiscusCore;
import com.qiscus.sdk.chat.core.data.model.QiscusChatRoom;
import com.qiscus.sdk.chat.core.data.model.QiscusComment;
import com.qiscus.sdk.chat.core.data.remote.QiscusApi;
import com.qiscus.sdk.ui.QiscusChatActivity;
import com.qiscus.sdk.ui.QiscusGroupChatActivity;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NotificationClickReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
QiscusComment qiscusComment = intent.getParcelableExtra("data");
QiscusApi.getInstance()
.getChatRoom(qiscusComment.getRoomId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(qiscusChatRoom -> QiscusCore.getDataStore().addOrUpdate(qiscusChatRoom))
.map(qiscusChatRoom -> getChatRoomActivity(context, qiscusChatRoom))
.subscribe(newIntent -> start(context, newIntent), throwable ->
Toast.makeText(context, throwable.getLocalizedMessage(), Toast.LENGTH_SHORT).show());
}

private Intent getChatRoomActivity(Context context, QiscusChatRoom qiscusChatRoom) {
return qiscusChatRoom.isGroup() ? QiscusGroupChatActivity.generateIntent(context, qiscusChatRoom) :
QiscusChatActivity.generateIntent(context, qiscusChatRoom);
}

private void start(Context context, Intent newIntent) {
context.startActivity(newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.qiscus.nirmana.Nirmana;
import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.R;
import com.qiscus.sdk.chat.core.QiscusCore;
import com.qiscus.sdk.chat.core.data.local.QiscusCacheManager;
import com.qiscus.sdk.chat.core.data.model.QiscusChatRoom;
import com.qiscus.sdk.chat.core.data.model.QiscusComment;
Expand Down Expand Up @@ -215,10 +216,10 @@ private static void showPushNotification(Context context, QiscusComment comment)
new QiscusPushNotificationMessage(comment.getId(), messageText);
pushNotificationMessage.setRoomName(comment.getRoomName());
pushNotificationMessage.setRoomAvatar(comment.getRoomAvatar());
if (!QiscusCacheManager.getInstance()
.addMessageNotifItem(pushNotificationMessage, comment.getRoomId())) {
return;
}
// if (!QiscusCacheManager.getInstance()
// .addMessageNotifItem(pushNotificationMessage, comment.getRoomId())) {
// return;
// }

if (Qiscus.getChatConfig().isEnableAvatarAsNotificationIcon()) {
QiscusAndroidUtil.runOnUIThread(() -> loadAvatar(context, comment, pushNotificationMessage));
Expand Down Expand Up @@ -259,7 +260,13 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
private static void pushNotification(Context context, QiscusComment comment,
QiscusPushNotificationMessage pushNotificationMessage, Bitmap largeIcon) {

String notificationChannelId = Qiscus.getApps().getPackageName() + ".qiscus.sdk.notification.channel";
// if (QiscusCore.getDataStore().isContains(comment)) {
// return;
// }

QiscusCore.getDataStore().addOrUpdate(comment);

String notificationChannelId = QiscusCore.getApps().getPackageName() + ".qiscus.sdk.notification.channel";
if (BuildVersionUtil.isOreoOrHigher()) {
NotificationChannel notificationChannel =
new NotificationChannel(notificationChannelId, "Chat", NotificationManager.IMPORTANCE_HIGH);
Expand All @@ -270,9 +277,8 @@ private static void pushNotification(Context context, QiscusComment comment,
}

PendingIntent pendingIntent;
Intent openIntent = new Intent(context, QiscusPushNotificationClickReceiver.class);
Intent openIntent = new Intent(context, NotificationClickReceiver.class);
openIntent.putExtra("data", comment);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getBroadcast(context, QiscusNumberUtil.convertToInt(comment.getRoomId()),
openIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
Expand All @@ -282,66 +288,19 @@ private static void pushNotification(Context context, QiscusComment comment,
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, notificationChannelId);
notificationBuilder.setContentTitle(pushNotificationMessage.getRoomName())
notificationBuilder.setContentTitle(comment.getSender())
.setContentIntent(pendingIntent)
.setContentText(pushNotificationMessage.getMessage())
.setTicker(pushNotificationMessage.getMessage())
.setSmallIcon(Qiscus.getChatConfig().getNotificationSmallIcon())
.setLargeIcon(largeIcon)
.setColor(ContextCompat.getColor(context, Qiscus.getChatConfig().getInlineReplyColor()))
.setContentText(comment.getMessage())
.setTicker(comment.getMessage())
.setSmallIcon(R.drawable.ic_qiscus_notif_app)
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setGroup("CHAT_NOTIF_" + comment.getRoomId())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

if (Qiscus.getChatConfig().isEnableReplyNotification() && isNougatOrHigher()) {
String getRepliedTo = pushNotificationMessage.getRoomName();
RemoteInput remoteInput = new RemoteInput.Builder(KEY_NOTIFICATION_REPLY)
.setLabel(QiscusTextUtil.getString(R.string.qiscus_reply_to, getRepliedTo.toUpperCase()))
.build();

NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_send,
QiscusTextUtil.getString(R.string.qiscus_reply_to, getRepliedTo.toUpperCase()), pendingIntent)
.addRemoteInput(remoteInput)
.build();
notificationBuilder.addAction(replyAction);
}

boolean cancel = false;
if (Qiscus.getChatConfig().getNotificationBuilderInterceptor() != null) {
cancel = !Qiscus.getChatConfig().getNotificationBuilderInterceptor()
.intercept(notificationBuilder, comment);
}

if (cancel) {
return;
}

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
List<QiscusPushNotificationMessage> notifItems = QiscusCacheManager.getInstance()
.getMessageNotifItems(comment.getRoomId());
if (notifItems == null) {
notifItems = new ArrayList<>();
}
int notifSize = 5;
if (notifItems.size() < notifSize) {
notifSize = notifItems.size();
}
if (notifItems.size() > notifSize) {
inboxStyle.addLine(".......");
}
int start = notifItems.size() - notifSize;
for (int i = start; i < notifItems.size(); i++) {
inboxStyle.addLine(notifItems.get(i).getMessage());
}
inboxStyle.setSummaryText(QiscusTextUtil.getString(R.string.qiscus_notif_count, notifItems.size()));
notificationBuilder.setStyle(inboxStyle);

if (notifSize <= 3) {
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
}

QiscusAndroidUtil.runOnUIThread(() -> NotificationManagerCompat.from(context)
.notify(QiscusNumberUtil.convertToInt(comment.getRoomId()), notificationBuilder.build()));

}

private static void handleDeletedComment(Context context, List<QiscusComment> comments, boolean hardDelete) {
Expand Down

0 comments on commit ac78b0d

Please sign in to comment.