问题
I wrote some native android code in java, which I need for my app build in flutter. In this code, I schedule notifications using PendingIntent
. When I run the code when editing the android part (opening the android folder in android studio and running app
), my notifications show and work like expected. However, when I open the dart/flutter part of my app and run main.dart
my all functionality to do with PendingIntent
s breaks.
A strange artifact in this is that it only seems to happen on Broadcasts, and not on Activities. To explain: The main tap action is to bring the MainActivity
to the foreground, which always works. I've also added action buttons to the notification which execute broadcasts, which show the earlier described problem.
I've tried looking at my AndroidManifest.xml
and build.gradle
, and checking if all my libraries from androidX are updated (also double checked the dependencies).
The code for building the notification (showing it simply uses 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;
}
The code for receiving the 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);
}
}
As said before, I expect the onReceive()
method to be executed - which write a line to stdout
- when I press the "Snooze" button on the notification (which shows fine). This happens when running app
but not when running main.dart
.
来源:https://stackoverflow.com/questions/55694333/flutter-pendingintents-not-received-when-code-is-executed-through-flutter