在count.ly上推送通知

时间:2019-01-01 07:53:11

标签: android countly-analytics

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

Demo provided by 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);
    }
}

1 个答案:

答案 0 :(得分:0)

由于您没有在Countly仪表板上看到您的用户,因此属于以下其中一种:

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

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

也可以使用sdk-messaging依赖性而不是sdk-messaging-fcm。第一个使用GCM,而FCM已弃用GCM,即Firebase。我们有a detailed guide for that