【问题标题】:Notifications don't come through when the app is closed关闭应用程序时不会收到通知
【发布时间】:2012-05-30 12:35:58
【问题描述】:

我有一个使用 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”。

编辑

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

【问题讨论】:

  • 当您的应用未激活时收到推送消息时,您希望如何显示您的通知?你想使用android通知(状态栏)吗?
  • 是的。目前推送消息显示在通知栏中。我只是希望它在我的应用未打开时做同样的事情
  • 激活时你会得到它们吗? (例如,推送消息被传递)?
  • 是的,如果我的应用程序处于活动状态或在后台打开,推送消息将被传递并显示
  • 然后需要查看您的 C2DMPushReceiver,因为它应该在后台处理内容。如果是您将收到的信息传递给服务或活动,那么您需要获取唤醒锁以使您拥有“CPU时间”

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


【解决方案1】:

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

我认为实际的问题是应用程序被强制关闭时广播接收器被杀死,但我不确定。

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

【讨论】:

    【解决方案2】:

    你的项目包真的命名为“mypackage”吗?你的接收器等在那个包裹里吗? 因为看起来所有“myPackage”都应该替换为“com.urbanairship”

    【讨论】:

    • 不,它并没有真正命名为“myPackage”。那就是从问题中删除敏感信息。在本教程中:docs.urbanairship.com/display/DOCS/… 我已将所有“com.urbanairship.push.sample”替换为我的包名,因为这似乎是必需的
    • 好吧,如果您认为您的包名真的是一个敏感的主题,那么恐怕我无法帮助您。因为我在你的 xml 文件中找不到正面或反面,因为有些东西被称为“com.urbanairship”和一些“mypackage”,我怎么知道你打错了哪个,哪个打对了?以及如何知道它们真的与您的包裹相匹配。我在c2dm中处理了很多,这是一个非常大的问题。
    • 编辑问题以包含整个清单并隐藏较少的包信息
    • 好,我有点不安,因为您在 1 个包中安装了接收器,并且意图过滤器指向另一个包。但是如果你收到推送消息应该没问题,然后我需要查看你的接收器代码。
    • 这是我第一次不得不使用接收器,所以可能有很多问题。我会接受任何建议。我只是复制并修改了 Urban Airship 示例代码。我现在将编辑我的问题
    猜你喜欢
    • 2017-11-23
    • 2018-04-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多