我正在使用Firebase Cloud Messaging发送推送通知 .
这是我的 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>
所以问题是当App正在运行时 ticker
显示良好并且通知带有默认声音,但是当App处于前台或未运行时,通知没有任何声音, ticker
未显示在状态栏中 .
为什么会发生这种情况,我该如何解决?
2 回答
使用FCM,您可以指定要发送到
https://fcm.googleapis.com/fcm/send
的POST有效负载 . 在该有效负载中,您可以指定data
或notification
键,或两者都指定 .如果您的有效负载仅包含
data
密钥,则您的应用程序将自行处理所有推送消息 . 例如 . 它们都被发送到你的onMessageReceived
处理程序 .如果您的有效负载包含
notification
密钥,那么如果您的应用处于活动状态/前台,您的应用将自动处理推送消息 only . 如果不是(因此它在后台,或完全关闭),FCM会通过使用您放入notification
密钥有效负载的值来处理显示通知 .请注意,从控制台(如Firebase控制台)发送的通知,它们始终包含
notification
键 .看起来您希望自己处理FCM消息,以便您可以更多地自定义通知等,因此最好不要在POST有效负载中包含
notification
密钥,因此所有推送消息都会传递到您的onMessageReceived
.你可以在这里读更多关于它的内容:
Advanced messaging options
Downstream message syntax
我花了两个星期的时间来理解为什么我的应用程序在后台无法接收数据消息,并且尽可能奇怪,我得出的结论是Android-Studio 2.1.2就是问题所在!
I cannot receive any FCM message anymore in background application
https://github.com/firebase/quickstart-android/issues/89