android - 关闭应用程序时不会收到通知

标签 android push-notification android-c2dm urbanairship.com

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

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

任何帮助都会很棒

编辑

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

编辑

事实证明,我的应用程序在关闭时确实会收到通知,但在强制关闭后不会。看完感悟this问题。

最佳答案

事实证明,无论应用程序是否正在运行,通知都能正常工作。问题是当应用程序被强制关闭时。

我认为实际问题是 BroadcastReceiver 在强制关闭应用程序时被杀死,但我不确定。

这只是为了关闭问题,因为现在我意识到问题与问题标题不同,不太可能解决。

关于android - 关闭应用程序时不会收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10816434/

相关文章:

android - 测试 Activity 是否已使用 FLAG_ACTIVITY_CLEAR_TOP 启动

android - 如何获取用户指纹进行身份验证?

android - AppBar折叠/展开时如何保持工具栏固定在顶部?

iphone - 将 Urbanairship 集成到我的 iOS 应用程序中所需的最少文件集是什么?

android - 为什么我使用 Google Cloud To Device Messaging Service 为我的设备获取多个 Activity token ?

android - 只有在没有更多工作时才停止后台服务

ios - 当打开另一个通知时,如何防止通知从通知中心消失? iOS

android - 在多模块项目android中集成onesignal

android - 处理 Android 中的 C2DM 错误 ACCOUNT_MISSING

android-c2dm - 需要 C2DM collapse_key 实现解释