3

我有一个使用 Urban Airship 设置推送通知的 Android 应用程序。我的应用程序打开时通知工作正常,但我的应用程序关闭时我仍需要接收通知。我环顾四周,但没有找到有用的东西。我很确定问题出在我的清单中,所以就在这里。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="za.co.foo.android.financials"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

    <!-- Permissions for Urban Airship -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- Not sure if required for C2DM -->
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <!-- Only this application can receive the messages and registration result -->
    <permission android:name="za.co.foo.android.financials.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="za.co.foo.android.financials.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive message -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".InvestorRelations"
            android:label="@string/app_name" 
            android:screenOrientation="landscape" 
            android:theme="@android:style/Theme.Holo.NoActionBar">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- For Urban Airship -->
        <receiver android:name="com.urbanairship.CoreReceiver">
        </receiver>

        <receiver android:name="com.urbanairship.push.c2dm.C2DMPushReceiver"
                android:permission="com.google.android.c2dm.permission.SEND"
                android:enabled="true">
             <!-- Receive the actual message -->
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                 <category android:name="za.co.foo.android.financials" />
             </intent-filter>
             <!-- Receive the registration id -->
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                 <category android:name="za.co.foo.android.financials" />
             </intent-filter>
        </receiver>

        <receiver android:name="za.co.foo.android.financials.IntentReceiver" />

        <service android:name="com.urbanairship.push.PushService" />

    </application>

</manifest>

这是我的IntentReceiver

public class IntentReceiver extends BroadcastReceiver {

    private static final String logTag = "IR Intent Receiver";

    @Override
        public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();

        if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
            int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);

            Log.i(logTag, "Received push notification. Alert: "
                    + intent.getStringExtra(PushManager.EXTRA_ALERT)
                    + " [NotificationID="+id+"]");

            logPushExtras(intent);

        } else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
            Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));

            logPushExtras(intent);

            Intent launch = new Intent(Intent.ACTION_MAIN);
            launch.setClass(UAirship.shared().getApplicationContext(), InvestorRelations.class);
            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launch.putExtra("FromNotification", true);

            UAirship.shared().getApplicationContext().startActivity(launch);

        } else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
            Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
                    + ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
        }

    }

    public void logPushExtras(intent) {
        //Does some logging stuff
    }
}

任何帮助都会很棒

编辑

包括整个清单,只有我的公司名称更改为“foo”。

编辑

事实证明,我的应用在关闭时确实会收到通知,但在强制关闭后不会收到通知。看完这个问题才明白。

4

2 回答 2

2

It turns out the notifications worked fine whether the app was running or not. The problem was when the app was force-closed.

I think the actual problem is something along the lines of the BroadcastReceiver being killed when the app is force closed but I'm not sure.

This is just to close the question off because it's unlikely to be solved now that I realise the problem is different to the question title.

于 2012-06-01T06:59:31.227 回答
1

is your projects package really named "mypackage" ? and is your receiver etc. in that package? Because it looks like all "myPackage" should be repalced with "com.urbanairship"

于 2012-05-30T12:43:55.063 回答