0

我已经下载了 Android Rich Push Notification 示例代码。我已经进行了示例教程中描述的必要更改。

我是Urban Airship的初学者。我的 Android 清单文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:amazon="http://schemas.amazon.com/apk/res/android"
          package="com.jpc.urbanairship.richpush.sample"
          android:versionCode="1"
          android:versionName="1.0">

    <!-- minSdkVersion sets runtime compatibility ("will run on API level 9") -->
    <!-- targetSdkVersion should be set to the latest version tested, to disable compatibility modes -->
    <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="19" />

    <!-- REQUIRED for Urban Airship -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />  <!-- Required for Push -->

    <!-- MODIFICATION REQUIRED - Replace "com.urbanairship.richpush.sample" with your package name -->
    <permission android:name="com.jpc.urbanairship.richpush.sample.permission.UA_DATA" android:protectionLevel="signature" />
    <uses-permission android:name="com.jpc.urbanairship.richpush.sample.permission.UA_DATA" />
    <!-- The two elements above ensure that only this application has access to the Urban Airship provider and can receive push intents -->

    <!-- REQUIRED PERMISSIONS for GCM -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- This app has permission to register with GCM and receive message -->
    <!-- Required MODIFICATION REQUIRED - Replace "com.urbanairship.richpush.sample" with your package name -->
    <permission android:name="com.jpc.urbanairship.richpush.sample.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.jpc.urbanairship.richpush.sample.permission.C2D_MESSAGE" />
    <!-- The two elements above ensure that only this application can receive the messages and registration result -->

    <!-- REQUIRED PERMISSIONS for ADM -->
    <uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE" />
    <!-- Required MODIFICATION REQUIRED - Replace "com.urbanairship.richpush.sample" with your package name -->
    <permission android:name="com.jpc.urbanairship.richpush.sample.permission.RECEIVE_ADM_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.jpc.urbanairship.richpush.sample.permission.RECEIVE_ADM_MESSAGE" />
    <!-- The two elements above ensure that only this application can receive the messages and registration result -->

    <!-- OPTIONAL Urban Airship Settings -->
    <!-- REQUIRED FOR LOCATION -->
    <!-- Use ACCESS_COARSE_LOCATION if GPS access is not necessary -->
    <!-- uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!--  OPTIONAL - This permission is only necessary if your app has multiple processes -->
    <!--  <uses-permission android:name="android.permission.BROADCAST_STICKY" /> -->

    <application
        android:label="@string/app_name"
        android:icon="@drawable/ua_launcher"
        android:name="com.jpc.urbanairship.richpush.sample.RichPushApplication"
        android:theme="@style/Theme.AppCompat">

        <service android:name="com.urbanairship.richpush.RichPushUpdateService"/>

        <activity android:name="com.jpc.urbanairship.richpush.sample.MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.jpc.urbanairship.richpush.sample.inbox.InboxActivity" />
        <activity android:name="com.jpc.urbanairship.richpush.sample.preference.PushPreferencesActivity" />
        <activity android:name="com.jpc.urbanairship.richpush.sample.inbox.MessageActivity" />

        <!-- Optional: This is an example of one of the many ways to handle deep
        linking in the application.  To use with your application, update the data
        scheme to be unique for the application and modify ParseDeepLinkActivity.parseDeepLink
        method to match your application's deep link parsing -->
        <activity android:name="com.jpc.urbanairship.richpush.sample.ParseDeepLinkActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <!-- Handles any vnd.urbanairship.richpush://deeplink URI's -->
                <data
                    android:scheme="vnd.urbanairship.richpush" android:host="deeplink" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>

        <!-- The provider is needed for the RichPush Widget -->
        <receiver android:name="com.jpc.urbanairship.richpush.sample.widget.RichPushWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <!-- This specifies the widget provider info -->
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widgetinfo" />
        </receiver>

        <!-- The service serving the RemoteViews to the collection widget -->
        <service
            android:name="com.jpc.urbanairship.richpush.sample.widget.RichPushWidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS"
            android:exported="false" />

        <!-- OPTIONAL, if you want to receive push, push opened and registration completed intents -->
        <!-- Replace the receiver below with your package and class name -->
        <receiver android:name="com.jpc.urbanairship.richpush.sample.PushReceiver"
                  android:exported="false">
            <intent-filter>
                <action android:name="com.urbanairship.push.CHANNEL_UPDATED" />
                <action android:name="com.urbanairship.push.OPENED" />
                <action android:name="com.urbanairship.push.RECEIVED" />

                <!-- MODIFICATION REQUIRED - Use your package name as the category -->
                <category android:name="com.jpc.urbanairship.richpush.sample" />
            </intent-filter>
        </receiver>


        <!-- REQUIRED for ADM - You must explicitly enable ADM and declare whether your app cannot work without
        ADM (android:required="true") or can work without ADM (android:required="false").
        If you specify android:required="false", your app must degrade gracefully if ADM
        is unavailable. -->
        <amazon:enable-feature
            android:name="com.amazon.device.messaging"
            android:required="false" />

        <!-- REQUIRED for Google Play services (GCM)-->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <!-- Needed for Action.startActivityForResult -->
        <activity android:name="com.urbanairship.actions.ActionActivity" />

        <activity android:name="com.urbanairship.CoreActivity" />

        <!-- REQUIRED for Landing Pages
        MODIFICATION REQUIRED:
         - Set or Remove the parent activity
         - Set or Remove the theme.  Removing the theme will cause the landing page
         to use the default theme for the application.  If the theme allows an action
         bar and is running on a honeycomb or newer device, the action bar will enable
         up navigation.
         - For more customization details, see com.urbanairship.actions.LandingPageActivity -->
        <activity
            android:name="com.urbanairship.actions.LandingPageActivity"
            android:parentActivityName="com.jpc.urbanairship.richpush.sample.MainActivity"
            android:theme="@style/LandingPage"
            android:exported="false">

            <!-- Sample layout, remove to use the default -->
            <meta-data
                android:name="com.urbanairship.action.LANDING_PAGE_VIEW"
                android:resource="@layout/landing_page_activity" />

            <!-- Optional: Landing page will start the parent activity if the landing
            page is the root task.  Also supports proper up navigation if the action
            bar is supported -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.urbanairship.richpush.sample.MainActivity" />

            <intent-filter>
                <action android:name="com.urbanairship.actions.SHOW_LANDING_PAGE_INTENT_ACTION" />
                <data android:scheme="http" />
                <data android:scheme="https" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- REQUIRED for PlayServiceUtils.handleAnyPlayServicesError to handle Google Play services recoverable errors. -->
        <activity
            android:name="com.urbanairship.google.PlayServicesErrorActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <!-- REQUIRED for Urban Airship Push. The priority is important to be set lower than the
               application's push intent receiver in order for the push intent receiver to handle push intents
               before the core receiver. This allows the application to launch any activities before Urban
               Airship performs any actions or falls back to launching the application launch intent. -->
        <receiver android:name="com.urbanairship.CoreReceiver"
                  android:priority="-999"
                  android:exported="false">

            <intent-filter>
                <action android:name="com.urbanairship.push.OPENED" />

                <!-- MODIFICATION REQUIRED - Use your package name as the category -->
                <category android:name="com.jpc.urbanairship.richpush.sample" />
            </intent-filter>
        </receiver>

        <!-- REQUIRED for GCM -->
        <receiver
            android:name="com.urbanairship.push.GCMPushReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">

            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!-- MODIFICATION REQUIRED - Use your package name as the category -->
                <category android:name="com.jpc.urbanairship.richpush.sample" />
            </intent-filter>
        </receiver>

        <!-- REQUIRED for ADM -->
        <receiver
            android:name="com.urbanairship.push.ADMPushReceiver"
            android:permission="com.amazon.device.messaging.permission.SEND">
            <intent-filter>
                <action android:name="com.amazon.device.messaging.intent.REGISTRATION" />
                <action android:name="com.amazon.device.messaging.intent.RECEIVE" />

                <!-- MODIFICATION REQUIRED - Use your package name as the category -->
                <category android:name="com.jpc.urbanairship.richpush.sample" />
            </intent-filter>
        </receiver>

        <!-- REQUIRED for Urban Airship -->
        <service android:name="com.urbanairship.push.PushService" android:label="Push Notification Service" />
        <service android:name="com.urbanairship.analytics.EventService" android:label="Event Service" />
        <service android:name="com.urbanairship.actions.ActionService" />
        <service android:name="com.urbanairship.richpush.RichPushUpdateService" />

        <!-- OPTIONAL for Urban Airship Location (for segments support) -->
        <service android:name="com.urbanairship.location.LocationService" android:label="Segments Service" />

        <!-- This is required for persisting preferences related to push and location -->
        <!-- MODIFICATION REQUIRED - Replace "com.urbanairship.richpush.sample" with your package name -->
        <provider
            android:name="com.urbanairship.UrbanAirshipProvider"
            android:authorities="com.jpc.urbanairship.richpush.sample.urbanairship.provider"
            android:permission="com.jpc.urbanairship.richpush.sample.permission.UA_DATA"
            android:exported="true"
            android:multiprocess="true" />
    </application>

</manifest>

我没有对 MainActivity.java 文件进行任何更改。这是 MainActivity 文件:

public class MainActivity extends ActionBarActivity implements
ActionBar.OnNavigationListener {

    ArrayAdapter<String> navAdapter;
    RichPushUser user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_activity);
        this.configureActionBar();

        this.user = UAirship.shared().getRichPushManager().getRichPushUser();

    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }

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

        // Handle any Google Play services errors
        if (PlayServicesUtils.isGooglePlayStoreAvailable()) {
            PlayServicesUtils.handleAnyPlayServicesError(this);
        }

        // Activity instrumentation for analytic tracking
        Analytics.activityStarted(this);
    }

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

        // Activity instrumentation for analytic tracking
        Analytics.activityStopped(this);
    }

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

        // Show a message dialog if the pending message id is not null
        String pendingMessageId = getIntent().getStringExtra(RichPushApplication.EXTRA_OPEN_MESSAGE_ID);
        if (!UAStringUtil.isEmpty(pendingMessageId)) {
            getIntent().removeExtra(RichPushApplication.EXTRA_OPEN_MESSAGE_ID);
            showRichPushMessage(pendingMessageId);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.preferences:
            this.startActivity(new Intent(this, PushPreferencesActivity.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        String navName = this.navAdapter.getItem(itemPosition);
        if (RichPushApplication.HOME_ACTIVITY.equals(navName)) {
            // do nothing, we're here
        } else if (RichPushApplication.INBOX_ACTIVITY.equals(navName)) {
            Intent intent = new Intent(this, InboxActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            this.startActivity(intent);
        }

        return true;
    }

    /**
     * Displays the rich push message in a RichPushMessageDialogFragment
     * @param messageId The specified message id
     */
    private void showRichPushMessage(String messageId) {
        RichPushMessageDialogFragment message = RichPushMessageDialogFragment.newInstance(messageId);
        message.show(this.getSupportFragmentManager(), "message");
    }

    /**
     * Configures the action bar to have a navigation list of
     * 'Home' and 'Inbox'
     */
    private void configureActionBar() {
        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        this.navAdapter = new ArrayAdapter<String>(this, android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item,
                RichPushApplication.navList);
        actionBar.setListNavigationCallbacks(this.navAdapter, this);
    }

    /**
     * Sets the action bar navigation to show 'Home'
     */
    private void setNavigationToMainActivity() {
        int position = this.navAdapter.getPosition("Home");
        getSupportActionBar().setSelectedNavigationItem(position);
    }
}

我在运行示例代码时遇到了这个问题:

java.lang.NoClassDefFoundError: com.urbanairship.R$string
at com.urbanairship.push.NotificationActionButtonGroupFactory.createUrbanAirshipGroups(NotificationActionButtonGroupFactory.java:40)

at com.urbanairship.push.PushManager.<init>(PushManager.java:198)

at com.urbanairship.push.PushManager.<init>(PushManager.java:186)

请帮助我获得解决方案。提前致谢。

4

1 回答 1

2

如果您使用的是 Eclipse 或 Android Studio,我无法直接从您的问题中判断,但我在使用 Android Studio 时遇到了这个问题。

我一直在我的项目的 libs 文件夹中包含 urbanairship-lib-xxxjar 文件,但这会产生与您遇到的相同的错误,即使编译很好。

我做了什么:

  1. 从我的 libs 文件夹中删除了 jar。
  2. 遵循来自http://docs.urbanairship.com/build/push/android.html的 Android Studio 说明。精简:将这些存储库/依赖项添加到您的 build.gradle:
repositories {
    mavenCentral()

    flatDir {
        dirs 'aars'
    }
}

...

dependencies {
    compile 'com.google.android.gms:play-services:5.0.89'
    compile 'com.android.support:support-v4:20.+'
    compile 'com.urbanairship:urbanairship-lib:+@aar'
}
  1. 最后,由于 build.gradle 将寻找顶级目录 'aars',因此在项目的根目录下创建一个目录,然后插入 urbanairship-lib-xxxaar(而不是 jar)。aar 文件位于您为示例教程下载的 ua-android-lib-latest.zip 中。

不幸的是,我不熟悉使用 Eclipse 的任何解决方案。

于 2014-10-14T18:16:00.947 回答