当前位置: 首页 > 知识库问答 >
问题:

从未调用FireBaseMessagingService的onMessageReceived

丁俊智
2023-03-14

我想在我的android设备收到推送通知时执行一些代码。我有以下文件:mainactivity.javaMyFireBaseMessagingService.java
mainactivity中,我启动消息服务:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService(new Intent(this, MyFirebaseMessagingService.class));
}

并且在MyFireBaseMessagingService中,我重写了OnMessageReceived方法,如下所示:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getName();

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "app-token: " + token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "on-message-received");
        String title = remoteMessage.getData().get("title");
        sendSMS("+491792654018", "sent from background");
    }

此外,在我的AndroidManifest中,我在 中有以下设置:

<service
    android:name=".MyFirebaseMessagingService"
    android:stopWithTask="false"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name=".MyFirebaseMessagingService" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

然后我触发通知,它出现在我的设备上(它震动,我看到通知的标题):

curl https://fcm.googleapis.com/fcm/send \                  
  -H "Authorization:key=<my-key>" \
  -H "Content-Type:application/json" \
  -d '{
    "to" : "<device token>",
    "notification": {
      "title": "hello from the server"
    },
    "priority": "high"
  }'

但是,我在OnMessageReceived中的代码从来没有执行过。
我注意到OnNewToken中的日志也没有被调用,所以我假设整个类都被完全忽略了。

我做了很多研究,有很多类似的问题,但我发现的所有修复,如关于清单,或分级配置没有帮助我。

我做错了什么?
[免责声明:我是Android开发的绝对初学者。]

共有1个答案

颜嘉福
2023-03-14

如果查看文档,您将看到OnMessageReceived仅当您处于前台时,而您的有效负载中没有Data时才调用,您在有效负载中仅使用了Notification

至于启动不启动的服务,Google Play Services会处理通知的发送,这是因为有意向过滤器 。阅读设置文档

 类似资料:
  • Firebase服务器发送通知,我收到了,但我的MyFirebaseMessagingService的onMessageReceived没有被调用。 这是MyFirebaseMessagingService: 我正在使用这个版本的firebase: 实现“com.google.firebase:firebase消息传递:12.0.1” 我在我的AndroidManifest中声明了我的Fireba

  • 我尝试使用Firebase为Android提供推送通知。但我面临着非常奇怪的问题。当我在前台发送推送时,一切正常。当我在后台发送推送时(我只发送数据),一切都正常,直到我回到前台,然后回到后台。 当我的应用程序第二次转到后台时,不会调用我的FirebaseMessagingService。另外,请注意,我使用的是Android Emulator。代码: AndroidManifest。xml: 服

  • 我正在将FireBase推送通知集成到我的应用程序中。虽然我收到推送通知(当应用程序在后台时),但我的onMessageRec的(远程消息远程消息)从来没有被调用,无论应用程序是在前台还是后台。如果这是FCM的问题,或者我做错了什么。我在我的应用程序中使用com.google.firebase: Firebase消息传递: 9.0.0build.gradle.我已经在我的应用程序清单中声明了这两个

  • 我已经厌倦了发现这个问题,但无法理解为什么没有调用Location Manager的委派方法或。我在我的iPhone上测试了这个,但它不起作用。请告诉我我错过了什么。我的源代码是: 有人能找出问题出在哪里吗??我已经搜索了很多演示和源代码,但大多数都以结尾,在我的例子中调用了它,但是我对和有问题。我还实现了,但它没有做到这一点。请给出不调用这两个方法的任何线索。或者给我任何调用这些方法的链接,我没

  • 问题内容: 我有以下代码: 从不调用completionHandler函数。我尝试在浏览器中调用该URL,但工作正常。我尝试使用另一个URL,但仍然无法正常工作。我检查了一下我的ios模拟器是否可以连接到Internet,确实可以。 我不知道为什么不调用该函数,并且由于我没有任何错误,因此很难调试。 问题答案: 该任务永远不会完成,因为它永远不会开始。您必须使用其方法手动启动数据任务。