Implement super hacky notifications

This commit is contained in:
Paul Schaub 2019-09-23 23:55:57 +02:00
parent 75483efd70
commit a8892ee884
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
8 changed files with 75 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import org.mercury_im.messenger.core.connection.MercuryConfiguration;
import org.mercury_im.messenger.di.component.AppComponent;
import org.mercury_im.messenger.di.component.DaggerAppComponent;
import org.mercury_im.messenger.di.module.AppModule;
import org.mercury_im.messenger.persistence.pojo.Chat;
import org.mercury_im.messenger.persistence.room.RoomModule;
import org.mercury_im.messenger.persistence.room.RoomRepositoryModule;
import org.mercury_im.messenger.service.XmppConnectionService;
@ -23,7 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
public class MercuryImApplication extends Application {
public class MercuryImApplication extends Application implements org.mercury_im.messenger.core.NotificationManager {
public static final String TAG = "Mercury-IM";
@ -110,11 +111,16 @@ public class MercuryImApplication extends Application {
String mDescription = getResources().getString(R.string.channel_description_message);
NotificationChannel messages = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__NEW_MESSAGE,
mName, NotificationManager.IMPORTANCE_HIGH);
mName, NotificationManager.IMPORTANCE_DEFAULT);
messages.setDescription(mDescription);
notificationManager.createNotificationChannel(messages);
}
@Override
public int chatMessageReceived(Chat chat, String contactName, String body) {
return Notifications.chatMessageReceived(this, chat, contactName, body);
}
public AppComponent getAppComponent() {
return appComponent;
}

View File

@ -1,5 +1,15 @@
package org.mercury_im.messenger;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import org.mercury_im.messenger.persistence.pojo.Chat;
import org.mercury_im.messenger.ui.chat.ChatActivity;
public class Notifications {
public static final String NOTIFICATION_CHANNEL__FOREGROUND_SERVICE = "foreground_service";
@ -10,5 +20,28 @@ public class Notifications {
// Notification IDs
public static final int FOREGROUND_SERVICE_ID = 1; // must not be 0
public static int chatMessageReceived(Context context, Chat chat, String contactName, String body) {
int id = (System.nanoTime() + " " + body).hashCode();
Intent tapAction = new Intent(context, ChatActivity.class);
tapAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
tapAction.putExtra("JID", chat.jid.toString());
tapAction.putExtra("ACCOUNT", chat.accountId);
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 notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(id, builder.build());
return id;
}
}

View File

@ -6,6 +6,7 @@ import dagger.Module;
import dagger.Provides;
import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.core.NotificationManager;
import org.mercury_im.messenger.core.di.CenterModule;
import javax.inject.Singleton;
@ -24,4 +25,10 @@ public class AppModule {
Application provideApplication() {
return mApplication;
}
@Provides
@Singleton
NotificationManager providerNotificationManager() {
return mApplication;
}
}

View File

@ -6,7 +6,7 @@
android:icon="@drawable/ic_message_black_24dp" />
<item android:id="@+id/entry_contacts"
android:title="@string/entry_contacts"
android:title="@string/entry_contact_list"
android:icon="@drawable/ic_group_black_24dp" />
<item android:id="@+id/entry_accounts"

View File

@ -120,9 +120,10 @@
<string name="action_add_reaction">Add Reaction</string>
<string name="action_message_details">Details</string>
<string name="entry_chats">Chats</string>
<string name="entry_contacts">Contacts</string>
<string name="entry_contact_list">Contact List</string>
<string name="entry_accounts">Accounts</string>
<string name="tab_contacts">Contacts</string>
<string name="tab_bookmarks">Bookmarks</string>
<string name="notification_title">%1$s wrote:</string>
</resources>

View File

@ -0,0 +1,9 @@
package org.mercury_im.messenger.core;
import org.mercury_im.messenger.persistence.pojo.Chat;
public interface NotificationManager {
int chatMessageReceived(Chat chat, String contactName, String body);
}

View File

@ -1,5 +1,6 @@
package org.mercury_im.messenger.core.di;
import org.mercury_im.messenger.core.NotificationManager;
import org.mercury_im.messenger.core.centers.ConnectionCenter;
import org.mercury_im.messenger.core.stores.EntityCapsStore;
import org.mercury_im.messenger.core.stores.PlainMessageStore;
@ -30,8 +31,8 @@ public class CenterModule {
@Singleton
@Provides
static PlainMessageStore provideMessageStore(MessageRepository messageRepository) {
return new PlainMessageStore(messageRepository);
static PlainMessageStore provideMessageStore(MessageRepository messageRepository, NotificationManager notificationManager) {
return new PlainMessageStore(messageRepository, notificationManager);
}
}

View File

@ -8,6 +8,7 @@ import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.mam.MamManager;
import org.jxmpp.jid.EntityBareJid;
import org.mercury_im.messenger.core.NotificationManager;
import org.mercury_im.messenger.core.connection.MercuryConnection;
import org.mercury_im.messenger.persistence.model.MessageModel;
import org.mercury_im.messenger.persistence.repository.MessageRepository;
@ -29,14 +30,24 @@ public class PlainMessageStore {
private final MessageRepository messageRepository;
public PlainMessageStore(MessageRepository messageRepository) {
private final NotificationManager notificationManager;
public PlainMessageStore(MessageRepository messageRepository, NotificationManager notificationManager) {
this.messageRepository = messageRepository;
this.notificationManager = notificationManager;
}
public void newIncomingMessage(long accountId, EntityBareJid from, Message message, Chat chat) {
if (message.getBody() == null) {
return;
}
org.mercury_im.messenger.persistence.pojo.Chat chatPojo = new org.mercury_im.messenger.persistence.pojo.Chat();
chatPojo.jid = from;
chatPojo.accountId = accountId;
chatPojo.peerName = null;
notificationManager.chatMessageReceived(chatPojo, null, message.getBody());
MessageModel messageModel = messageRepository.newMessageModel();
messageModel.setAccountId(accountId);
messageModel.setFrom(chat.getXmppAddressOfChatPartner());