0

I have an issue and I think I may not what it is causing it. In my activity data is posted to the server on button click.

The server then processes this request and initiates a network PROMPT on your phone using your number, like the prompt in a USSD.

Now the thing is the minute the prompt appears the user is required to choose an option and enter their pin code to finish the request. On the server once this request has been verified I post to the app using FCM. Now when the messaging service gets this request I send a Broadcast to the current activity.

The broadcast is received in my activity which notifies the app to take the user to the final activity.

Now this works perfectly on my emulator but on my phone it doesn't. I checked my log and noticed that the receiver does not get called on phone and I do not know why. I have a feeling it had something to do with the app losing focus or something when the prompt appears. I'm not too sure.

Edit:

MyFireBaseMessagingService.java:

 public void onMessageReceived(RemoteMessage remoteMessage) {
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    Map<String, String> data = remoteMessage.getData();
    if (data.containsKey("method") && data.containsKey("action")) {
        String action = data.get("action");
        switch (action) {
            case "/checkout":
                Log.d("onMessageReceived", data.toString());
                Intent i = new Intent();
                i.setAction("com.xxxxxx.xxxxxx.CHECKOUT");
                i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                i.putExtra("data", new JSONObject(data).toString());
                sendBroadcast(i);
                break;
            default:
                Log.d("Default", action);
                break;
        }
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

MyBroadcastReceiver.java:

private class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        showProgress(false);
        Intent checkoutIntent = new Intent(PaymentActivity.this, CheckoutActivity.class);
        startActivity(checkoutIntent);
    }
}

MyActivity.java:

@Override
public void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter("com.xxxxxx.xxxxxx.CHECKOUT");
    filter.setPriority(999);
    mBroadcastReceiver = new MyBroadcastReceiver();
    mContext.registerReceiver(mBroadcastReceiver, filter);
}

@Override
public void onPause() {
    super.onPause();
    mContext.unregisterReceiver(mBroadcastReceiver);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
user3718908x100
  • 7,939
  • 15
  • 64
  • 123

3 Answers3

0

Here's what you can try. Remove the broadcast registerReceiver from onResume and unregisterReceiver from onPause().

Then, add a new class:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent checkoutIntent = new Intent(context, CheckoutActivity.class);
        context.startActivity(checkoutIntent);
    }
}

Add this to your manifest (and make sure to change the action):

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="com.xxxxxx.xxxxxx.CHECKOUT" />
    </intent-filter>
</receiver>

And try now.
This way, even if your activity's onPause() is called, onReceive() on BroadcastReceiver will still be called.

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
0

If "Now this works perfectly on my emulator but on my phone it doesn't.". I think you need check android permission.

Dungnbhut
  • 176
  • 5
0

It's likely not the receiving that is the problem but rather sending. (sending with in the same app?) You can test the receiving on a real device using.

➜  ~ adb shell am broadcast -a com.xxxxxx.xxxxxx.CHECKOUT
Broadcasting: Intent { act=com.xxxxxx.xxxxxx.CHECKOUT flg=0x400000 }
Broadcast completed: result=0