diff --git a/src/main/java/io/lbry/browser/LbrynetMessagingService.java b/src/main/java/io/lbry/browser/LbrynetMessagingService.java index 958d0e1..94745fe 100644 --- a/src/main/java/io/lbry/browser/LbrynetMessagingService.java +++ b/src/main/java/io/lbry/browser/LbrynetMessagingService.java @@ -5,6 +5,7 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; @@ -15,21 +16,45 @@ import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; +import io.lbry.browser.reactmodules.UtilityModule; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class LbrynetMessagingService extends FirebaseMessagingService { private static final String TAG = "LbrynetMessagingService"; private static final String NOTIFICATION_CHANNEL_ID = "io.lbry.browser.LBRY_ENGAGEMENT_CHANNEL"; + private static final String TYPE_SUBSCRIPTION = "subscription"; + + private static final String TYPE_REWARD = "reward"; + + private static final String TYPE_INTERESTS = "interests"; + + private static final String TYPE_CREATOR = "creator"; + @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); - // Check if message contains a notification payload. - RemoteMessage.Notification remoteNotification = remoteMessage.getNotification(); - if (remoteNotification != null) { - Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); - sendNotification(remoteNotification.getTitle(), remoteNotification.getBody()); + Map payload = remoteMessage.getData(); + String type = null; + String url = null; + if (payload != null) { + type = payload.get("type"); + url = payload.get("target"); + } + + if (type != null && getEnabledTypes().indexOf(type) > -1) { + // Check if message contains a notification payload. + RemoteMessage.Notification remoteNotification = remoteMessage.getNotification(); + if (remoteNotification != null) { + Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); + sendNotification(remoteNotification.getTitle(), remoteNotification.getBody(), type, url); + } } } @@ -60,12 +85,21 @@ public class LbrynetMessagingService extends FirebaseMessagingService { * * @param messageBody FCM message body received. */ - private void sendNotification(String title, String messageBody) { - Intent intent = new Intent(this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - // TODO: Update pending intent based on notification data (eg. open a LBRY URI or the Rewards page?) - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, - PendingIntent.FLAG_ONE_SHOT); + private void sendNotification(String title, String messageBody, String type, String url) { + //Intent intent = new Intent(this, MainActivity.class); + //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + if (url == null) { + if (TYPE_REWARD.equals(type)) { + url = "lbry://?rewards"; + } else { + // default to home page + url = "lbry://?discover"; + } + } + + Intent launchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, launchIntent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = @@ -90,4 +124,24 @@ public class LbrynetMessagingService extends FirebaseMessagingService { notificationManager.notify(9898, notificationBuilder.build()); } + + public List getEnabledTypes() { + SharedPreferences sp = getSharedPreferences(MainActivity.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); + List enabledTypes = new ArrayList(); + + if (sp.getBoolean(UtilityModule.RECEIVE_SUBSCRIPTION_NOTIFICATIONS, true)) { + enabledTypes.add(TYPE_SUBSCRIPTION); + } + if (sp.getBoolean(UtilityModule.RECEIVE_REWARD_NOTIFICATIONS, true)) { + enabledTypes.add(TYPE_REWARD); + } + if (sp.getBoolean(UtilityModule.RECEIVE_INTERESTS_NOTIFICATIONS, true)) { + enabledTypes.add(TYPE_INTERESTS); + } + if (sp.getBoolean(UtilityModule.RECEIVE_CREATOR_NOTIFICATIONS, true)) { + enabledTypes.add(TYPE_CREATOR); + } + + return enabledTypes; + } } \ No newline at end of file diff --git a/src/main/java/io/lbry/browser/MainActivity.java b/src/main/java/io/lbry/browser/MainActivity.java index f4eee2a..b386592 100644 --- a/src/main/java/io/lbry/browser/MainActivity.java +++ b/src/main/java/io/lbry/browser/MainActivity.java @@ -534,6 +534,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.cancel(sourceNotificationId); } + + // check for target (notification payload) + String target = intent.getStringExtra("target"); + if (target != null && target.trim().length() > 0) { + ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + if (reactContext != null) { + WritableMap params = Arguments.createMap(); + params.putString("url", target); + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("onNotificationTargetLaunch", params); + } + } } super.onNewIntent(intent); diff --git a/src/main/java/io/lbry/browser/reactmodules/FirebaseModule.java b/src/main/java/io/lbry/browser/reactmodules/FirebaseModule.java index d75aacf..b16af22 100644 --- a/src/main/java/io/lbry/browser/reactmodules/FirebaseModule.java +++ b/src/main/java/io/lbry/browser/reactmodules/FirebaseModule.java @@ -98,7 +98,7 @@ public class FirebaseModule extends ReactContextBaseJavaModule { @Override public void onComplete(Task task) { if (!task.isSuccessful()) { - promise.reject("getInstanceId failed"); + promise.reject("Firebase getInstanceId call failed"); return; } diff --git a/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java index 09bf33f..dc5107d 100644 --- a/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java +++ b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java @@ -52,6 +52,15 @@ public class UtilityModule extends ReactContextBaseJavaModule { public static final String ACTION_NOTIFICATION_LATER = "io.lbry.browser.ACTION_NOTIFICATION_LATER"; + public static final String RECEIVE_SUBSCRIPTION_NOTIFICATIONS = "receiveSubscriptionNotifications"; + + public static final String RECEIVE_REWARD_NOTIFICATIONS = "receiveRewardNotifications"; + + public static final String RECEIVE_INTERESTS_NOTIFICATIONS = "receiveInterestsNotifications"; + + public static final String RECEIVE_CREATOR_NOTIFICATIONS = "receiveCreatorNotifications"; + + private Context context; private KeyStore keyStore; @@ -377,4 +386,14 @@ public class UtilityModule extends ReactContextBaseJavaModule { Intent.createChooser(intent, "Select a file"), MainActivity.DOCUMENT_PICKER_RESULT_CODE); } } + + @ReactMethod + public void setNativeBooleanSetting(String key, boolean value) { + if (context != null) { + SharedPreferences sp = context.getSharedPreferences(MainActivity.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + editor.putBoolean(key, value); + editor.commit(); + } + } }