Проблема следующая, при закрытом(не свернутом) приложении Push сообщения не приходят. Использую FireBase. Пока приложение открыто или в фоне, все работает.
public class MessagingFirebase extends FirebaseMessagingService {
private static final String TAG = "message";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Intent notificationIntent = new Intent();
android.support.v4.app.NotificationCompat.Builder nb = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) //иконка уведомления
.setAutoCancel(true) //уведомление закроется по клику на него
.setTicker(remoteMessage.getData().get("message")) //текст, который отобразится вверху статус-бара при создании уведомления
.setContentText(remoteMessage.getData().get("message")) // Основной текст уведомления
.setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis()) //отображаемое время уведомления
.setContentTitle("AppName") //заголовок уведомления
.setDefaults(Notification.DEFAULT_ALL); // звук, вибро и диодный индикатор выставляются по умолчанию
Notification notification = nb.getNotification(); //генерируем уведомление
NotificationManager manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
manager.notify(100, notification); // отображаем его пользователю.
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
}
public class getTokenFirebase extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("token", "Refreshed token: " + refreshedToken);
//startForeground(100000,viewNotification());
}
}
AndroidManifest
<service
android:name=".Service.getTokenFirebase">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".Service.MessagingFirebase">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>