我正在使用Firebase并测试在应用程序处于后台时从服务器发送通知到我的应用程序。通知发送成功,它甚至出现在设备的通知中心,但当通知出现或即使我点击它,我的FCMessagingService中的onmessagerreceived方法永远不会被调用。
当我测试这个,而我的应用程序是在前台,onmessagerreceived方法被调用,一切工作正常。问题发生在应用程序在后台运行时。
这是我有意为之的行为吗,或者我有办法解决这个问题吗?
这是我的FBMessagingService:
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FBMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i("PVL", "MESSAGE RECEIVED!!");
if (remoteMessage.getNotification().getBody() != null) {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
} else {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
}
}
}
我正在使用的后端使用通知消息,而不是数据消息。因此,在阅读了所有的答案后,我试图从启动的活动的意图包中检索额外的内容。
但无论我试图从getIntent(). getextras();中检索哪个键,值始终为空。
然而,我最终找到了一种方法,使用通知消息发送数据,并从意图中检索它。
这里的关键是向Notification消息添加数据有效负载。
例子:
{
"data": {
"message": "message_body",
"title": "message_title"
},
"notification": {
"body": "test body",
"title": "test title"
},
"to": "E4An.."
}
在你这样做之后,你将能够以这种方式获得你的信息:
intent.getExtras () .getString(“标题”)
message_title
而且
intent.getExtras () .getString(“信息”)
message_body
参考
我也有同样的问题。使用“数据消息”而不是“通知”更容易。数据消息总是加载类onmessagerreceived。
在该类中,您可以使用notificationbuilder创建自己的通知。
例子:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我也有类似的问题。根据本页中提到的答案和参考资料,以下是我如何用以下方法解决问题的两点意见:
我之前的消息格式如下:
{
"notification": {
"title": "AppName",
"sound": null,
"body": "Hey!YouhaveaMessage"
},
"data": {
"param1": null,
"param2": [
238
],
"id": 1
},
"to": "--the device push token here--"
}
我修改了消息格式如下:
{
"data": {
"title": "AppName",
"body": "Hey! You have a message",
"param1": null,
"param2": [
238
],
"id": 1
},
"priority": "high",
"to": " — device push token here — "
}
然后我从“data”负载本身检索标题、主体和所有参数。这解决了问题,然后我可以得到onmessagerreceived回调,即使应用程序是在后台。
我写了一篇博客文章解释了同样的问题,你可以在这里找到。
我现在回答这个问题可能有点晚了,但是官方文件有点混乱。
它还明确指出,有两种类型的通知
通知消息:由FCM自动处理
数据消息:由客户端应用程序处理。
毫无疑问,如果服务器发送了一个数据消息,那么onmessagerecreceived()方法肯定会被调用,但在通知消息的情况下,onmessagerecreceived()方法只会在应用程序在前台被调用,当应用程序在后台时,我们发送的数据只是null。
例子:
假设服务器正在发送通知消息类型:
A.对于前景:
remoteMessage。Data ["key"]可以工作
B.背景情况:
-remoteMessage。Data ["key"]将返回null
但在这里,如果你在默认活动中发现相同的意图数据,getIntent().getExtras().getString("key")将工作
C.如发生灭杀:
-remoteMessage。Data ["key"]将返回null
但在这里,如果你在默认活动中发现相同的意图数据,getIntent().getExtras().getString("key")将工作
现在,让我们假设服务器正在发送数据消息类型:
D.前景情况:
remoteMessage。Data ["key"]可以工作
E.背景情况:
remoteMessage。Data ["key"]可以工作
F.如果杀死:
remoteMessage。Data ["key"]可以工作
毫无疑问,数据消息将始终调用onmessagerreceived()方法,但在通知消息和应用程序处于后台/kill状态的情况下,您可以使用b的解决方案
我希望这会节省大家的时间。