0

Morning all, Overview: Bit of a noob in the whole Android app dev area, but had some free time and thought I would give it a shot myself. Facing issue with error "Error:(35, 54) error: incompatible types: main cannot be converted to Application". Any and all help is appreciated.

Steps: 1) Setup Android Studio and created a basic project using the Boilerplates they provide. Added the MC libraries(etsdk-4.8.5) as per http://salesforce-marketingcloud.github.io/JB4A-SDK-Android/ - This Builds fine -

2)When adding the ETPush.configureSdk() part (see code below), I get the above error because the type expected by the SDK is Application, but it seems the type "this" refers to is "main".

Notes: Looking at the learningapp example the difference seems to be related to the boilerplate from Android Studio but could easily be wrong.

package com.example.douglasmidgley.basicjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//added MC stuff
import com.exacttarget.etpushsdk.ETAnalytics;
import com.exacttarget.etpushsdk.ETException;
import com.exacttarget.etpushsdk.ETLogListener;
import com.exacttarget.etpushsdk.ETNotificationBuilder;
import com.exacttarget.etpushsdk.ETNotifications;
import com.exacttarget.etpushsdk.ETPush;
import com.exacttarget.etpushsdk.ETPushConfig;
import com.exacttarget.etpushsdk.ETPushConfigureSdkListener;
import com.exacttarget.etpushsdk.ETRequestStatus;
import com.exacttarget.etpushsdk.data.Attribute;
import com.exacttarget.etpushsdk.data.Region;
import com.exacttarget.etpushsdk.event.BeaconResponseEvent;
import com.exacttarget.etpushsdk.event.GeofenceResponseEvent;
import com.exacttarget.etpushsdk.event.RegistrationEvent;
import com.exacttarget.etpushsdk.util.EventBus;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import android.util.Log;

public class main extends AppCompatActivity{
    private static final String TAG = "~#Main";

    //@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* HERE IS THE ISSUE*/
        ETPush.configureSdk(new ETPushConfig.Builder(this)

                        .setEtAppId("-APPID-")
                        .setAccessToken("-token-")
                        .setGcmSenderId("-senderid-")
                        .setAnalyticsEnabled(true)    // ET Analytics, default = false
                        .setWamaEnabled(true)         // Web & Mobile Analytics, default = false
                        .build()
                , new ETPushConfigureSdkListener() {
                    @Override
                    public void onETPushConfigurationSuccess(ETPush etPush, ETRequestStatus etRequestStatus) {
                        // Verify Google Play Services availability and notify the user of any exceptions
                        if (etRequestStatus.getGooglePlayServiceStatusCode() != ConnectionResult.SUCCESS && GoogleApiAvailability.getInstance().isUserResolvableError(etRequestStatus.getGooglePlayServiceStatusCode())) {
                            GoogleApiAvailability.getInstance().showErrorNotification(getApplicationContext(), etRequestStatus.getGooglePlayServiceStatusCode());
                        }

                        // Add attributes, tags, set subscriber key, etc.
                        try {
                            etPush.addAttribute("someKey", "someValue");
                            etPush.addTag("someTag");
                            // etc ...
                        } catch (ETException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }

                    @Override
                    public void onETPushConfigurationFailed(ETException e) {
                        // If we're here then your application will _NOT_ receive push notifications.
                        Log.e(TAG, e.getMessage());
                    }
                }
        );
    }
}
1

Keith C is partially correct. You must provide Application Context, but you also must do this from a class that extends Application. This should not be done from an Activity.

SDK Documentation WRT Configuring the SDK

  • Hi Bill, I did as Keith suggested and it compiled fine, but from what you are saying it will not work? The site you linked to is exactly where I got the code from so unfortunately I'd need more explanation than that. Could you provide more detail? Regards – Doug Apr 7 '17 at 13:35
  • The ETPush.configureSdk() portion of your code is fine, but you're making that call from an Activity class rather than an Application class. It will work fine as long as the application has been launched. See the documentation. It clearly states that this must be done from an Application class. Your application will not work with push if the application is removed from memory by Android or has not been launched after a reboot. – Bill Mote Apr 7 '17 at 14:29
  • Right! thanks for the heads up, both of you! Looks all good – Doug Apr 8 '17 at 17:04
0

The ETPushConfig.Builder constructor takes Android Application object as its argument and an Android Activity has a getApplication() method so based just on that API documentation this should at least compile:

ETPush.configureSdk(new ETPushConfig.Builder(this.getApplication())
        .set ...

But see Bill Mote's answer on how to get the code to not just compile but to work.

  • This is technically correct per the method contract, but the implementation documentation states that this call must be made from the Application's Application Class to ensure proper operation of the SDK. – Bill Mote Apr 7 '17 at 14:31
  • 1
    @BillMote Understood and I already upvoted your answer. Was going to delete mine but as yours mentioned it thought I'd leave it - maybe the wrong call. If you briefly explained how to add the custom Application class to your answer I think that would help Doug. – Keith C Apr 7 '17 at 14:35
  • Edit your answer in some way and I'll undo the downvote. I guess I should have had coffee before I read through these this morning. – Bill Mote Apr 7 '17 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.