通知PendingIntent附加内容始终为空 [英] Notification PendingIntent extras always empty

查看:89
本文介绍了通知PendingIntent附加内容始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在用不同的解决方案来了解这一点,但到目前为止还没有奏效.

I read about this all day long with different kind of solutions but non worked so far.

我有一个上传服务(IntentService),可将更新发送到ResultReceiver.接收方创建并管理通知.

I have an upload service (IntentService) that sends updates to a ResultReceiver. The receiver creates and manages the Notification.

单击通知(错误或成功)后,将加载MainActivity.但是捆绑销售商品始终为空.如何更改我的代码以访问/获取捆绑包?

After clicking on the notification (error or success) the MainActivity is loaded. But the bundle is always null. How can I change my code to access/get the bundle?

如果我处于另一个活动中,或者该应用程序处于后台并且停止运行,则会发生这种情况.

This happens if I'am in another Activity, if the App is in background and if the app is stopped.

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.

Creates the Notification. In onReceiveResult I notify the notification and if an error occured or the upload is successful I add a 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提供一个结果消息和一个成功布尔值:

createContentIntent() provides a PendingIntent with a result message and a success boolean:

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.

The MainActivity is an android.support.v7.app.AppCompatActivity.

为了创建PendingIntent,我已经尝试了几种不同的Flag组合.

For the creation of the PendingIntent I already tried several different Flag combinations.

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

Here are some snippets from the manifest:

我的权限:

<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"/>

我通过通知打开的活动:

The Activity that I open via the Notification:

<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启动活动时,会发生以下情况:

For when I launch the activity from the ResultReceiver following happens:

活动开始,并在我检查捆绑的地方调用onCreate,如下所示:

The activity starts and onCreate is called where i check for the bundle like this:

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));
        }
    }
    ...
}

推荐答案

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

So I finally found the problem. After debugging several flag options and changing request codes. The request code here is irrelevant to the problem.

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

Responsible for the empty (not null) bundle is the flag ACTIVITY_NEW_TASK.

如果我将所有其他经过测试的标志用于Intent或PendingIntent,则一切正常.

If I went with all other tested flags for the Intent or the PendingIntent everything works fine.

android doc 对于ACTIVITY_NEW_TASK说以下内容:

The android doc says following for ACTIVITY_NEW_TASK:

使用此标志时,如果活动已在运行任务 您现在开始,则新的活动将不会开始; 而是将当前任务简单地带到 屏幕上显示的是最后一次进入的状态.参见FLAG_ACTIVITY_MULTIPLE_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,该捆绑包还是空的.据我了解,该文档将另行声明(活动不在任务中吗?->具有新活动的新任务).

This seems to indicate that the old Intent is still present and not the new one which I build in the receiver. Funny fact: even if I closed the app and then started the PendingIntent the bundle was empty. The doc as for my understanding would state it otherwise (activity not in a task? -> new task with new activity).

这篇关于通知PendingIntent附加内容始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆