gpt4 book ai didi

android - MyFirebaseMessagingService.onMessageReceived() 不起作用

转载 作者:行者123 更新时间:2023-11-30 04:57:13 31 4
gpt4 key购买 nike

我正在尝试“捕获”发送到应用程序的 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:问题是由 google 的 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 或与之相关的一些库以某种方式引起的

编辑 5:所以我找到了一种解决方法 - 如果您想同时继续使用 TWA 和 FCM,您将不得不只使用数据消息,通知消息似乎不会触发 onMessageReceived即使应用程序在前台

最佳答案

From your question, I can guess you are receiving Data Message

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
String title = "", body = "", type = "";
Intent hIntent = new Intent(getApplicationContext(), HomePageActivity.class);
try {
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
CommonUtils.logThis(TAG, "key, " + key + " value " + value);
switch (key) {
case "title":
title = entry.getValue();
break;
case "body":
body = entry.getValue();
break;
case "type":
type = entry.getValue().toLowerCase();
break;
}
}
CommonUtils.logThis("jeev", "Type is : " + type);
switch (type) {
case "your_notification_type":
//write your code here
break;
}
} catch (Exception e) {
CommonUtils.logThis(TAG, "Exception: " + e.getMessage());
}
}
}

关于android - MyFirebaseMessagingService.onMessageReceived() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938234/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com