Skip to content

Commit

Permalink
implementing registerForPushNotifications({useFCM=false}) for Android…
Browse files Browse the repository at this point in the history
… to ignore FCM messages
  • Loading branch information
Shchvova committed Aug 16, 2021
1 parent c6076a5 commit 6a6568a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Binary file modified plugins/2020.3569/android/plugin.notifications.v2.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

package plugin.notifications.v2;

import android.content.Context;
import android.content.SharedPreferences;

import com.ansca.corona.CoronaEnvironment;
import com.ansca.corona.events.NotificationRegistrationTask;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
Expand All @@ -19,18 +23,28 @@

public class CoronaFirebaseMessagingService extends FirebaseMessagingService
{
public static String PREFERENCE_FILE = "fcm-notifications";
public static String SKIP_FCM = "skipFCM";

public CoronaFirebaseMessagingService() {
super();
}

private boolean ignoreFCM() {
SharedPreferences preferences = CoronaEnvironment.getApplicationContext().getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
return preferences.getBoolean(SKIP_FCM, false);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(ignoreFCM()) return;;
super.onMessageReceived(remoteMessage);
NotificationsV2Helper.processRemoteMessage(remoteMessage, getApplicationContext());
}

@Override
public void onNewToken( String deviceToken ) {
if(ignoreFCM()) return;;
super.onNewToken(deviceToken);
NotificationRegistrationTask registrationTask = new NotificationRegistrationTask(deviceToken);
for (com.ansca.corona.CoronaRuntime runtime : com.ansca.corona.CoronaRuntimeProvider.getAllCoronaRuntimes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.naef.jnlua.LuaType;
import com.naef.jnlua.NamedJavaFunction;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessaging;
Expand Down Expand Up @@ -208,24 +210,24 @@ public String getName() {
* <p>
* Warning! This method is not called on the main UI thread.
*
* @param luaState Reference to the Lua state.
* @param L Reference to the Lua state.
* Needed to retrieve the Lua function's parameters and to return values back to Lua.
* @return Returns the number of values to be returned by the Lua function.
*/
@Override
public int invoke(final LuaState luaState) {
functionSignature = "notifications.registerForPushNotifications( options )";

// check number of args
int nargs = luaState.getTop();
if (nargs > 1) {
logMsg(ERROR_MSG, "Expected 0 or 1 argument, got " + nargs);
return 0;
public int invoke(final LuaState L) {

if(L.isTable(1)) {
L.getField(1, "useFCM");
if(L.type(-1) == LuaType.BOOLEAN) {
SharedPreferences preferences = CoronaEnvironment.getApplicationContext().getSharedPreferences(CoronaFirebaseMessagingService.PREFERENCE_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(CoronaFirebaseMessagingService.SKIP_FCM, !L.toBoolean(-1));
editor.apply();
}
L.pop(1);
}

// NOP
// iOS feature only

return 0;
}
}
Expand Down

0 comments on commit 6a6568a

Please sign in to comment.