我正在使用Firebase雲消息傳遞發送推送通知。當應用程序處於後臺或未運行時,推送通知無法正常工作
這是我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
是表現良好,通知com e默認聲音,但是當App處於前臺或未運行通知時,沒有任何聲音,並且ticker
未顯示在狀態欄中。
爲什麼會發生這種情況,我該如何解決?
謝謝,你是對的,如果我刪除了'notification'關鍵,我開始收到通知的正確 – Ololoking
蒂姆是正確的,我只是想補充一點,從火力地堡控制檯發送的消息是通知消息,因此如果您使用Firebase控制檯,則必須相應地處理它們。 –
感謝它幫助我很多。 –