Android 当应用程序处于后台或未运行时,推送通知无法正常工作
我正在使用Firebase云消息发送推送通知 这是我的Android 当应用程序处于后台或未运行时,推送通知无法正常工作,android,service,push-notification,android-notifications,firebase-cloud-messaging,Android,Service,Push Notification,Android Notifications,Firebase Cloud Messaging,我正在使用Firebase云消息发送推送通知 这是我的FirebaseMessageService: public class FireBaseMessageService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.e("TAG", "From: " + remoteMessage.getFrom());
FirebaseMessageService
:
public class FireBaseMessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("TAG", "From: " + remoteMessage.getFrom());
Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+" : "+remoteMessage.getData().get("CardCode"));
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, StartActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_final)
.setContentTitle("Notification")
.setContentText(messageBody)
.setTicker("Test")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
和FirebaseInstanceServer
:
public class FirebaseInstanceService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("TAG", "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
Log.e("TAG", "Refreshed token2: " + token);
}
}
在AndroidManifest
中声明:
<service
android:name=".util.notifications.FireBaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".util.notifications.FirebaseInstanceService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
所以问题是,当应用程序运行时,ticker
显示良好,通知带有默认声音,但当应用程序处于后台或未运行时,通知没有任何声音,且状态栏中不显示ticker
。为什么会发生这种情况?我如何修复它?使用FCM,您可以指定要发送到
https://fcm.googleapis.com/fcm/send
。在该有效负载中,您可以指定数据
或通知
键,或同时指定两者
如果您的有效负载仅包含一个数据
键,则您的应用程序将自行处理所有推送消息。例如,它们都会传递给您的onMessageReceived
处理程序
如果您的有效负载包含一个通知
键,则您的应用程序将只处理推送消息本身如果您的应用程序处于活动状态/在前台。如果不是(因此它在后台,或完全关闭),FCM将使用您输入到通知
密钥有效负载中的值为您处理显示通知
请注意,从控制台(如Firebase控制台)发送的通知始终包含通知
键
看起来您希望自己处理FCM消息,以便可以自定义更多的通知等,因此最好不要在POST有效负载中包含通知
键,以便所有推送消息都传递到您的onMessageReceived
您可以在此处阅读更多信息:使用FCM,您可以指定要发送到
https://fcm.googleapis.com/fcm/send
。在该有效负载中,您可以指定数据
或通知
键,或同时指定两者
如果您的有效负载仅包含一个数据
键,则您的应用程序将自行处理所有推送消息。例如,它们都会传递给您的onMessageReceived
处理程序
如果您的有效负载包含一个通知
键,则您的应用程序将只处理推送消息本身如果您的应用程序处于活动状态/在前台。如果不是(因此它在后台,或完全关闭),FCM将使用您输入到通知
密钥有效负载中的值为您处理显示通知
请注意,从控制台(如Firebase控制台)发送的通知始终包含通知
键
看起来您希望自己处理FCM消息,以便可以自定义更多的通知等,因此最好不要在POST有效负载中包含通知
键,以便所有推送消息都传递到您的onMessageReceived
您可以在此处阅读更多信息:我花了两周的时间来理解为什么我的应用程序在后台无法接收数据消息,尽管它可能很奇怪,但我得出结论,安卓Studio 2.1.2就是问题所在
我花了两周的时间来理解为什么我的应用程序在后台无法接收数据消息,尽管它可能很奇怪,但我得出结论,安卓Studio 2.1.2就是问题所在 从后端的通知消息json响应中删除
通知
键
从通知消息中删除
通知
键来自后端的json响应谢谢,你说得对,当我删除通知
键时,我开始正确接收通知TIM是正确的,我想补充一点,从Firebase控制台发送的消息是通知消息,因此如果您确实使用Firebase控制台,您必须相应地处理它们。@tim castelijns您确定数据消息仍在后台接收吗。直到最近我才开始这么做。我有一个在FCM 9.2.0下运行的应用程序,但在上一次studio、sdk、java更新之后,我不知道,它不再工作了,尽管我没有对应用程序代码做任何更改!?!是的,我肯定。这就是它们在以后的用途all@miroslavign我不知道。谢谢分享:)谢谢,你说得对,当我删除通知
键时,我开始正确接收通知。TIM是正确的,我想补充一点,从Firebase控制台发送的消息是通知消息,因此如果您确实使用Firebase控制台,您必须相应地处理它们。@tim castelijns您确定数据消息仍在后台接收吗。直到最近我才开始这么做。我有一个在FCM 9.2.0下运行的应用程序,但在上一次studio、sdk、java更新之后,我不知道,它不再工作了,尽管我没有对应用程序代码做任何更改!?!是的,我肯定。这就是它们在以后的用途all@miroslavign我不知道。谢谢分享:)这把钥匙在哪里?用户\设备\令牌?var message=new message(){Data=Data,Token=Token,Notification=new Notification(){Body=notificationBody,Title=Title};这把钥匙在哪里?用户\设备\令牌?var message=new message(){Data=Data,Token=Token,Notification=new Notification(){Body=notificationBody,Title=Title};
{
"to": "user_device_token",
"collapse_key": "type_a",
"data": {
"body": "your message body",
"title": "notification title",
"description":"notification description ",
"imageUrl":"image url",
"key_1": "Value for key_1",
"key_2": "Value for key_2"
}
}