Flutter:通过Flutter执行代码时未收到PendingIntents

时间:2019-04-15 17:19:42

标签: android-intent flutter

我用Java编写了一些本地android代码,而我的应用程序在Flutter中则需要这些代码。在这段代码中,我使用PendingIntent安排通知。当我在编辑android部件时运行代码(在android studio中打开android文件夹并运行app)时,我的通知将按预期方式显示和运行。但是,当我打开应用程序的飞镖/颤振部分并运行main.dart时,我所有的功能都与PendingIntent的中断有关。

其中一个奇怪的伪像是它似乎只发生在广播中,而不发生在活动中。解释:主要的轻击操作是将MainActivity带到前台,这始终有效。我还向通知中添加了执行广播的操作按钮,这些按钮显示了前面描述的问题。

我尝试查看我的AndroidManifest.xmlbuild.gradle,并检查是否所有来自androidX的库都已更新(还仔细检查了依赖项)。

用于构建通知的代码(仅使用NotificationManager.notify()即可显示通知)

private static NotificationCompat.Builder getBuilder(Context context, NotificationData data) {
        Intent tapIntent = new Intent(context, getMainActivityClass(context));
//      tapIntent.setAction(ACTION_OPEN);
        PendingIntent tapPendingIntent = PendingIntent.getActivity(context, 0, tapIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Intent snoozeIntent = new Intent(context, SnoozeBroadcast.class);
//      snoozeIntent.setAction(ACTION_SNOOZE);
        snoozeIntent.putExtra(SnoozeBroadcast.SNOOZE_ID, data.id);
        snoozeIntent.putExtra(SnoozeBroadcast.SNOOZE_TITLE, data.title);
        snoozeIntent.putExtra(SnoozeBroadcast.SNOOZE_MESSAGE, data.message);
        snoozeIntent.putExtra(SnoozeBroadcast.SNOOZE_DELAY, data.snoozeDelay);
        PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(context, 1, snoozeIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Intent cancelIntent = new Intent(context, CancelBroadcast.class);
//      cancelIntent.setAction(ACTION_CANCEL);
        cancelIntent.putExtra(CancelBroadcast.CANCEL_ID, data.id);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(context, 2, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if(snoozePendingIntent == null) System.out.println("snooze null");
        if(cancelPendingIntent == null) System.out.println("cancel null");

        // TODO: get channel from arguments
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(data.title)
                .setContentText(data.message)
                .setContentIntent(tapPendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .addAction(R.drawable.notification_icon, "Snooze", snoozePendingIntent)
                .addAction(R.drawable.notification_icon, "Cancel", cancelPendingIntent)
                .setAutoCancel(true);
        return builder;
    }

用于接收CancelBroadcast

的代码
public class CancelBroadcast extends BroadcastReceiver {
    public static final String CANCEL_ID = "com.example.my_app.cancel_id";

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("received");
        Bundle data = intent.getExtras();
        int id = data.getInt(CANCEL_ID);
        FlutterNotificationsPlugin.cancelById(context, id);
    }
}

如前所述,当我按下通知上的“ Snooze”按钮(显示正常)时,我希望执行onReceive()方法-将一行写入stdout。在运行app时会发生这种情况,而在运行main.dart时却不会发生。

0 个答案:

没有答案