1

Below is the code snippet which is demo application listed in countly documentation for sending push notifications to user in android & we have community edition of countly , issue is users are not even getting registered in countly.

Demo provided by countly

After adding necessary credentials in above demo app none of those is actually registering my device in countly , is countly accurate to show live users ? or am i missing anything wrong in below code ?

public class MainActivity extends Activity {

    private BroadcastReceiver messageReceiver;

    /**
     * You should use try.count.ly instead of YOUR_SERVER for the line below if you are using Countly trial service
     */
    final String COUNTLY_SERVER_URL = "http://xxxx";
    final String COUNTLY_APP_KEY = "xxxx";
     final String COUNTLY_MESSAGING_PROJECT_ID = "xxxx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Context appC = getApplicationContext();
        Countly.onCreate(this);
        Countly.sharedInstance().setLoggingEnabled(true);
        Countly.sharedInstance().setPushIntentAddMetadata(true);
        Countly.sharedInstance().enableCrashReporting();
        Countly.sharedInstance().setViewTracking(true);
        Countly.sharedInstance().setAutoTrackingUseShortName(true);
        Countly.sharedInstance().setRequiresConsent(false);
        Countly.sharedInstance().setConsent(new String[]{Countly.CountlyFeatureNames.sessions}, true);
        Countly.sharedInstance().init(appC, COUNTLY_SERVER_URL, COUNTLY_APP_KEY)
                .initMessaging(this, MainActivity.class, COUNTLY_MESSAGING_PROJECT_ID, Countly.CountlyMessagingMode.PRODUCTION);

        Countly.sharedInstance().recordEvent("test", 1);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Countly.sharedInstance().recordEvent("test2", 1, 2);
            }
        }, 5000);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Countly.sharedInstance().recordEvent("test3");
            }
        }, 10000);
    }

    @Override
    public void onStart() {
        super.onStart();
        Countly.sharedInstance().onStart(this);
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Register for broadcast action if you need to be notified when Countly message received
        messageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Message message = intent.getParcelableExtra(CountlyMessaging.BROADCAST_RECEIVER_ACTION_MESSAGE);
                Log.i("CountlyActivity", "Got a message with data: " + message.getData());

                //Badge related things
                Bundle data = message.getData();
                String badgeString = data.getString("badge");
                try {
                    if (badgeString != null) {
                        int badgeCount = Integer.parseInt(badgeString);

                        boolean succeeded = ShortcutBadger.applyCount(getApplicationContext(), badgeCount);
                        if (!succeeded) {
                            Toast.makeText(getApplicationContext(), "Unable to put badge", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (NumberFormatException exception) {
                    Toast.makeText(getApplicationContext(), "Unable to parse given badge number", Toast.LENGTH_SHORT).show();
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(CountlyMessaging.getBroadcastAction(getApplicationContext()));
        registerReceiver(messageReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(messageReceiver);
    }
}
erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
KOTIOS
  • 11,177
  • 3
  • 39
  • 66

1 Answers1

1

Since you don't see your user on Countly dashboard, it's one of the following:

  • Wrong COUNTLY_APP_KEY;
  • Wrong COUNTLY_SERVER_URL;
  • Some connectivity issue - your device cannot access that server.

You've enabled logging by Countly.sharedInstance().setLoggingEnabled(true), so your Logcat should have some errors which would tell you which one of those is about your case.

Also you use sdk-messaging dependency instead of sdk-messaging-fcm. First one uses GCM, which is deprecated in favor of FCM, that is Firebase. We have a detailed guide for that.

Artem
  • 400
  • 2
  • 7