getIntent().getExtras() returns null FCM



我花了几个小时就没有运气研究,所以我在这里问。我已经检查了这些问题,无济于事:

  • 如果应用在后台,如何使用FCM从服务器发送数据?
  • 推送通知不使用FCM
  • Google FCM GetIntent在应用程序处于后台状态时不会返回预期数据

直到今天(AFAIK - 一段时间我还没有测试过(,Firebase Cloud Mess传递工作正常。我可以从Firebase控制台中发送带有数据的通知,并且使用getIntent().getExtras()可以允许我从应用程序访问该数据。

今天,我发送了一份测试通知,但是敲击它没有执行预期的动作。经过一番挖掘,我发现getIntent().getExtras()无论如何都返回null。这是相关代码:

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

(此方法在主要活动中从onCreate()onResume()调用。(

但是,该日志从未打印过,如果我尝试在if语句之外登录Intent.getExtras(),我会得到NPE。

我怀疑这与firebase 11 vs 10有关,或者这个应用程序针对API 26,但我只是不知道如何修复它,而Google的文档并不总是最有用的。<<<<<。/p>

这里发生了什么?这是一个已知问题吗?是因为我使用的是beta API(即使应该是最终确定的(?

不是答案,但是我有太多要分享的评论...

听起来您在API 26模拟器上运行。正如我在先前的评论中指出的那样,尚未发布与Firebase 11.0.1兼容的API 26仿真器图像。当我在API 26模拟器上运行时,该日志消息确认了IncompatiBlity:

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

尽管如此,出现了从firebase控制台发送的通知(没有数据值(,当触摸时会调用我的启动器活动。在该活动的onCreate()方法中,我有此代码:

    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");
    }

产生此输出的

 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

所以我以前关于Google Play服务的评论是错误的。您观察到的行为是由其他事物引起的。

如果您有溅起屏幕,因为在您清单文件中声明的启动器活动,通知有效负载将在此处传递。

将意图数据从该活动传递到您需要使用它的下一个活动。

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();
    }
}

您可以从androidmanifest.xml文件中检查启动器活动:

<activity android:name=".SplashScreenActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

最新更新