Notification PendingIntent 附加项始终为空

Notification PendingIntent extras always empty

我整天都在阅读有关此内容的不同类型的解决方案,但到目前为止都没有奏效。

我有一个将更新发送到 ResultReceiver 的上传服务 (IntentService)。接收方创建和管理通知。

点击通知(错误或成功)后,加载 MainActivity。但是包总是空的。如何将我的代码更改为 access/get 捆绑包?

如果我在另一个 Activity 中,如果该应用程序处于后台并且该应用程序已停止,则会发生这种情况。

CustomResultReceiver:

private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;

public static final String RETURN_MESSAGE = "RETURN_MESSAGE";
public static final String RETURN_STATUS = "RETURN_STATUS";

private final int id = 1;

public CustomResultReceiver(Handler handler, Context context) {
        super(handler);
        mContext = context;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle("Upload data")
                .setContentText("uploading...")
                .setSmallIcon(R.mipmap.ic_launcher);
    }

创建通知。在 onReceiveResult 中,我通知通知,如果发生错误或上传成功,我添加一个 ContentIntent。

protected void onReceiveResult(int resultCode, Bundle resultData) {

    super.onReceiveResult(resultCode, resultData);
    String message;
    boolean success;    

switch (resultCode) {
case RESULT_ERROR:

                message = resultData.getString(BROADCAST_MESSAGE, null);
                boolean failed = resultData.getBoolean(EXTENDED_ACTION_FAILED, false);
                if (failed) {
                    mBuilder.setProgress(0, 0, false);
                    mBuilder.setContentText("Aborted! Error while uploading.");
                    mBuilder.setContentIntent(createContentIntent(message, false));
                    mNotificationManager.notify(id, mBuilder.build());
                }
                break;
 case RESULT_FINISHED:
                message = resultData.getString(UPLOAD_MESSAGE, null);
                success = resultData.getBoolean(UPLOAD_STATUS, false);
                if (success) {
                    mBuilder.setProgress(0, 0, false);
                    mBuilder.setContentText("Upload successful");
                    mBuilder.setContentIntent(createContentIntent(message, true));
                    mNotificationManager.notify(id, mBuilder.build());
                }
                break;
}
}

createContentIntent() 提供带有结果消息和成功布尔值的 PendingIntent:

private PendingIntent createContentIntent(String message, boolean success) {
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.putExtra(RETURN_MESSAGE, message);
        intent.putExtra(RETURN_STATUS, success);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setAction("NOTIFIY RESPONSE");
        return PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

在我使用的接收器中:

android.app.NotificationManager;
android.app.PendingIntent;
android.content.Context;
android.content.Intent;
android.os.Bundle;
android.os.Handler;
android.os.ResultReceiver;
android.support.v4.app.NotificationCompat;

MainActivity 是一个 android.support.v7.app.AppCompatActivity.

为了创建 PendingIntent,我已经尝试了几种不同的标志组合。

以下是清单中的一些片段:

我的权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>

我通过通知打开的Activity:

<activity
            android:name=".views.activities.MainActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="landscape"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.RUN"/>
            </intent-filter>
</activity>

这里是服务:

<service android:name=".services.UploadService"/>

当我从 ResultReceiver 启动 activity 时,会发生以下情况:

activity 启动并调用 onCreate,我会像这样检查包:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);

    ...
    Intent old = getIntent();
    if (old != null) {
        Bundle extras = old.getExtras();
        if (extras != null) {
            Log.d(MainActivity.class.getSimpleName(), "Message: " + extras.getString(CustomResultReceiver.RETURN_MESSAGE, "empty"));
            Log.d(MainActivity.class.getSimpleName(), "Status: " + extras.getBoolean(CustomResultReceiver.RETURN_STATUS, false));
        }
    }
    ...
}

所以我终于找到了问题所在。在调试了几个标志选项并更改了请求代码之后。这里的请求码与问题无关。

负责空(非空)包的是标志 ACTIVITY_NEW_TASK。

如果我为 Intent 或 PendingIntent 使用所有其他经过测试的标志,一切正常。

android doc 为 ACTIVITY_NEW_TASK 说了以下内容:

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

这似乎表明旧 Intent 仍然存在,而不是我在接收器中构建的新 Intent。有趣的事实:即使我关闭应用程序然后启动 PendingIntent 包也是空的。就我的理解而言,文档会以其他方式说明(activity 不在任务中?-> 带有新 activity 的新任务)。