Backendless Push Notification stop being shown after some time

I just migrated my app from Parse to Backendless. Now nearly everything works as I want it to. There is just one issue left: A lot of push notifications are not shown.

I am still not sure about the pattern of this problem. But my feeling is, that after not using my app for like 1 or 2 days, push messages are not displayed anymore. Is there someone else, who experienced such a problem?

This is how I send messages via REST:

private void pushForAndroid(News newItem) {
    DeliveryOptions deliveryOptions = new DeliveryOptions();
    deliveryOptions.setPushBroadcast(PushBroadcastMask.ANDROID);
    deliveryOptions.setPushPolicy(PushPolicyEnum.ONLY);

    PublishOptions publishOptions = new PublishOptions();
    publishOptions.putHeader("android-content-title", newItem.getTitle());
    publishOptions.putHeader("android-content-source", newItem.getSource());
    publishOptions.putHeader("android-content-url", newItem.getLink());

    MessageStatus status = Backendless.Messaging.publish(newItem.getSource(), "", publishOptions, deliveryOptions);
    logger.debug("Push message status: " + status);
  }

This is my Android code:

public class AppPushService extends BackendlessPushService {

    NotificationCompat.Builder builder;
    
    Uri notifySound;
    int notificationId = 0;

    public final static String ANDROID_SOURCE_TEXT_TAG = "android-content-source";
    public final static String ANDROID_TITLE_TEXT_TAG = "android-content-title";
    public final static String ANDROID_URL_TEXT_TAG = "android-content-url";

    String contentSource = "";
    String contentTitle = "";
    String contentUrl = "";

    @Override
    public void onRegistered(Context context, String registrationId) {
        Log.i(getClass().getSimpleName(), "device registered");
    }

    @Override
    public void onUnregistered(Context context, Boolean unregistered) {
        Log.d(getClass().getSimpleName(), "device unregistered");
    }

    @Override
    public boolean onMessage(Context context, Intent intent) {
        Log.d(getClass().getSimpleName(), "Message received: " + intent.getExtras());
        createNotification(context, intent);
        // When returning 'true', default Backendless onMessage implementation will be executed.
        // The default implementation displays the notification in the Android Notification Center.
        // Returning false, cancels the execution of the default implementation.
        return false;
    }

    @Override
    public void onError(Context context, String message) {
        Log.e(getClass().getSimpleName(), "error: " + message);
    }

    private void createNotification(Context context, Intent intent) {
        if (!prefs.allowPush().get()) return;

        //Get JSON data and put them into variables
        try {
            contentSource = intent.getStringExtra(ANDROID_SOURCE_TEXT_TAG);
            contentTitle = intent.getStringExtra(ANDROID_TITLE_TEXT_TAG);
            contentUrl = intent.getStringExtra(ANDROID_URL_TEXT_TAG);
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Unexpected JSONException when receiving push data: ", e);
        }

        notifySound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.ic_push_statusbar);
        builder.setColor(context.getResources().getColor(R.color.main));
        builder.setLights(context.getResources().getColor(R.color.main), 1000, 3000);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
        builder.setContentTitle(contentSource + " meldet:");
        builder.setContentText(contentTitle);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(contentTitle));
        if (!TextUtils.isEmpty(prefs.ringtoneUri().get())) {
            notifySound = Uri.parse(prefs.ringtoneUri().get());
            builder.setSound(notifySound);
        }
        builder.setAutoCancel(true);

        final Intent resultIntent = new Intent(context, HomeActivity_.class);
        resultIntent.setAction(Long.toString(System.currentTimeMillis()));

        int dummyUniqueInt = new Random().nextInt(15432541);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, dummyUniqueInt, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, builder.build());
    }
}

As I said, I know this is working as most messages are shown on my device. But unfortunately not all…

Hello, Jens!

After a few days device looses registration (it’s controlled by GCM service, not by Backendless) so the best practice is to re-register device each time the app is started.
best regards,
Alex

Ah, great. That was the problem!