RN-138 Android notification preferences (#889)

* Fix Android and iOS flow

* Own review

* RN-138 Android notification preferences

* Feedback review

* Feedback review 2
This commit is contained in:
enahum
2017-09-13 12:00:16 -03:00
committed by GitHub
parent 7aebcbd52e
commit e0cfefcdff
17 changed files with 632 additions and 48 deletions

View File

@@ -7,6 +7,10 @@ import android.content.res.Resources;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Build;
import android.app.Notification;
@@ -23,6 +27,8 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.helpers.ApplicationBadgeHelper;
import android.util.Log;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME;
public class CustomPushNotification extends PushNotification {
@@ -99,7 +105,13 @@ public class CustomPushNotification extends PushNotification {
@Override
protected void postNotification(int id, Notification notification) {
if (!mAppLifecycleFacade.isAppVisible()) {
boolean force = false;
Bundle bundle = notification.extras;
if (bundle != null) {
force = bundle.getBoolean("localTest");
}
if (!mAppLifecycleFacade.isAppVisible() || force) {
super.postNotification(id, notification);
}
}
@@ -108,9 +120,10 @@ public class CustomPushNotification extends PushNotification {
protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
final Resources res = mContext.getResources();
String packageName = mContext.getPackageName();
NotificationPreferencesModule notificationPreferences = NotificationPreferencesModule.getInstance();
// First, get a builder initialized with defaults from the core class.
final Notification.Builder notification = super.getNotificationBuilder(intent);
final Notification.Builder notification = new Notification.Builder(mContext);
Bundle bundle = mNotificationProps.asBundle();
String title = bundle.getString("title");
if (title == null) {
@@ -120,13 +133,18 @@ public class CustomPushNotification extends PushNotification {
String channelId = bundle.getString("channel_id");
String postId = bundle.getString("post_id");
int notificationId = channelId.hashCode();
int notificationId = channelId != null ? channelId.hashCode() : MESSAGE_NOTIFICATION_ID;
String message = bundle.getString("message");
String subText = bundle.getString("subText");
String numberString = bundle.getString("badge");
String smallIcon = bundle.getString("smallIcon");
String largeIcon = bundle.getString("largeIcon");
Bundle b = bundle.getBundle("userInfo");
if (b != null) {
notification.addExtras(b);
}
int smallIconResId;
int largeIconResId;
@@ -157,10 +175,12 @@ public class CustomPushNotification extends PushNotification {
int numMessages = getMessageCountInChannel(channelId);
notification
.setContentIntent(intent)
.setGroupSummary(true)
.setSmallIcon(smallIconResId)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setPriority(Notification.PRIORITY_HIGH);
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);
if (numMessages == 1) {
notification
@@ -223,6 +243,27 @@ public class CustomPushNotification extends PushNotification {
notification.setSubText(subText);
}
String soundUri = notificationPreferences.getNotificationSound();
if (soundUri != null) {
if (soundUri != "none") {
notification.setSound(Uri.parse(soundUri), AudioManager.STREAM_NOTIFICATION);
}
} else {
Uri defaultUri = RingtoneManager.getActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_NOTIFICATION);
notification.setSound(defaultUri, AudioManager.STREAM_NOTIFICATION);
}
boolean vibrate = notificationPreferences.getShouldVibrate();
if (vibrate) {
// Each element then alternates between delay, vibrate, sleep, vibrate, sleep
notification.setVibrate(new long[] {1000, 1000, 500, 1000, 500});
}
boolean blink = notificationPreferences.getShouldBlink();
if (blink) {
notification.setLights(Color.CYAN, 500, 500);
}
return notification;
}
@@ -236,7 +277,7 @@ public class CustomPushNotification extends PushNotification {
return (Integer)objCount;
}
return 0;
return 1;
}
private void cancelNotification(Bundle data, int notificationId) {