define a notification channel, closes #708

This commit is contained in:
B. Petersen
2019-02-09 01:33:22 +01:00
parent 4866b5ea40
commit 46ac343181
4 changed files with 48 additions and 0 deletions
@@ -1,6 +1,9 @@
package org.thoughtcrime.securesms.connect;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
@@ -107,6 +110,24 @@ public class KeepAliveService extends Service {
builder.setWhen(0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.notification_permanent);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
createFgNotificationChannel(this);
builder.setChannelId(FG_CHANNEL_ID);
}
return builder.build();
}
private static boolean ch_created = false;
private static final String FG_CHANNEL_ID = "dc_foreground_notification_ch";
@TargetApi(Build.VERSION_CODES.O)
static private void createFgNotificationChannel(Context context) {
if(!ch_created) {
ch_created = true;
NotificationChannel channel = new NotificationChannel(FG_CHANNEL_ID,
"Receive messages in background.", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Ensure reliable message receiving.");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
@@ -1,9 +1,13 @@
package org.thoughtcrime.securesms.notifications;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
@@ -86,4 +90,18 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui
return blinkPattern.split(",");
}
private static boolean ch_created = false;
protected static final String MSG_CHANNEL_ID = "dc_message_notification_ch";
@TargetApi(Build.VERSION_CODES.O)
protected void createMsgNotificationChannel(Context context) {
if(!ch_created) {
ch_created = true;
NotificationChannel channel = new NotificationChannel(MSG_CHANNEL_ID,
"Message notifications", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Informs about new messages.");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
@@ -4,6 +4,7 @@ import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
@@ -41,6 +42,10 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu
setColor(context.getResources().getColor(R.color.delta_primary));
setSmallIcon(R.drawable.icon_notification);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
createMsgNotificationChannel(context);
setChannelId(MSG_CHANNEL_ID);
}
setContentTitle(context.getString(R.string.app_name));
setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, ConversationListActivity.class), 0));
setCategory(NotificationCompat.CATEGORY_MESSAGE);
@@ -51,6 +51,10 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
super(context, privacy);
setSmallIcon(R.drawable.icon_notification);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
createMsgNotificationChannel(context);
setChannelId(MSG_CHANNEL_ID);
}
setColor(context.getResources().getColor(R.color.delta_primary));
setPriority(Prefs.getNotificationPriority(context));
setCategory(NotificationCompat.CATEGORY_MESSAGE);