2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger;
|
|
|
|
|
2019-09-23 23:55:57 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
import org.mercury_im.messenger.persistence.entity.ChatModel;
|
2019-09-23 23:55:57 +02:00
|
|
|
import org.mercury_im.messenger.ui.chat.ChatActivity;
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
public class Notifications {
|
|
|
|
|
|
|
|
public static final String NOTIFICATION_CHANNEL__FOREGROUND_SERVICE = "foreground_service";
|
|
|
|
public static final String NOTIFICATION_CHANNEL__NEW_MESSAGE = "new_message";
|
|
|
|
public static final String NOTIFICATION_CHANNEL__INCOMING_CALL = "incoming_call"; // One day...
|
|
|
|
public static final String NOTIFICATION_CHANNEL__DEBUG = "debug";
|
|
|
|
|
2019-05-27 21:34:11 +02:00
|
|
|
// Notification IDs
|
2019-05-13 03:19:17 +02:00
|
|
|
public static final int FOREGROUND_SERVICE_ID = 1; // must not be 0
|
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
public static int chatMessageReceived(Context context, ChatModel chat, String contactName, String body) {
|
2019-09-24 22:15:11 +02:00
|
|
|
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
|
2019-10-28 14:56:56 +01:00
|
|
|
int id = (int) chat.getId();
|
2019-09-23 23:55:57 +02:00
|
|
|
|
|
|
|
Intent tapAction = new Intent(context, ChatActivity.class);
|
|
|
|
tapAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2019-10-28 14:56:56 +01:00
|
|
|
tapAction.putExtra(ChatActivity.EXTRA_JID, chat.getPeer().getJid().toString());
|
|
|
|
tapAction.putExtra(ChatActivity.EXTRA_ACCOUNT, chat.getPeer().getAccount().getId());
|
2019-09-23 23:55:57 +02:00
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, tapAction, 0);
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
|
|
|
|
Notifications.NOTIFICATION_CHANNEL__NEW_MESSAGE);
|
|
|
|
builder.setContentTitle(context.getResources().getString(R.string.notification_title, contactName));
|
|
|
|
builder.setContentText(body);
|
|
|
|
builder.setSmallIcon(R.drawable.ic_person_outline_black_24dp);
|
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
builder.setAutoCancel(true);
|
|
|
|
|
|
|
|
notificationManagerCompat.notify(id, builder.build());
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
2019-05-13 03:19:17 +02:00
|
|
|
|
|
|
|
}
|