当应用程序处于后台时,我遇到了一个非常奇怪的问题,即第二次没有收到有效负载数据。
例如 - (当我还没有启动我的应用程序时)
- 当我第一次收到通知时,它会打开我的应用程序并阅读通知中的附加信息。
- 当我收到第二条通知时,它会最大化我的应用程序或将我的应用程序置于最前面,但不会阅读额外内容。
当我的应用程序打开时,一切正常。即使我在手动打开我的应用程序后将其置于后台,所有通知也能正常工作。
这是我的 list 代码
<activity
android:name=".PagerActivity"
android:launchMode="standard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是我的 Activity 代码
@Override
public void onNewIntent(Intent newIntent) {
this.setIntent(newIntent);
// Now getIntent() returns the updated Intent
if(getIntent().hasExtra("sno")){
Log.d("New Intent DATA", "Yes it has the data sno is: " + getIntent().getExtras().getString("sno"));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent().hasExtra("sno")){
Log.d("DATA", "Yes it has the data");
}
}
我尝试了很多研究,但无济于事。如果您需要任何其他详细信息,请告诉我。
我正在使用 firebase 发送带有自定义数据的通知。我也试过它通过网络服务器发送,但仍然没有成功。
我调试了我的应用程序,发现当我通过第二个通知打开我的应用程序时,没有创建新实例。
最佳答案
当您同时发送notification
和data payload
时,如果您的应用程序处于后台,通知将显示在托盘中,但如果您的启动 Activity 点击通知在后台,它将被带到前台,您将不会收到数据有效负载。
一种解决方案是将该特定 Activity 的启动模式
设置为singleTask并在 Activity 中实现onNewIntent
以接收数据负载
。
这样,您的 Activity 将被带到前台,位于堆栈中此 Activity 顶部的 Activity 将被销毁。
您可以找到有关启动模式的更多信息 here .
关于当应用程序在后台时,Android 第二次推送通知有效负载数据未接收到额外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40650807/