Android通知操作未决Intent触发接收方,但接收方intent内容为空

时间:2019-01-09 10:13:11

标签: android notifications broadcastreceiver

在运行“ Pie”的设备上的我的应用程序中,当作业(yigit / android-priority-jobqueue)失败时,我会通过重试操作触发通知。操作按钮“再试一次”将广播接收器按预期方式调用onReceive,并且操作名称正确,但意图附加项为空。通道也已创建。

通知代码如下:

    NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 123;

    Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    Intent tryAgainIntent = new Intent(getApplicationContext(), MyReceiver.class);
    tryAgainIntent.setAction(TRY_AGAIN_ACTION);
    tryAgainIntent.putExtra(Extras.POST_ID, postId);
    PendingIntent tryAgainPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, tryAgainIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "com.example.android.GENERAL_NOTIFICATIONS")
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle("Post sync failed")
            .setContentText("Syncing the post failed")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setWhen(System.currentTimeMillis())
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText("Syncing the post failed, try again?"))
            .setContentIntent(contentPendingIntent)
            .addAction(R.drawable.ic_logo, "Try Again", tryAgainPendingIntent)
            .setAutoCancel(true);

    // send the notification
    manager.notify(notificationId, builder.build());

我的接收器如下:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Bundle extras = intent.getExtras();

        if(TRY_AGAIN_ACTION.equals(action)) {
            // gets here fine

            Long postId = intent.getLongExtra(Extras.POST_ID, -1);
            // postId is always -1

            Long postId = (Long) extras.get(Extras.POST_ID);
            // postId is always -1 here also
        }
    }
}

我的清单条目是:

<receiver
    android:name=".receivers.MyReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="TRY_AGAIN_ACTION" />
    </intent-filter>
</receiver>

我很困惑为什么除了接收器中的意向附加项为空以外,其他所有功能都可行,有什么建议吗?

0 个答案:

没有答案