【问题标题】:Notification PendingIntent extras always empty通知 PendingIntent 额外内容始终为空
【发布时间】:2018-02-13 12:42:50
【问题描述】:

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

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

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

如果我在另一个 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,我已经尝试了几种不同的标志组合。

以下是清单中的一些 sn-ps:

我的权限:

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

活动开始并调用 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));
        }
    }
    ...
}

【问题讨论】:

  • 请发布您的清单
  • 并将代码发布到您希望在 extras Bundle 中找到内容的位置。
  • @DavidWasser 我添加了(可能)相关的清单 sn-ps 和 MainActivity 的 onCreate。我还尝试在未调用的 onNewIntent 方法中获取捆绑包(其他一些线程建议这样做)。我还尝试更改没有帮助的请求代码。
  • 是的,我尝试了另一个代码,但没有任何改变。但现在我尝试了其他标志。现在的工作是:对于意图 -> intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 和对于 PendingIntent.getActivity() -> PendingIntent.FLAG_CANCEL_CURRENT
  • 如果您对我为什么不工作感兴趣,我添加了一个答案。

标签: android android-notifications android-pendingintent


【解决方案1】:

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

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

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

android doc 表示以下 ACTIVITY_NEW_TASK:

当使用这个标志时,如果一个任务已经在为这个活动运行 您现在正在开始,则不会开始新的活动; 相反,当前任务将简单地被带到前面 屏幕上一次显示的状态。见 FLAG_ACTIVITY_MULTIPLE_TASK 用于禁用此行为的标志。

这似乎表明旧 Intent 仍然存在,而不是我在接收器中构建的新 Intent。有趣的事实:即使我关闭了应用程序然后启动了 PendingIntent,捆绑包也是空的。根据我的理解,文档会另外说明(活动不在任务中?-> 新任务和新活动)。

【讨论】:

  • 所以试试this - 不需要玩标志,它有用吗?
  • 当然,如果你想调用活动,你必须使用getActivity - 我使用了服务,因为它更容易测试
  • 好吧,就像我上面说的那样,它现在正在工作。但我还是尝试了你的建议。没有标志但使用您的 onStartCommand 覆盖我的服务停止工作,它工作正常。如果我只使用您建议的活动部分的无标志部分,它也可以工作,但不是在 onCreate 中,而是在 onNewIntent 方法中获得 Intent。所以它也使用这种方法。
  • 服务正常,会在哪里崩溃?在Log.d?对于活动,是的:如果未启动,则调用 onCreate,否则您必须在 onNewIntent 方法中捕获 Intent
  • 它没有崩溃,只是没有启动可能是由于我的服务调用。在您的方法中,我使用一个按钮通过activity.startService(intent) 启动服务,您使用通知通过PendingIntent 启动服务。但它现在又可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2023-03-27
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多