我正在尝试“捕获”发送到应用程序的 firebase 云消息,当我收到(/单击)一个消息时,我想根据在 firebase 控制台中添加的“自定义数据”键/值执行某些操作。
我的代码现在看起来像这样:
MyFirebaseMessagingService:
package com.company.app;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "Notification";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.i(TAG, "Receiving notification...");
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d(TAG, "key, " + key + " value " + value);
}
}
}
AndroidManifest.xml:
...
<!-- Firebase notification -->
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
build.gradle(模块:应用程序):
dependencies {
//Google firebase
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.0.1'
...
我的猜测是<service android:name=".MyFirebaseMessagingService" android:exported="false">
没有被执行,但我不知道为什么。我没有收到任何错误。除此之外,我的通知工作没有任何问题。
如果有任何帮助,这里是控制台日志:
D/FA: Logging event (FE): notification_receive(_nr),
Bundle[{
ga_event_origin(_o)=fcm,
message_device_time(_ndt)=0,
message_type(_nmc)=display,
message_name(_nmn)=B1 - TN18,
message_time(_nmt)=1574177464,
message_id(_nmid)=8950284200210511319}]
有任何想法吗?
编辑:相同的代码适用于其他空的应用程序
编辑 2:问题是由谷歌的 TWA LauncherActivity 引起的
编辑3:
public class LauncherActivityTWA extends AppCompatActivity {
private TwaLauncher mTwaLauncher;
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
this.mTwaLauncher.launch(getLaunchingUrl()); //this is causing the issue
}
编辑 4:现在我已经通过切换到 WebView 解决了这个问题,稍后我将尝试让它与 TWA 一起工作。我仍然认为这个问题是由 TWA 或与之相关的一些库引起的
onMessageReceived
编辑 5:所以我找到了一种解决方法 - 如果您想同时继续使用 TWA 和 FCM,您将不得不只使用数据消息,即使应用程序在前台,通知消息似乎也不会触发