2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger;
|
|
|
|
|
2019-12-13 11:05:33 +01:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2019-09-23 23:55:57 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2019-12-13 11:05:33 +01:00
|
|
|
import android.os.Build;
|
2019-09-23 23:55:57 +02:00
|
|
|
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
import org.mercury_im.messenger.entity.chat.DirectChat;
|
2019-09-23 23:55:57 +02:00
|
|
|
import org.mercury_im.messenger.ui.chat.ChatActivity;
|
|
|
|
|
2019-12-21 16:30:14 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
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-12-13 11:05:33 +01:00
|
|
|
public static final int FOREGROUND_SERVICE_ID = 1;
|
|
|
|
|
|
|
|
public static void initializeNotificationChannels(Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT < 26) {
|
|
|
|
// No not call below code on lower API levels
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
createForegroundNotificationChannel(context);
|
|
|
|
createMessageNotificationChannel(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(26)
|
|
|
|
private static void createForegroundNotificationChannel(Context context) {
|
|
|
|
NotificationManager notificationManager =
|
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
String channelName = context.getResources().getString(R.string.channel_name_foreground);
|
|
|
|
String channelDescription = context.getResources().getString(R.string.channel_description_foreground);
|
|
|
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(
|
|
|
|
NOTIFICATION_CHANNEL__FOREGROUND_SERVICE, channelName, NotificationManager.IMPORTANCE_MIN);
|
|
|
|
channel.setDescription(channelDescription);
|
|
|
|
channel.setShowBadge(false);
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(26)
|
|
|
|
private static void createMessageNotificationChannel(Context context) {
|
|
|
|
NotificationManager notificationManager =
|
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
String channelName = context.getResources().getString(R.string.channel_name_message);
|
|
|
|
String channelDescription = context.getResources().getString(R.string.channel_description_message);
|
|
|
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(
|
|
|
|
NOTIFICATION_CHANNEL__NEW_MESSAGE, channelName, NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
channel.setDescription(channelDescription);
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
}
|
2019-05-13 03:19:17 +02:00
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
public static int directChatMessageReceived(Context context, DirectChat chat, String contactName, String body) {
|
2019-09-24 22:15:11 +02:00
|
|
|
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
|
2019-12-21 16:30:14 +01:00
|
|
|
UUID id = 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-12-06 15:52:50 +01:00
|
|
|
tapAction.putExtra(ChatActivity.EXTRA_JID, chat.getPeer().getAddress());
|
2019-10-28 14:56:56 +01:00
|
|
|
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);
|
|
|
|
|
2019-12-21 16:30:14 +01:00
|
|
|
notificationManagerCompat.notify(id.hashCode(), builder.build());
|
2019-09-23 23:55:57 +02:00
|
|
|
|
2019-12-21 16:30:14 +01:00
|
|
|
return id.hashCode();
|
2019-09-23 23:55:57 +02:00
|
|
|
}
|
2019-05-13 03:19:17 +02:00
|
|
|
|
|
|
|
}
|