Commit 6ff79f42 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add a notification channel when target is api level >= 26

parent 20f815c4
package chat.rocket.android.push; package chat.rocket.android.push;
import android.annotation.TargetApi;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ContentResolver; import android.content.ContentResolver;
...@@ -13,13 +15,16 @@ import android.graphics.Bitmap; ...@@ -13,13 +15,16 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.RemoteInput; import android.support.v4.app.RemoteInput;
import android.support.v4.util.SparseArrayCompat; import android.support.v4.util.SparseArrayCompat;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import android.util.Log; import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -31,6 +36,7 @@ import java.net.URL; ...@@ -31,6 +36,7 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import chat.rocket.android.activity.MainActivity; import chat.rocket.android.activity.MainActivity;
import chat.rocket.android.helper.ServerPolicyHelper; import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
...@@ -93,7 +99,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -93,7 +99,7 @@ public class PushNotificationHandler implements PushConstants {
} }
public void createNotification(Context context, Bundle extras) { public void createNotification(Context context, Bundle extras) {
NotificationManager mNotificationManager = NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(context); String appName = getAppName(context);
String packageName = context.getPackageName(); String packageName = context.getPackageName();
...@@ -117,13 +123,6 @@ public class PushNotificationHandler implements PushConstants { ...@@ -117,13 +123,6 @@ public class PushNotificationHandler implements PushConstants {
PendingIntent contentIntent = PendingIntent PendingIntent contentIntent = PendingIntent
.getActivity(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); .getActivity(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setWhen(System.currentTimeMillis())
.setContentTitle(fromHtml(extras.getString(TITLE)))
.setTicker(fromHtml(extras.getString(TITLE)))
.setContentIntent(contentIntent)
.setAutoCancel(true);
SharedPreferences prefs = context SharedPreferences prefs = context
.getSharedPreferences(PushConstants.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); .getSharedPreferences(PushConstants.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
String localIcon = prefs.getString(ICON, null); String localIcon = prefs.getString(ICON, null);
...@@ -135,91 +134,62 @@ public class PushNotificationHandler implements PushConstants { ...@@ -135,91 +134,62 @@ public class PushNotificationHandler implements PushConstants {
Log.d(LOG_TAG, "stored sound=" + soundOption); Log.d(LOG_TAG, "stored sound=" + soundOption);
Log.d(LOG_TAG, "stored vibrate=" + vibrateOption); Log.d(LOG_TAG, "stored vibrate=" + vibrateOption);
/* if (Build.VERSION.SDK_INT >= 26) {
* Notification Vibration String channelId = "rocket-chat-channel";
*/ CharSequence name = "RocketChatMessage";
int importance = NotificationManager.IMPORTANCE_HIGH;
setNotificationVibration(extras, vibrateOption, notificationBuilder); NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.enableLights(true);
notificationManager.createNotificationChannel(channel);
/* Notification.Builder notificationBuilder = new Notification.Builder(context, channelId)
* Notification Icon Color .setWhen(System.currentTimeMillis())
* .setContentTitle(fromHtml(extras.getString(TITLE)))
* Sets the small-icon background color of the notification. .setTicker(fromHtml(extras.getString(TITLE)))
* To use, add the `iconColor` key to plugin android options .setContentIntent(contentIntent)
* .setChannelId(channelId)
*/ .setAutoCancel(true);
setNotificationIconColor(extras.getString("color"), notificationBuilder, localIconColor);
/* setNotificationImportance(extras, channel);
* Notification Icon setNotificationVibration(extras, vibrateOption, channel);
* setNotificationMessage(notId, extras, notificationBuilder);
* Sets the small-icon of the notification. setNotificationCount(context, extras, notificationBuilder);
* setNotificationSmallIcon(extras, packageName, resources, notificationBuilder,
* - checks the plugin options for `icon` key
* - if none, uses the application icon
*
* The icon value must be a string that maps to a drawable resource.
* If no resource is found, falls
*
*/
setNotificationSmallIcon(context, extras, packageName, resources, notificationBuilder,
localIcon); localIcon);
/*
* Notification Large-Icon
*
* Sets the large-icon of the notification
*
* - checks the gcm data for the `image` key
* - checks to see if remote image, loads it.
* - checks to see if assets image, Loads It.
* - checks to see if resource image, LOADS IT!
* - if none, we don't set the large icon
*
*/
setNotificationLargeIcon(context, extras, packageName, resources, notificationBuilder); setNotificationLargeIcon(context, extras, packageName, resources, notificationBuilder);
setNotificationLedColor(extras, channel);
if (soundOption) {
setNotificationSound(context, extras, channel);
}
createActions(context, extras, notificationBuilder, resources, packageName, notId);
notificationManager.notify(notId, notificationBuilder.build());
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setWhen(System.currentTimeMillis())
.setContentTitle(fromHtml(extras.getString(TITLE)))
.setTicker(fromHtml(extras.getString(TITLE)))
.setContentIntent(contentIntent)
.setAutoCancel(true);
/* setNotificationCount(context, extras, notificationBuilder);
* Notification Sound setNotificationVibration(extras, vibrateOption, notificationBuilder);
*/ setNotificationIconColor(extras.getString("color"), notificationBuilder, localIconColor);
setNotificationSmallIcon(extras, packageName, resources, notificationBuilder,
localIcon);
setNotificationLargeIcon(context, extras, packageName, resources, notificationBuilder);
if (soundOption) { if (soundOption) {
setNotificationSound(context, extras, notificationBuilder); setNotificationSound(context, extras, notificationBuilder);
} }
/*
* LED Notification
*/
setNotificationLedColor(extras, notificationBuilder); setNotificationLedColor(extras, notificationBuilder);
/*
* Priority Notification
*/
setNotificationPriority(extras, notificationBuilder); setNotificationPriority(extras, notificationBuilder);
/*
* Notification message
*/
setNotificationMessage(notId, extras, notificationBuilder); setNotificationMessage(notId, extras, notificationBuilder);
/*
* Notification count
*/
setNotificationCount(context, extras, notificationBuilder);
/*
* Notification count
*/
setVisibility(context, extras, notificationBuilder); setVisibility(context, extras, notificationBuilder);
/*
* Notification add actions
*/
createActions(context, extras, notificationBuilder, resources, packageName, notId); createActions(context, extras, notificationBuilder, resources, packageName, notId);
notificationManager.notify(appName, notId, notificationBuilder.build());
mNotificationManager.notify(appName, notId, notificationBuilder.build()); }
} }
private void createActions(Context context, Bundle extras, NotificationCompat.Builder mBuilder, private void createActions(Context context, Bundle extras, NotificationCompat.Builder builder,
Resources resources, String packageName, int notId) { Resources resources, String packageName, int notId) {
Log.d(LOG_TAG, "create actions: with in-line"); Log.d(LOG_TAG, "create actions: with in-line");
String actions = extras.getString(ACTIONS); String actions = extras.getString(ACTIONS);
...@@ -298,16 +268,112 @@ public class PushNotificationHandler implements PushConstants { ...@@ -298,16 +268,112 @@ public class PushNotificationHandler implements PushConstants {
wActions.add(actionBuilder.build()); wActions.add(actionBuilder.build());
if (inline) { if (inline) {
mBuilder.addAction(wAction); builder.addAction(wAction);
} else {
builder.addAction(
resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent);
}
wAction = null;
pIntent = null;
}
builder.extend(new NotificationCompat.WearableExtender().addActions(wActions));
wActions.clear();
} catch (JSONException e) {
// nope
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
private void createActions(Context context, Bundle extras, Notification.Builder builder,
Resources resources, String packageName, int notId) {
Log.d(LOG_TAG, "create actions: with in-line");
String actions = extras.getString(ACTIONS);
if (actions == null) {
return;
}
try {
JSONArray actionsArray = new JSONArray(actions);
ArrayList<Notification.Action> wActions = new ArrayList<>();
for (int i = 0; i < actionsArray.length(); i++) {
int min = 1;
int max = 2000000000;
Random random = new Random();
int uniquePendingIntentRequestCode = random.nextInt((max - min) + 1) + min;
Log.d(LOG_TAG, "adding action");
JSONObject action = actionsArray.getJSONObject(i);
Log.d(LOG_TAG, "adding callback = " + action.getString(CALLBACK));
boolean foreground = action.optBoolean(FOREGROUND, true);
boolean inline = action.optBoolean("inline", false);
Intent intent;
PendingIntent pIntent;
if (inline) {
Log.d(LOG_TAG, "Version: " + android.os.Build.VERSION.SDK_INT + " = "
+ android.os.Build.VERSION_CODES.M);
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
Log.d(LOG_TAG, "push activity");
intent = new Intent(context, MainActivity.class);
} else {
Log.d(LOG_TAG, "push receiver");
intent = new Intent(context, BackgroundActionButtonHandler.class);
}
updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
Log.d(LOG_TAG, "push activity for notId " + notId);
pIntent =
PendingIntent.getActivity(context, uniquePendingIntentRequestCode, intent,
PendingIntent.FLAG_ONE_SHOT);
} else {
Log.d(LOG_TAG, "push receiver for notId " + notId);
pIntent = PendingIntent
.getBroadcast(context, uniquePendingIntentRequestCode, intent,
PendingIntent.FLAG_ONE_SHOT);
}
} else if (foreground) {
intent = new Intent(context, MainActivity.class);
updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
pIntent = PendingIntent
.getActivity(context, uniquePendingIntentRequestCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else {
intent = new Intent(context, BackgroundActionButtonHandler.class);
updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
pIntent = PendingIntent
.getBroadcast(context, uniquePendingIntentRequestCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
Notification.Action.Builder actionBuilder = new Notification.Action.Builder(
resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent);
android.app.RemoteInput remoteInput;
if (inline) {
Log.d(LOG_TAG, "create remote input");
String replyLabel = "Enter your reply here";
remoteInput = new android.app.RemoteInput.Builder(INLINE_REPLY)
.setLabel(replyLabel)
.build();
actionBuilder.addRemoteInput(remoteInput);
}
Notification.Action wAction = actionBuilder.build();
wActions.add(actionBuilder.build());
if (inline) {
builder.addAction(wAction);
} else { } else {
mBuilder.addAction( builder.addAction(
resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName), resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent); action.getString(TITLE), pIntent);
} }
wAction = null; wAction = null;
pIntent = null; pIntent = null;
} }
mBuilder.extend(new NotificationCompat.WearableExtender().addActions(wActions)); builder.extend(new Notification.WearableExtender().addActions(wActions));
wActions.clear(); wActions.clear();
} catch (JSONException e) { } catch (JSONException e) {
// nope // nope
...@@ -315,15 +381,24 @@ public class PushNotificationHandler implements PushConstants { ...@@ -315,15 +381,24 @@ public class PushNotificationHandler implements PushConstants {
} }
private void setNotificationCount(Context context, Bundle extras, private void setNotificationCount(Context context, Bundle extras,
NotificationCompat.Builder mBuilder) { NotificationCompat.Builder builder) {
int count = extractBadgeCount(extras);
if (count >= 0) {
Log.d(LOG_TAG, "count =[" + count + "]");
builder.setNumber(count);
}
}
private void setNotificationCount(Context context, Bundle extras,
Notification.Builder builder) {
int count = extractBadgeCount(extras); int count = extractBadgeCount(extras);
if (count >= 0) { if (count >= 0) {
Log.d(LOG_TAG, "count =[" + count + "]"); Log.d(LOG_TAG, "count =[" + count + "]");
mBuilder.setNumber(count); builder.setNumber(count);
} }
} }
private void setVisibility(Context context, Bundle extras, NotificationCompat.Builder mBuilder) { private void setVisibility(Context context, Bundle extras, NotificationCompat.Builder builder) {
String visibilityStr = extras.getString(VISIBILITY); String visibilityStr = extras.getString(VISIBILITY);
if (visibilityStr == null) { if (visibilityStr == null) {
return; return;
...@@ -333,7 +408,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -333,7 +408,7 @@ public class PushNotificationHandler implements PushConstants {
Integer visibility = Integer.parseInt(visibilityStr); Integer visibility = Integer.parseInt(visibilityStr);
if (visibility >= NotificationCompat.VISIBILITY_SECRET if (visibility >= NotificationCompat.VISIBILITY_SECRET
&& visibility <= NotificationCompat.VISIBILITY_PUBLIC) { && visibility <= NotificationCompat.VISIBILITY_PUBLIC) {
mBuilder.setVisibility(visibility); builder.setVisibility(visibility);
} else { } else {
Log.e(LOG_TAG, "Visibility parameter must be between -1 and 1"); Log.e(LOG_TAG, "Visibility parameter must be between -1 and 1");
} }
...@@ -343,7 +418,28 @@ public class PushNotificationHandler implements PushConstants { ...@@ -343,7 +418,28 @@ public class PushNotificationHandler implements PushConstants {
} }
private void setNotificationVibration(Bundle extras, Boolean vibrateOption, private void setNotificationVibration(Bundle extras, Boolean vibrateOption,
NotificationCompat.Builder mBuilder) { NotificationCompat.Builder builder) {
String vibrationPattern = extras.getString(VIBRATION_PATTERN);
if (vibrationPattern != null) {
String[] items = vibrationPattern.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
long[] results = new long[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Long.parseLong(items[i].trim());
} catch (NumberFormatException nfe) {
}
}
builder.setVibrate(results);
} else {
if (vibrateOption) {
builder.setDefaults(Notification.DEFAULT_VIBRATE);
}
}
}
@RequiresApi(api = 26)
private void setNotificationVibration(Bundle extras, Boolean vibrateOption,
NotificationChannel channel) {
String vibrationPattern = extras.getString(VIBRATION_PATTERN); String vibrationPattern = extras.getString(VIBRATION_PATTERN);
if (vibrationPattern != null) { if (vibrationPattern != null) {
String[] items = vibrationPattern.replaceAll("\\[", "").replaceAll("\\]", "").split(","); String[] items = vibrationPattern.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
...@@ -354,23 +450,23 @@ public class PushNotificationHandler implements PushConstants { ...@@ -354,23 +450,23 @@ public class PushNotificationHandler implements PushConstants {
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
} }
} }
mBuilder.setVibrate(results); channel.setVibrationPattern(results);
} else { } else {
if (vibrateOption) { if (vibrateOption) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); channel.enableVibration(true);
} }
} }
} }
private void setNotificationMessage(int notId, Bundle extras, private void setNotificationMessage(int notId, Bundle extras,
NotificationCompat.Builder mBuilder) { NotificationCompat.Builder builder) {
String message = extras.getString(MESSAGE); String message = extras.getString(MESSAGE);
String style = extras.getString(STYLE, STYLE_TEXT); String style = extras.getString(STYLE, STYLE_TEXT);
if (STYLE_INBOX.equals(style)) { if (STYLE_INBOX.equals(style)) {
setNotification(notId, message); setNotification(notId, message);
mBuilder.setContentText(fromHtml(message)); builder.setContentText(fromHtml(message));
ArrayList<String> messageList = getMessageList(notId); ArrayList<String> messageList = getMessageList(notId);
Integer sizeList = messageList.size(); Integer sizeList = messageList.size();
...@@ -389,13 +485,13 @@ public class PushNotificationHandler implements PushConstants { ...@@ -389,13 +485,13 @@ public class PushNotificationHandler implements PushConstants {
notificationInbox.addLine(fromHtml(messageList.get(i))); notificationInbox.addLine(fromHtml(messageList.get(i)));
} }
mBuilder.setStyle(notificationInbox); builder.setStyle(notificationInbox);
} else { } else {
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
if (message != null) { if (message != null) {
bigText.bigText(fromHtml(message)); bigText.bigText(fromHtml(message));
bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));
mBuilder.setStyle(bigText); builder.setStyle(bigText);
} }
} }
} else if (STYLE_PICTURE.equals(style)) { } else if (STYLE_PICTURE.equals(style)) {
...@@ -406,17 +502,17 @@ public class PushNotificationHandler implements PushConstants { ...@@ -406,17 +502,17 @@ public class PushNotificationHandler implements PushConstants {
bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE))); bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE)));
bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT))); bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT)));
mBuilder.setContentTitle(fromHtml(extras.getString(TITLE))); builder.setContentTitle(fromHtml(extras.getString(TITLE)));
mBuilder.setContentText(fromHtml(message)); builder.setContentText(fromHtml(message));
mBuilder.setStyle(bigPicture); builder.setStyle(bigPicture);
} else { } else {
setNotification(notId, ""); setNotification(notId, "");
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
if (message != null) { if (message != null) {
mBuilder.setContentText(fromHtml(message)); builder.setContentText(fromHtml(message));
bigText.bigText(fromHtml(message)); bigText.bigText(fromHtml(message));
bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));
...@@ -426,30 +522,145 @@ public class PushNotificationHandler implements PushConstants { ...@@ -426,30 +522,145 @@ public class PushNotificationHandler implements PushConstants {
bigText.setSummaryText(fromHtml(summaryText)); bigText.setSummaryText(fromHtml(summaryText));
} }
mBuilder.setStyle(bigText); builder.setStyle(bigText);
} }
} }
} }
private void setNotificationMessage(int notId, Bundle extras,
Notification.Builder builder) {
String message = extras.getString(MESSAGE);
String style = extras.getString(STYLE, STYLE_TEXT);
if (STYLE_INBOX.equals(style)) {
setNotification(notId, message);
builder.setContentText(fromHtml(message));
ArrayList<String> messageList = getMessageList(notId);
Integer sizeList = messageList.size();
if (sizeList > 1) {
String sizeListMessage = sizeList.toString();
String stacking = sizeList + " more";
if (extras.getString(SUMMARY_TEXT) != null) {
stacking = extras.getString(SUMMARY_TEXT);
stacking = stacking.replace("%n%", sizeListMessage);
}
Notification.InboxStyle notificationInbox = new Notification.InboxStyle()
.setBigContentTitle(fromHtml(extras.getString(TITLE)))
.setSummaryText(fromHtml(stacking));
for (int i = messageList.size() - 1; i >= 0; i--) {
notificationInbox.addLine(fromHtml(messageList.get(i)));
}
builder.setStyle(notificationInbox);
} else {
Notification.BigTextStyle bigText = new Notification.BigTextStyle();
if (message != null) {
bigText.bigText(fromHtml(message));
bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));
builder.setStyle(bigText);
}
}
} else if (STYLE_PICTURE.equals(style)) {
setNotification(notId, "");
Notification.BigPictureStyle bigPicture = new Notification.BigPictureStyle();
bigPicture.bigPicture(getBitmapFromURL(extras.getString(PICTURE)));
bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE)));
bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT)));
builder.setContentTitle(fromHtml(extras.getString(TITLE)));
builder.setContentText(fromHtml(message));
builder.setStyle(bigPicture);
} else {
setNotification(notId, "");
Notification.BigTextStyle bigText = new Notification.BigTextStyle();
if (message != null) {
builder.setContentText(fromHtml(message));
bigText.bigText(fromHtml(message));
bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));
String summaryText = extras.getString(SUMMARY_TEXT);
if (summaryText != null) {
bigText.setSummaryText(fromHtml(summaryText));
}
builder.setStyle(bigText);
}
}
}
private void setNotificationSound(Context context, Bundle extras,
NotificationCompat.Builder builder) {
String soundname = extras.getString(SOUNDNAME);
if (soundname == null) {
soundname = extras.getString(SOUND);
}
if (SOUND_RINGTONE.equals(soundname)) {
builder.setSound(android.provider.Settings.System.DEFAULT_RINGTONE_URI);
} else if (soundname != null && !soundname.contentEquals(SOUND_DEFAULT)) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString());
builder.setSound(sound);
} else {
builder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
}
}
@RequiresApi(api = 26)
private void setNotificationSound(Context context, Bundle extras, private void setNotificationSound(Context context, Bundle extras,
NotificationCompat.Builder mBuilder) { NotificationChannel channel) {
String soundname = extras.getString(SOUNDNAME); String soundname = extras.getString(SOUNDNAME);
if (soundname == null) { if (soundname == null) {
soundname = extras.getString(SOUND); soundname = extras.getString(SOUND);
} }
if (SOUND_RINGTONE.equals(soundname)) { if (SOUND_RINGTONE.equals(soundname)) {
mBuilder.setSound(android.provider.Settings.System.DEFAULT_RINGTONE_URI); channel.setSound(android.provider.Settings.System.DEFAULT_RINGTONE_URI,
Notification.AUDIO_ATTRIBUTES_DEFAULT);
} else if (soundname != null && !soundname.contentEquals(SOUND_DEFAULT)) { } else if (soundname != null && !soundname.contentEquals(SOUND_DEFAULT)) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname); + "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString()); Log.d(LOG_TAG, sound.toString());
mBuilder.setSound(sound); channel.setSound(sound, Notification.AUDIO_ATTRIBUTES_DEFAULT);
} else {
channel.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI,
Notification.AUDIO_ATTRIBUTES_DEFAULT);
}
}
private void setNotificationLedColor(Bundle extras, NotificationCompat.Builder builder) {
String ledColor = extras.getString(LED_COLOR);
if (ledColor == null) {
return;
}
// Converts parse Int Array from ledColor
String[] items = ledColor.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i].trim());
} catch (NumberFormatException nfe) {
}
}
if (results.length == 4) {
builder.setLights(Color.argb(results[0], results[1], results[2], results[3]), 500, 500);
} else { } else {
mBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI); Log.e(LOG_TAG, "ledColor parameter must be an array of length == 4 (ARGB)");
} }
} }
private void setNotificationLedColor(Bundle extras, NotificationCompat.Builder mBuilder) { @RequiresApi(api = 26)
private void setNotificationLedColor(Bundle extras, NotificationChannel channel) {
String ledColor = extras.getString(LED_COLOR); String ledColor = extras.getString(LED_COLOR);
if (ledColor == null) { if (ledColor == null) {
return; return;
...@@ -466,14 +677,14 @@ public class PushNotificationHandler implements PushConstants { ...@@ -466,14 +677,14 @@ public class PushNotificationHandler implements PushConstants {
} }
if (results.length == 4) { if (results.length == 4) {
mBuilder.setLights(Color.argb(results[0], results[1], results[2], results[3]), 500, 500); channel.setLightColor(Color.argb(results[0], results[1], results[2], results[3]));
} else { } else {
Log.e(LOG_TAG, "ledColor parameter must be an array of length == 4 (ARGB)"); Log.e(LOG_TAG, "ledColor parameter must be an array of length == 4 (ARGB)");
} }
} }
private void setNotificationPriority(Bundle extras, NotificationCompat.Builder mBuilder) { private void setNotificationPriority(Bundle extras, NotificationCompat.Builder builder) {
String priorityStr = extras.getString(PRIORITY); String priorityStr = extras.getString(PRIORITY);
if (priorityStr == null) { if (priorityStr == null) {
return; return;
...@@ -483,7 +694,28 @@ public class PushNotificationHandler implements PushConstants { ...@@ -483,7 +694,28 @@ public class PushNotificationHandler implements PushConstants {
Integer priority = Integer.parseInt(priorityStr); Integer priority = Integer.parseInt(priorityStr);
if (priority >= NotificationCompat.PRIORITY_MIN if (priority >= NotificationCompat.PRIORITY_MIN
&& priority <= NotificationCompat.PRIORITY_MAX) { && priority <= NotificationCompat.PRIORITY_MAX) {
mBuilder.setPriority(priority); builder.setPriority(priority);
} else {
Log.e(LOG_TAG, "Priority parameter must be between -2 and 2");
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@RequiresApi(api = 26)
private void setNotificationImportance(Bundle extras, NotificationChannel channel) {
String priorityStr = extras.getString(PRIORITY);
if (priorityStr == null) {
return;
}
try {
Integer priority = Integer.parseInt(priorityStr);
if (priority >= NotificationCompat.PRIORITY_MIN
&& priority <= NotificationCompat.PRIORITY_MAX) {
channel.setImportance(priority);
} else { } else {
Log.e(LOG_TAG, "Priority parameter must be between -2 and 2"); Log.e(LOG_TAG, "Priority parameter must be between -2 and 2");
} }
...@@ -494,14 +726,14 @@ public class PushNotificationHandler implements PushConstants { ...@@ -494,14 +726,14 @@ public class PushNotificationHandler implements PushConstants {
} }
private void setNotificationLargeIcon(Context context, Bundle extras, String packageName, private void setNotificationLargeIcon(Context context, Bundle extras, String packageName,
Resources resources, NotificationCompat.Builder mBuilder) { Resources resources, NotificationCompat.Builder builder) {
String gcmLargeIcon = extras.getString(IMAGE); // from gcm String gcmLargeIcon = extras.getString(IMAGE); // from gcm
if (gcmLargeIcon == null || "".equals(gcmLargeIcon)) { if (gcmLargeIcon == null || "".equals(gcmLargeIcon)) {
return; return;
} }
if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) { if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) {
mBuilder.setLargeIcon(getBitmapFromURL(gcmLargeIcon)); builder.setLargeIcon(getBitmapFromURL(gcmLargeIcon));
Log.d(LOG_TAG, "using remote large-icon from gcm"); Log.d(LOG_TAG, "using remote large-icon from gcm");
} else { } else {
AssetManager assetManager = context.getAssets(); AssetManager assetManager = context.getAssets();
...@@ -509,13 +741,13 @@ public class PushNotificationHandler implements PushConstants { ...@@ -509,13 +741,13 @@ public class PushNotificationHandler implements PushConstants {
try { try {
istr = assetManager.open(gcmLargeIcon); istr = assetManager.open(gcmLargeIcon);
Bitmap bitmap = BitmapFactory.decodeStream(istr); Bitmap bitmap = BitmapFactory.decodeStream(istr);
mBuilder.setLargeIcon(bitmap); builder.setLargeIcon(bitmap);
Log.d(LOG_TAG, "using assets large-icon from gcm"); Log.d(LOG_TAG, "using assets large-icon from gcm");
} catch (IOException e) { } catch (IOException e) {
int largeIconId = resources.getIdentifier(gcmLargeIcon, DRAWABLE, packageName); int largeIconId = resources.getIdentifier(gcmLargeIcon, DRAWABLE, packageName);
if (largeIconId != 0) { if (largeIconId != 0) {
Bitmap largeIconBitmap = BitmapFactory.decodeResource(resources, largeIconId); Bitmap largeIconBitmap = BitmapFactory.decodeResource(resources, largeIconId);
mBuilder.setLargeIcon(largeIconBitmap); builder.setLargeIcon(largeIconBitmap);
Log.d(LOG_TAG, "using resources large-icon from gcm"); Log.d(LOG_TAG, "using resources large-icon from gcm");
} else { } else {
Log.d(LOG_TAG, "Not setting large icon"); Log.d(LOG_TAG, "Not setting large icon");
...@@ -524,8 +756,59 @@ public class PushNotificationHandler implements PushConstants { ...@@ -524,8 +756,59 @@ public class PushNotificationHandler implements PushConstants {
} }
} }
private void setNotificationSmallIcon(Context context, Bundle extras, String packageName, private void setNotificationLargeIcon(Context context, Bundle extras, String packageName,
Resources resources, NotificationCompat.Builder mBuilder, Resources resources, Notification.Builder builder) {
String gcmLargeIcon = extras.getString(IMAGE); // from gcm
if (gcmLargeIcon == null || "".equals(gcmLargeIcon)) {
return;
}
if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) {
builder.setLargeIcon(getBitmapFromURL(gcmLargeIcon));
Log.d(LOG_TAG, "using remote large-icon from gcm");
} else {
AssetManager assetManager = context.getAssets();
InputStream istr;
try {
istr = assetManager.open(gcmLargeIcon);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
builder.setLargeIcon(bitmap);
Log.d(LOG_TAG, "using assets large-icon from gcm");
} catch (IOException e) {
int largeIconId = resources.getIdentifier(gcmLargeIcon, DRAWABLE, packageName);
if (largeIconId != 0) {
Bitmap largeIconBitmap = BitmapFactory.decodeResource(resources, largeIconId);
builder.setLargeIcon(largeIconBitmap);
Log.d(LOG_TAG, "using resources large-icon from gcm");
} else {
Log.d(LOG_TAG, "Not setting large icon");
}
}
}
}
private void setNotificationSmallIcon(Bundle extras, String packageName,
Resources resources, NotificationCompat.Builder builder,
String localIcon) {
int iconId = 0;
String icon = extras.getString(ICON);
if (icon != null && !"".equals(icon)) {
iconId = resources.getIdentifier(icon, DRAWABLE, packageName);
Log.d(LOG_TAG, "using icon from plugin options");
} else if (localIcon != null && !"".equals(localIcon)) {
iconId = resources.getIdentifier(localIcon, DRAWABLE, packageName);
Log.d(LOG_TAG, "using icon from plugin options");
}
if (iconId == 0) {
Log.d(LOG_TAG, "no icon resource found - using default icon");
iconId = resources.getIdentifier("rocket_chat_notification", DRAWABLE, packageName);
}
builder.setSmallIcon(iconId);
}
private void setNotificationSmallIcon(Bundle extras, String packageName,
Resources resources, Notification.Builder builder,
String localIcon) { String localIcon) {
int iconId = 0; int iconId = 0;
String icon = extras.getString(ICON); String icon = extras.getString(ICON);
...@@ -540,10 +823,10 @@ public class PushNotificationHandler implements PushConstants { ...@@ -540,10 +823,10 @@ public class PushNotificationHandler implements PushConstants {
Log.d(LOG_TAG, "no icon resource found - using default icon"); Log.d(LOG_TAG, "no icon resource found - using default icon");
iconId = resources.getIdentifier("rocket_chat_notification", DRAWABLE, packageName); iconId = resources.getIdentifier("rocket_chat_notification", DRAWABLE, packageName);
} }
mBuilder.setSmallIcon(iconId); builder.setSmallIcon(iconId);
} }
private void setNotificationIconColor(String color, NotificationCompat.Builder mBuilder, private void setNotificationIconColor(String color, NotificationCompat.Builder builder,
String localIconColor) { String localIconColor) {
int iconColor = 0; int iconColor = 0;
if (color != null && !"".equals(color)) { if (color != null && !"".equals(color)) {
...@@ -560,7 +843,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -560,7 +843,7 @@ public class PushNotificationHandler implements PushConstants {
} }
} }
if (iconColor != 0) { if (iconColor != 0) {
mBuilder.setColor(iconColor); builder.setColor(iconColor);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment