Urban Airship Rich Push Notification Android出错

时间:2014-10-13 11:38:19

标签: android urbanairship.com richpush

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

我是初学者,致力于城市飞艇。 我的Android Manifest文件如下:

<?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)

请帮我解决问题。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

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

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

我做了什么:

  1. 从我的libs文件夹中删除了jar。
  2. 按照http://docs.urbanairship.com/build/push/android.html中的Android Studio说明操作。压缩:将这些存储库/依赖项添加到build.gradle:
  3. 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将寻找顶级目录&#39; aars&#39;,在项目的根目录创建一个,而是插入urbanairship-lib-xxxaar(不是jar )。 aar文件位于您为示例教程下载的ua-android-lib-latest.zip中。
    2. 不幸的是,我对使用Eclipse的任何解决方案都不熟悉。