1

I spent a few hours researching this with no luck, so I'm asking here. I've already checked these questions to no avail:

Up until today (AFAIK — I haven't tested it in a while), Firebase Cloud Messaging was working just fine. I could send a notification with data from the Firebase Console, and using getIntent().getExtras() would allow me to access that data from the app.

Today, however, I sent a test notification, but tapping it did not perform the expected action. After some digging, I found out that getIntent().getExtras() was just returning null no matter what. This is the relevant code:

private void respondToNotificationClick() {
    if (getIntent().getExtras() != null) {
        Log.e("NOTIF", "NOTIF");
        //...
    }
}

(This method is called from both onCreate() and onResume() in the main activity.)

But that log is never printed, and if I try to log Intent.getExtras() outside of the if statement, I get an NPE.

I suspect it has something to do with Firebase 11 vs 10 or maybe that this app is targeting API 26, but I just can't figure out how to fix it, and Google's documentation isn't always the most helpful.

What's going on here? Is it a known issue? Is it because I'm using a beta API (even though it's supposed to have been finalized)?

KENdi
  • 7,576
  • 2
  • 16
  • 31
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • FWIW--Works for me using 11.0.1 on an API 24 device – Bob Snyder Jun 25 '17 at 20:23
  • @BobSnyder is it possible that I'm being a complete derp and I need Google Play Services? – TheWanderer Jun 25 '17 at 20:28
  • 1
    Yes, I was just thinking about that. Definitely needed. If you are using an emulator, there is not yet a released image that contains GPS for API 26. Updated for lower APIs were just released a few days ago. – Bob Snyder Jun 25 '17 at 20:34
  • @BobSnyder I'm a smart one.... I'll test out GPS when I can, but this question can probably be closed. – TheWanderer Jun 25 '17 at 20:35
  • [GoogleApiAvailability](https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability) is a handy collection of features to check for GPS. – Bob Snyder Jun 25 '17 at 20:39
  • I am a little confused though. Why is the emulator able to receive Firebase notifications? Is it just the data handling that GPS is needed for? – TheWanderer Jun 25 '17 at 20:40
  • Hmm. So I just tried on my API 25 device and it doesn't log that "NOTIF" string, either. – TheWanderer Jun 25 '17 at 20:51

2 Answers2

5

Not an answer, but I have too much to share for comments...

It sounds like you are running on an API 26 emulator. As I noted in a previous comment, an API 26 emulator image has not yet been released that is compatible with Firebase 11.0.1. When I run on an API 26 emulator, the version incompatiblity is confirmed by this log message:

W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 10930470

Despite that, a notification sent from the Firebase console (with no data values) appears, and when touched invokes my launcher activity. In the onCreate() method of that activity I have this code:

    Intent intent = getIntent();
    if (intent != null) {
        Bundle b = intent.getExtras();
        if (b != null) {
            Set<String> keys = b.keySet();
            for (String key : keys) {
                Log.d(TAG, "Bundle Contains: key=" + key);
            }
        } else {
            Log.w(TAG, "onCreate: BUNDLE is null");
        }
    } else {
        Log.w(TAG, "onCreate: INTENT is null");
    }

which produces this output:

 D/MainActivity: Bundle Contains: key=google.sent_time
 D/MainActivity: Bundle Contains: key=from
 D/MainActivity: Bundle Contains: key=google.message_id
 D/MainActivity: Bundle Contains: key=collapse_key

So my previous comments about Google Play services being the problem were mistaken. The behavior you are observing is caused by something else.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • 1
    Hmm. It also fails on a physical API 25 tablet. I guess I'll just have to wait. This is enough of an answer for me. – TheWanderer Jun 25 '17 at 21:40
0

If you have a SplashScreen as the launcher activity declared in you manifest file the notification payload will be delivered there.

Pass the intent data from that activity to the next activity where you need to use it.

public class SplashScreenActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent intent = new Intent(this, MainActivity.class);  
        if (getIntent().getExtras() != null) {
           intent.putExtras(getIntent().getExtras());
        }
        startActivity(intent);
       finish();
    }
}

You may check your launcher Activity from your AndroidManifest.xml file:

<activity android:name=".SplashScreenActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Josef Grunig
  • 442
  • 4
  • 12