Skip to content

Commit

Permalink
android notif: Use NotificationCompat instead of Notification
Browse files Browse the repository at this point in the history
This would simplify the setSound conditional logic for different API
versions.
  • Loading branch information
kunall17 committed Aug 14, 2018
1 parent 8ffb7c9 commit 0ac642d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

Expand Down Expand Up @@ -54,7 +54,7 @@ public void onMessageReceived(String s, Bundle bundle) {
}

private void showNotification(Context mContext, PushNotificationsProp notificationProp, LinkedHashMap<String, List<MessageInfo>> conversations) {
Notification.Builder builder = new Notification.Builder(getApplicationContext());
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

String type = notificationProp.getRecipientType();
String content = notificationProp.getContent();
Expand Down Expand Up @@ -91,12 +91,12 @@ private void showNotification(Context mContext, PushNotificationsProp notificati
builder.setLargeIcon(avatar);
}
}
builder.setStyle(new Notification.BigTextStyle().bigText(content));
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(content));
} else {
String conversationTitle = String.format(Locale.ENGLISH, "%d messages in %d conversations", totalMessagesCount, conversations.size());
builder.setContentTitle(conversationTitle);
builder.setContentText("Messages from " + TextUtils.join(",", extractNames(conversations)));
Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);
inboxStyle.setSummaryText(String.format(Locale.ENGLISH, "%d conversations", conversations.size()));
buildNotificationContent(conversations, inboxStyle, mContext);
builder.setStyle(inboxStyle);
Expand All @@ -120,16 +120,10 @@ private void showNotification(Context mContext, PushNotificationsProp notificati
Intent dismissIntent = new Intent(mContext, ProxyService.class);
dismissIntent.setAction(NOTIFICATIONS_ACTION_CLEAR);
PendingIntent piDismiss = PendingIntent.getService(mContext, 0, dismissIntent, 0);
Notification.Action action = new Notification.Action(android.R.drawable.ic_menu_close_clear_cancel, "Clear", piDismiss);
NotificationCompat.Action action = new NotificationCompat.Action(android.R.drawable.ic_menu_close_clear_cancel, "Clear", piDismiss);
builder.addAction(action);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttr = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
builder.setSound(Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.zulip), audioAttr);
} else {
builder.setSound(Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.zulip));
}
builder.setSound(Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.zulip));
Notification notification = builder.build();
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.notify(NOTIFICATION_ID, notification);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.zulipmobile.notifications;

import android.app.Notification;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static String extractName(String key) {
return key.split(":")[0];
}

public static void buildNotificationContent(LinkedHashMap<String, List<MessageInfo>> conversations, Notification.InboxStyle inboxStyle, Context mContext) {
public static void buildNotificationContent(LinkedHashMap<String, List<MessageInfo>> conversations, NotificationCompat.InboxStyle inboxStyle, Context mContext) {
for (Map.Entry<String, List<MessageInfo>> entry : conversations.entrySet()) {
String name = extractName(entry.getKey());
List<MessageInfo> messages = entry.getValue();
Expand Down

0 comments on commit 0ac642d

Please sign in to comment.