Cuando el app está abierta o detrás de escena, todo bien, llegan las notificaciones, pero cuando el app está cerrada, llega solo en algunos celulares.
Por ejemplo, en un Sony llega muy bien cuando el app está cerrada, pero en un Huawei Y6 no llega si el app está cerrada.
Código:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// SharedPreferences.getInstance(getApplicationContext()).setString(SPKeys.Keys.API_TOKEN,refreshedToken);
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void handleIntent(Intent intent) {
super.handleIntent(intent);
try {
if (intent.getExtras() != null) {
Log.i(TAG, "handleIntent: " + intent.getExtras().toString());
if (!isAppIsInBackground(getApplicationContext())) {
String title = intent.getExtras().get("gcm.notification.title").toString();
String body = intent.getExtras().get("gcm.notification.body").toString();
String url = intent.getExtras().get("URL").toString();
sendNotification(title, body, url);
} else {
Utility.PUSH_NOTIFICATIONMESSAGE = intent.getExtras().get("URL").toString();
Log.d(TAG, "Refreshed token: en handle intent " + Utility.PUSH_NOTIFICATIONMESSAGE);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Refreshed token: en catch " + Utility.PUSH_NOTIFICATIONMESSAGE);
}
}
private void sendNotification(String messageBody, String tittle, String urlIMG) {
PendingIntent pendingIntent = null;
if (urlIMG == null && urlIMG.equalsIgnoreCase("")) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
} else {
Uri uri = Uri.parse(urlIMG);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
/*notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher));*/
notificationBuilder.setSmallIcon(R.drawable.ic_notifications_black_24dp);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher));
notificationBuilder.setContentTitle(tittle);
notificationBuilder.setContentText(messageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
}
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
}
y en mi Manifest:
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@android:color/transparent" />
<!-- Firebase Notifications -->
<service android:name=".notification.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".notification.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>