1

下面是代码片段,它是 countly 文档中列出的演示应用程序,用于在 android 中向用户发送推送通知,我们有 countly 社区版,问题是用户甚至没有在 countly 中注册。

Demo由countly提供

在上面的演示应用程序中添加必要的凭据后,这些凭据都没有真正在计数中注册我的设备,计数是否准确显示实时用户?还是我在下面的代码中遗漏了什么?

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);
    }
}
4

1 回答 1

1

由于您在 Countly 仪表板上看不到您的用户,因此它是以下之一:

  • 错了COUNTLY_APP_KEY
  • 错了COUNTLY_SERVER_URL
  • 一些连接问题 - 您的设备无法访问该服务器。

您已启用日志记录Countly.sharedInstance().setLoggingEnabled(true),因此您的 Logcat 应该有一些错误,这些错误会告诉您其中哪一个与您的案例有关。

您还使用sdk-messaging依赖项而不是sdk-messaging-fcm. 第一个使用 GCM,它已被 FCM 弃用,即 Firebase。我们对此有详细的指南

于 2019-01-07T15:42:55.950 回答