【发布时间】: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