【问题标题】:How can I send messages in background of an android app?如何在 Android 应用程序的后台发送消息?
【发布时间】:2018-09-07 14:51:12
【问题描述】:

我想创建一个可以向手机发送 Firebase 推送消息的应用。这现在有效。但是当我关闭应用程序时,我也希望获得与应用程序中相同的推送消息。

我搜索了很多,但找不到解决方案。我找到了 IntentService,但我不确定如何将它与推送通知一起使用。

更新: 我使用了firebase教程。我的服务等级:

package com.example.kai.msgtest;

import android.app.NotificationManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage remoteMessage) {
        Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_mail_white_18dp)
                .setContentTitle("Sie haben Post")
                .setSound(sound)
                .setContentText(remoteMessage.getData().get("message"));
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

【问题讨论】:

  • 你的一些代码对人们评估情况很有用。
  • 这可能是一个简单的解决方法,但如果没有看到您的代码(处理已发送消息的部分)和您发送的消息示例,我们真的无能为力。
  • 你必须学习functions。一旦你的概念清晰,那么即使应用程序关闭/后台,你也可以发送通知。
  • 您的服务类看起来正确。您是否已将其添加到清单中?
  • 是的,我做到了。应用程序打开时的通知效果很好。当我关闭应用程序时,会出现通知,但没有任何声音,并且系统托盘中有默认/后备图标。我阅读了一些关于这种行为的信息,但我不明白如何处理这个问题,以便我可以像在前台一样获得相同的通知

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

我使用 fcm api 解决了这个问题。我配置了一些东西,比如 fcm 消息中显示的图标,如下所示:

curl -X POST --header "Authorization: key=<serverkey>" \
--Header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
-d "{\"to\":\"<device-token>\",\"notification\":{\"body\":\"test\", \"icon\":\"ic_mail_white_18dp\"},\"priority\":10}"

之后,图标会出现在应用处于前台时的样子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多