handle remote notification payloads (#737)
* handle remote notification payloads * handle intent extras for background notifications
This commit is contained in:
parent
6e6ed07890
commit
42cf11358e
4 changed files with 96 additions and 12 deletions
|
@ -5,6 +5,7 @@ import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
@ -15,21 +16,45 @@ import android.util.Log;
|
||||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||||
import com.google.firebase.messaging.RemoteMessage;
|
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 {
|
public class LbrynetMessagingService extends FirebaseMessagingService {
|
||||||
|
|
||||||
private static final String TAG = "LbrynetMessagingService";
|
private static final String TAG = "LbrynetMessagingService";
|
||||||
|
|
||||||
private static final String NOTIFICATION_CHANNEL_ID = "io.lbry.browser.LBRY_ENGAGEMENT_CHANNEL";
|
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
|
@Override
|
||||||
public void onMessageReceived(RemoteMessage remoteMessage) {
|
public void onMessageReceived(RemoteMessage remoteMessage) {
|
||||||
Log.d(TAG, "From: " + remoteMessage.getFrom());
|
Log.d(TAG, "From: " + remoteMessage.getFrom());
|
||||||
|
|
||||||
// Check if message contains a notification payload.
|
Map<String, String> payload = remoteMessage.getData();
|
||||||
RemoteMessage.Notification remoteNotification = remoteMessage.getNotification();
|
String type = null;
|
||||||
if (remoteNotification != null) {
|
String url = null;
|
||||||
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
|
if (payload != null) {
|
||||||
sendNotification(remoteNotification.getTitle(), remoteNotification.getBody());
|
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.
|
* @param messageBody FCM message body received.
|
||||||
*/
|
*/
|
||||||
private void sendNotification(String title, String messageBody) {
|
private void sendNotification(String title, String messageBody, String type, String url) {
|
||||||
Intent intent = new Intent(this, MainActivity.class);
|
//Intent intent = new Intent(this, MainActivity.class);
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
// TODO: Update pending intent based on notification data (eg. open a LBRY URI or the Rewards page?)
|
if (url == null) {
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
|
if (TYPE_REWARD.equals(type)) {
|
||||||
PendingIntent.FLAG_ONE_SHOT);
|
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);
|
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||||
NotificationCompat.Builder notificationBuilder =
|
NotificationCompat.Builder notificationBuilder =
|
||||||
|
@ -90,4 +124,24 @@ public class LbrynetMessagingService extends FirebaseMessagingService {
|
||||||
|
|
||||||
notificationManager.notify(9898, notificationBuilder.build());
|
notificationManager.notify(9898, notificationBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getEnabledTypes() {
|
||||||
|
SharedPreferences sp = getSharedPreferences(MainActivity.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||||
|
List<String> enabledTypes = new ArrayList<String>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -534,6 +534,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
|
||||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||||
notificationManager.cancel(sourceNotificationId);
|
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);
|
super.onNewIntent(intent);
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class FirebaseModule extends ReactContextBaseJavaModule {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(Task<InstanceIdResult> task) {
|
public void onComplete(Task<InstanceIdResult> task) {
|
||||||
if (!task.isSuccessful()) {
|
if (!task.isSuccessful()) {
|
||||||
promise.reject("getInstanceId failed");
|
promise.reject("Firebase getInstanceId call failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 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 Context context;
|
||||||
|
|
||||||
private KeyStore keyStore;
|
private KeyStore keyStore;
|
||||||
|
@ -377,4 +386,14 @@ public class UtilityModule extends ReactContextBaseJavaModule {
|
||||||
Intent.createChooser(intent, "Select a file"), MainActivity.DOCUMENT_PICKER_RESULT_CODE);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue