2016-02-12 11:39:27 +01:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2018-11-17 12:57:36 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2018-05-20 12:01:07 +02:00
|
|
|
import android.util.Log;
|
2016-02-12 11:39:27 +01:00
|
|
|
|
2018-05-19 20:05:45 +02:00
|
|
|
import com.google.firebase.messaging.FirebaseMessagingService;
|
|
|
|
import com.google.firebase.messaging.RemoteMessage;
|
2016-02-12 11:39:27 +01:00
|
|
|
|
2018-05-19 20:05:45 +02:00
|
|
|
import java.util.Map;
|
2016-02-12 11:39:27 +01:00
|
|
|
|
2018-05-20 12:01:07 +02:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-11-17 12:57:36 +01:00
|
|
|
import eu.siacs.conversations.utils.Compatibility;
|
2018-05-20 12:01:07 +02:00
|
|
|
|
2018-05-19 20:05:45 +02:00
|
|
|
public class PushMessageReceiver extends FirebaseMessagingService {
|
2016-02-12 11:39:27 +01:00
|
|
|
|
|
|
|
@Override
|
2018-05-19 20:05:45 +02:00
|
|
|
public void onMessageReceived(RemoteMessage message) {
|
2018-05-20 12:01:07 +02:00
|
|
|
if (!EventReceiver.hasEnabledAccounts(this)) {
|
|
|
|
Log.d(Config.LOGTAG,"PushMessageReceiver ignored message because no accounts are enabled");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-17 12:57:36 +01:00
|
|
|
final Map<String, String> data = message.getData();
|
|
|
|
final Intent intent = new Intent(this, XmppConnectionService.class);
|
2018-05-19 20:05:45 +02:00
|
|
|
intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
|
|
|
|
intent.putExtra("account", data.get("account"));
|
2018-11-22 10:06:56 +01:00
|
|
|
try {
|
|
|
|
if (Compatibility.runsAndTargetsTwentySix(this)) {
|
|
|
|
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
|
|
|
|
ContextCompat.startForegroundService(this, intent);
|
|
|
|
} else {
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
} catch (IllegalStateException e) {
|
2019-01-09 17:37:52 +01:00
|
|
|
Log.e(Config.LOGTAG,"PushMessageReceiver is not allowed to start service after receiving message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNewToken(String token) {
|
|
|
|
if (!EventReceiver.hasEnabledAccounts(this)) {
|
|
|
|
Log.d(Config.LOGTAG,"PushMessageReceiver ignored new token because no accounts are enabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Intent intent = new Intent(this, XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
|
|
|
|
try {
|
|
|
|
if (Compatibility.runsAndTargetsTwentySix(this)) {
|
|
|
|
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
|
|
|
|
ContextCompat.startForegroundService(this, intent);
|
|
|
|
} else {
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
Log.e(Config.LOGTAG,"PushMessageReceiver is not allowed to start service after receiving new token");
|
2018-11-17 12:57:36 +01:00
|
|
|
}
|
2016-02-12 11:39:27 +01:00
|
|
|
}
|
2018-05-19 20:05:45 +02:00
|
|
|
|
2016-02-12 11:39:27 +01:00
|
|
|
}
|